Reputation: 71
I dont know why I cant add image to my JPanel i use code:
class PanelGlowny extends JPanel {
private JLabel adam;
private JButton henryk;
PanelGlowny(){
this.setLayout(new BorderLayout());
ImageIcon imageurl = new ImageIcon("logo.jpg");
//Image img = imageurl.getImage();
adam = new JLabel(imageurl);
add(adam, BorderLayout.NORTH);
henryk = new JButton();
add(henryk, BorderLayout.CENTER);
}
}
Image is in the same folder as class, but if I use url to image it also do not add anything. This code adding button, but do not add image :(
The problem is probably with my JDE, or Sandbox or sth like this, because code should be fine.
Upvotes: 0
Views: 1185
Reputation: 21223
Try this:
imageurl = new ImageIcon(getClass().getResource("logo.jpg"));
Check How to Use Icons tutorial.
EDIT: loading remote image
Try that to load your image from web:
public static void main(String args[]) {
try {
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.INFORMATION_MESSAGE,
new ImageIcon(new URL("http://marinerczarter.pl/wp-content/themes/twentyten/images/headers/path.jpg")));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
Upvotes: 1