This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes ImageIcon not showing Ubuntu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ImageIcon not showing Ubuntu" Watch "ImageIcon not showing Ubuntu" New topic
Author

ImageIcon not showing Ubuntu

Dwayne Brown
Greenhorn

Joined: Jan 16, 2012
Posts: 13
Hello all. I am trying to display in image in my swing application. I can add images just fine in Windows but not in Ubuntu. How can I accomplish this? Usually I add the code down below to add in windows.



That is a generic example but it does work. What is the equivalent in Linux
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

A similar path on Linux might be "/images/logo.png", but chances are that file won't exist (unless somehow you put it there).

A common approach is to include any icon images in your classpath (like your application's jar file), and then use the ClassLoader's getResourceAsStream(String) method to read the icon as a byte array. Then use the ImageIcon(byte[]) constructor to create your icon.


OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Dwayne Brown
Greenhorn

Joined: Jan 16, 2012
Posts: 13
Do you know of any tutorials for that in a Linux perspective? All books I have learned from come from a Windows or Mac point of view. They work but what you have described is new to me. Thank you for your help and response.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4571
    
    5

How to Use Icons

Also note that the forward slash / as a path separator applies to both Windows and Linux. Only the backslash \ is Windows-only.

Moving to the GUis section, where we normally discuss Swing stuff.


luck, db
There are no new questions, but there may be new answers.
Dwayne Brown
Greenhorn

Joined: Jan 16, 2012
Posts: 13
I figured out how in eclipse if you add the image to your project you can find out the full path no matter what platform you are on. The steps I took was right click on project and select to add a source folder. I named mine resources. Then copy pictures into the source folder. If you right click the picture and go to properties it will display the full path. You can copy and paste path into your ImageIcon. I know that is not practical for deployment. I just need to know how to pull the copied images from the resource folder. I tried the exact path from the resource folder and the images do not show.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

check out what Pete said. that might be what you want


SCJP
Visit my download page
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4571
    
    5

Dwayne Brown wrote:I know that is not practical for deployment. I just need to know how to pull the copied images from the resource folder.

Too much trouble to go through the tutorial I gave you a link for, huh?
Dwayne Brown
Greenhorn

Joined: Jan 16, 2012
Posts: 13
Darryl, I appreciate your response and I am going through that. I was not ignoring your suggestion but was following other examples and was eager to share what I found. All responses are greatly appreciated!
 
GeeCON Prague 2014
 
subject: ImageIcon not showing Ubuntu