469,352 Members | 1,694 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Adding an icon to a JPanel

Hello, I'm trying to add an icon to a JPanel in the event where a button is pressed. I have the following code:
Expand|Select|Wrap|Line Numbers
  1. private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  2.         javax.swing.ImageIcon icon = new javax.swing.ImageIcon("/home/bbraun/NetBeansProjects/RPGI/sample2.jpg");
  3.         javax.swing.JLabel label = new javax.swing.JLabel();
  4.         label.setIcon(icon);
  5.         jPanel1.add(label);
  6. }
  7.  
I specify an absolute path where the image exists, but it won't display. With simpler examples I was able to get this to work. jPanel1 is nested in a JTabbedPane. Even if I change icon and label to member variables of the class it doesn't work. The JPanel shows up fine, but it has nothing inside.
Dec 8 '08 #1
7 20519
JosAH
11,448 Expert 8TB
Print the icon to see if it had successfully loaded:

Expand|Select|Wrap|Line Numbers
  1. System.out.println("icon: "+icon);
  2.  
If it prints "icon: null" the file could not be found. If the icon did load without errors we have to fiddle a bit with LayoutManagers in your JPanel.

kind regards,

Jos
Dec 8 '08 #2
It did indeed print the path, so it is loading properly. I set the layout using NetBeans, so the code is not the prettiest and I doubt you want to read through it. I have a main JFrame which contains, among other things, a tabbed panel which houses 2 panels. These do show up properly, but I'm having difficulties adding anything to these two panels. This is my first time creating a GUI so I do have much to learn.
Dec 8 '08 #3
JosAH
11,448 Expert 8TB
So you did add an ActionListener to a button, otherwise that code would never be executed. Can you add a bit of text to that JLabel as well so you can see if that label displays at all?

kind regards,

Jos
Dec 8 '08 #4
Yes, the action listener is working properly. I had it modify text in a different window and that worked. I added text to the label and it did not display in the jpanel.
Dec 8 '08 #5
JosAH
11,448 Expert 8TB
@KiddoGuy
Most likely your jpanel1 doesn't display; try to make your own quick and dirty JPanel; like this:

Expand|Select|Wrap|Line Numbers
  1. JPanel jpanel1= new JPanel() {
  2.    public void paintComponent(Graphics g) {
  3.       g.setColor(Color.RED);
  4.       g.fillRectangle(0, 0, this.getWidth(), this.getHeight());
  5.    }
  6. }
  7.  
This creates a JPanel that draws its entire background in red (quite visible). Check if you can see that JPanel;

kid regards,

Jos
Dec 8 '08 #6
Ooh yes, I can definitely see that rectangle. I think the problem is with using the JLabel, but I can't think of what it could be.
Dec 8 '08 #7
JosAH
11,448 Expert 8TB
@KiddoGuy
What happens when you make your entire JFrame a bit larger? A JPanel by default uses a FlowLayout and I don't know what it does when the horizontal size of a component (a JLabel) doesn't fit in the containing component (the JPanel).

kind regards,

Jos
Dec 8 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Haim Ashkenazi | last post: by
8 posts views Thread by Adrian | last post: by
1 post views Thread by Ellis Yu | last post: by
reply views Thread by porky008 | last post: by
4 posts views Thread by Pascal | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.