ImageIcon Netbeans IDE

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 6177 Views - Last Post: 22 June 2009 - 03:53 PM Rate Topic: -----

#1 Fish11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-June 09

ImageIcon Netbeans IDE

Post icon  Posted 21 June 2009 - 04:05 AM

I used the code in a Java application I created with Netbeans IDE.
There is no error when I compile/run it, but the image is not displayed.
What is the problem and solution?

ImageIcon test = new ImageIcon ("pictures/test.png");
JButton button1 = new JButton (test);


Is This A Good Question/Topic? 0
  • +

Replies To: ImageIcon Netbeans IDE

#2 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: ImageIcon Netbeans IDE

Posted 21 June 2009 - 04:21 AM

Your window might be displaying before the image is loaded fully. You're supposed to do fiddly stuff with ImageObservers. I can never be bothered. Check out ImageIO.read(file).

ImageIcon test = new ImageIcon(ImageIO.read(new File("pictures/test.png")));
Was This Post Helpful? 0
  • +
  • -

#3 gawdlike   User is offline

  • you can't fix stupid.

Reputation: -7
  • View blog
  • Posts: 647
  • Joined: 16-November 08

Re: ImageIcon Netbeans IDE

Posted 21 June 2009 - 05:46 AM

It could be as simple as not having a picture by that name in the project folder?

And at a second glance, do you add the button to the window?

This post has been edited by gawdlike: 21 June 2009 - 05:48 AM

Was This Post Helpful? 0
  • +
  • -

#4 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: ImageIcon Netbeans IDE

Posted 21 June 2009 - 07:39 AM

I think he could be missing either the picture or actually adding his button to the JFrame

This is pretty aweful however it shows that doing what he does should work

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Test extends JFrame
{
	private JButton fire;
	private ImageIcon fox;
	
	Test()
	{
		super("FireFox Pic");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(300, 300);
		
		// setup button
		fox = new ImageIcon("firefox.jpg");
		fire = new JButton( fox );
		
		this.add(fire);	
		
	}
	
	public static void main(String[] args)
	{
		Test p = new Test();
		p.setVisible(true);	
	}	
}



And the image to go with is attached along with an image of how the app looks when executed

Attached image(s)

  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 Fish11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-June 09

Re: ImageIcon Netbeans IDE

Posted 21 June 2009 - 03:40 PM

I am a beginner but I think the obvious mistakes I already canceled out. The image is in the specified folder and I did add the picture to a frame (the button is visible, not the picture).
I guess it should be something specific for Netbeans, some configuration issue?
Was This Post Helpful? 0
  • +
  • -

#6 Fish11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-June 09

Re: ImageIcon Netbeans IDE

Posted 21 June 2009 - 03:58 PM

Just copy-pasted the code from bbq (adjusted path to image) and I get a 300,300 window with 1 large button (but no image).
Was This Post Helpful? 0
  • +
  • -

#7 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: ImageIcon Netbeans IDE

Posted 21 June 2009 - 04:42 PM

Tell us exactly where you have place the image
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: ImageIcon Netbeans IDE

Posted 21 June 2009 - 06:35 PM

View Postcfoley, on 21 Jun, 2009 - 03:21 AM, said:

Your window might be displaying before the image is loaded fully. You're supposed to do fiddly stuff with ImageObservers. I can never be bothered. Check out ImageIO.read(file).

ImageIcon test = new ImageIcon(ImageIO.read(new File("pictures/test.png")));

New ImageIcon(filename); is supposed to handle all that for you
the problem is somewhere else
Was This Post Helpful? 0
  • +
  • -

#9 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: ImageIcon Netbeans IDE

Posted 21 June 2009 - 07:40 PM

The problem is clearly that the location of the image is incorrect.

Correctly me if i am wrong but it is possible the image might have the extension .PNG rather than .png which would make a difference. Turn on show file extensions for known types in folderoptions (if using windows) and confirm you have the exact spelling of the file name inclusive of its extension.

Also make sure the relative path is correct.
Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: ImageIcon Netbeans IDE

Posted 21 June 2009 - 08:08 PM

View Postbbq, on 21 Jun, 2009 - 06:40 PM, said:

Also make sure the relative path is correct.

That must be more be than anything else the problem
Was This Post Helpful? 0
  • +
  • -

#11 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: ImageIcon Netbeans IDE

Posted 22 June 2009 - 01:18 AM

View Postpbl, on 22 Jun, 2009 - 01:35 AM, said:

View Postcfoley, on 21 Jun, 2009 - 03:21 AM, said:

Your window might be displaying before the image is loaded fully. You're supposed to do fiddly stuff with ImageObservers. I can never be bothered. Check out ImageIO.read(file).

ImageIcon test = new ImageIcon(ImageIO.read(new File("pictures/test.png")));

New ImageIcon(filename); is supposed to handle all that for you
the problem is somewhere else


So it does. That's rather embarrassing. :( (but good to know).

Knowing where the current directory is in Java can be a wee bit tricky. Your file is probably close but not in the right place. Let's do some tests:

		File file = new File("pictures/test.png");
		System.out.println("File exists: " + file.exists());
		System.out.println(file.getAbsolutePath());



This will tell you if Java is finding the file and where it is looking for it. You should be able to work out where to move it to or how to alter your code from there.
Was This Post Helpful? 0
  • +
  • -

#12 Fish11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-June 09

Re: ImageIcon Netbeans IDE

Posted 22 June 2009 - 02:08 AM

I am quite sure the relative path and extension is correct. I also tried the solution suggested by cfoley but got some error using the ImageIO.read(file) statement.
I am at work atm but will post some more details (code and error) later on. I am sure I made some stupid mistake :(
Was This Post Helpful? 0
  • +
  • -

#13 Fish11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-June 09

Re: ImageIcon Netbeans IDE

Posted 22 June 2009 - 02:17 AM

View Postcfoley, on 22 Jun, 2009 - 12:18 AM, said:

Knowing where the current directory is in Java can be a wee bit tricky. Your file is probably close but not in the right place. Let's do some tests:

		File file = new File("pictures/test.png");
		System.out.println("File exists: " + file.exists());
		System.out.println(file.getAbsolutePath());



This will tell you if Java is finding the file and where it is looking for it. You should be able to work out where to move it to or how to alter your code from there.


I will try this when I am at home.
Thanks for all the efforts (not only cfoley) but pls don't shoot me when it is some utterly stupid mistake I made.
Was This Post Helpful? 0
  • +
  • -

#14 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: ImageIcon Netbeans IDE

Posted 22 June 2009 - 03:23 AM

No problem. The hardest to find mistakes are always the most stupid. ;)
Was This Post Helpful? 0
  • +
  • -

#15 Fish11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-June 09

Re: ImageIcon Netbeans IDE

Posted 22 June 2009 - 03:24 PM

I figured out that I used the wrong relative path (file.exists() and file.getAbsolutePath()). The pictures-folder was placed in the same folder where the .java-files are stored (which I thought was the correct folder), now I moved it to the correct NetBeansProjects-folder and it works. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2