Displaying Image Inside of Original Frame
I have a GUI form called SimpleGUI that is just a single JPanel. My goal is to display my URL image inside of the that JPanel.
When I try to add the image, I don't get an error, but nothing displays. The image DOES display if I create a new JFrame and add my JPanel to that. But then I have two Windows open.
How can I display my image in my original SimpleGUI form? (Code below)
Thank you!
public class SimpleGUI extends JFrame {
private JPanel imagesPanel;
private JFrame mainFrame;
public SimpleGUI() throws IOException {
super("GUI Page");
setContentPane(imagesPanel);
pack();
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(new Dimension(500, 500));
mainFrame = new JFrame();
imagesPanel.setLayout(new GridLayout());
imagesPanel.setBounds(0,0,200,200);
mainFrame.add(imagesPanel);
mainFrame.setVisible(true);
BufferedImage img1 = null;
try
{
URL url1 = new URL("http://cosplayidol.otakuhouse.com/wp-content/uploads/2012/06/s-1-1.jpg");
URLConnection conn1 = url1.openConnection();
conn1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
InputStream in1 = conn1.getInputStream();
img1 = ImageIO.read(in1);
} catch (IOException e)
{
e.printStackTrace();
}
JLabel wIcon = new JLabel(new ImageIcon(img1));
imagesPanel.add(wIcon);
}
}
Please sign in to leave a comment.
It's not clear what is initialized by the gui designer via the form and what is initialized by your own code.
Please provide a complete sampe project that will include the form and the code.