473,379 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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

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

Similar topics

0
by: Haim Ashkenazi | last post by:
Hi I'm trying to build a binary with py2exe. I've used the method suggested in the py2exe wiki about adding icon resources to apps. it build ok on win2k, but when I try to build the same on...
8
by: Adrian | last post by:
How do I put an icon on a form using code? Thank you.
1
by: Ellis Yu | last post by:
Hi all, I'd tried to follow the instructions from msdn for adding icon image for a control. I've checked the name of bmp file is same as the control name and turn the bmp file to "Embedded...
1
by: Sagaert Johan | last post by:
Hi How can i add an icon to my customcontrol so it appears in the toolbox ? How is this done in VS2005 ?
0
by: farseer | last post by:
Hi, is it possible to add an icon, toggle btn, or even a checkbox to the control box caption area of a form (this is there the minimize, maximize and close icons are located in the upper right...
0
by: porky008 | last post by:
I need to add a search button to this and have no clue how to do it. Can some one please help me on this? import java.awt.*; import java.awt.event.*; import javax.swing.*; import...
1
by: John | last post by:
I have a switchboard and wish to add an image to the buttons via the switchboard table. I have added an extra column in the switchboard table with the names of embedded ico images corresponding to...
0
by: mfsiddiq | last post by:
HI I wish to know how to add an icon in a tree control using custom treeRenderer.i have added a combo box to the tree and besides the combo box i wish to add an icon
4
by: Pascal | last post by:
Hi all, I'd tried to follow the instructions from msdn for adding icon image for a control. I've checked the name of bmp file is same as the control name and turn the bmp file to "Embedded...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.