This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image Display in JFrame; What is Wrong With This Code?

Henry Wilson
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to display a Jpeg image in a JFrame. I got the JFrame with a title, but no Jpeg.
Below is the code I used, can anyone tell me what I did wrong?

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class BarbaraGreetings {
public static void main(String[] args) {

Color myColor = new Color(150,250,150);
JFrame frame = new JFrame();
frame.setSize(400, 400);
JLabel label = new JLabel("JFrame for Graphics Display");
String string = "java/testclipart.jpeg";
ImageIcon image = new ImageIcon(string);
JLabel imageLabel = new JLabel(image);
boolean b = false;


Thank you
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
Java MySQL Database Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default layout of JFrame is BorderLayout. When you use code frame.add(imageLabel); , the imageLabel is added to center. And when you write again frame.add(label); the label is added on the imageLabel. Therefore imageLabel is hidden from the label.

And remember that, when you set, setOpaque(false), the component makes no guarantees about painting all the bits within its rectangular bounds. And this is not saying that "it will definitely not paint all the bits". Though we that property false, there are instances where all the bits are painted..
I agree. Here's the link:
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic