aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Image Display in JFrame; What is Wrong With This Code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image Display in JFrame; What is Wrong With This Code?" Watch "Image Display in JFrame; What is Wrong With This Code?" New topic
Author

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

Henry Wilson
Greenhorn

Joined: Aug 05, 2013
Posts: 4
Hello,
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;
label.setOpaque(b);
label.setBackground(myColor);
frame.add(imageLabel);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}
}

Thank you
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 169
    
    1

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..


Ramesh-X
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Image Display in JFrame; What is Wrong With This Code?
 
Similar Threads
Image is not been added to a label
java swing repaint
'Stale' image reference is retained by JFrame desktop app--latest image won't display
JLabel pan image
Click On Image.