Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

Henry Wilson
Posts: 4
  • 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
  • Likes 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..
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!