473,728 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding an icon to a JPanel

17 New Member
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 23130
JosAH
11,448 Recognized Expert MVP
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
KiddoGuy
17 New Member
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 Recognized Expert MVP
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
KiddoGuy
17 New Member
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 Recognized Expert MVP
@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
KiddoGuy
17 New Member
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 Recognized Expert MVP
@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
1378
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 win98 I get this error: copying C:\Python23\Lib\site-packages\py2exe\run.exe -> C:\My Documents\NS_Backup\dist\NS_Backup.exe Traceback (most recent call last): File "Setup.py", line 13, in ?
8
2280
by: Adrian | last post by:
How do I put an icon on a form using code? Thank you.
1
3576
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 Resource". After building the project again, the icon of my user control is the same as default. I'm not sure it's related to my VB.Net version is a standard edition or not. Anyone know what's wrong my steps? Thanks Rdgs
1
1535
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
1235
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 of a form) and also trap for user clicks on those controls? thanks much
0
2388
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 javax.swing.event.*; import javax.swing.JFrame.*; class Testing {
1
2592
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 each item and was hoping to read these and add the image on to transparent buttons along with the switchboard text items next to the buttons at runtime. The switchboard form has the ico images embedded elsewhere on the form to avoid having to...
0
1895
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
4185
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 Resource". After building the project again, the icon of my user control is the same as default. I'm not sure it's related to my VB.Net version is an express edition or not? Anyone know what's wrong ? I did this : Created a 16 x 16 pixd (256 colors)
0
8759
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9424
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9135
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8133
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4524
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3236
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2161
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.