Problem: ImageIcon and JPanel

Hi,

I'll try to explain what I want. I have a JDialog with a JList, a JPanel to show an imageicon and another JPanel with buttons. The list is filled with strings and each string has its own image to display on the JPanel. But when I tried to create the JDialog(in execution time), it fails because the imageicon is null. If I debug the app, I can see that the imageicon has the right height, width and the location. Can somebody explain me why?

Here it's the code:

publicclass CambioCampoextends JDialogimplements ActionListener, ListSelectionListener{

private JScrollPane list;

private JPanel buttons, previs;

private JList list_fields;

private ArrayList<String> name_fields =new ArrayList<String>();

private ArrayList<String> path_fields =new ArrayList<String>();

private JButton add accept, cancel;

private ImageIcon thumb;

public CambioCampo(JFrame dad, String title,boolean modal)

{

super(dad, title, modal);

this.setSize(400,300);

setResizable(false);

this.setLocationRelativeTo(dad);

crearCambioCampo();

}

privatevoid crearCambioCampo(){

Container cambiocampo = this.getContentPane();

cambiocampo.setLayout(new BorderLayout());

previs =new JPanel();

previs.setSize(270, 150);

String fields = getFile("images/campos.txt");

String delim ="_";

StringTokenizer separador =new StringTokenizer(fields,delim);

construyeArrayList(separador);

list_fields =new JList(name_fields.toArray());

list_fields.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

list_fields.setSelectedIndex(0);

list_fields.addListSelectionListener(this);

list =new JScrollPane(list_fields);

list.setSize(100, 150);

cambiocampo.add("West", list);

thumb =new ImageIcon("images/mes/classict.gif");

//when the next line is executed, it is printed the right width and height of the imageicon

System.out.println("Width: "+thumb.getIconWidth()+" Height: "+thumb.getIconHeight());

//the next line is where the nullpointerexception is throwed. It says thumb is null and can't paint it

thumb.paintIcon(previs,previs.getGraphics(), 0, 0);

cambiocampo.add("East", previs);

And here i have code that works and is not related with the thumb

By the way, the path is correct, the gif image is in its right place and I work with eclipse ide.

Thanks in advance,

Carlos

[3739 byte] By [paxtora] at [2007-11-26 14:58:41]
# 1
Why are you doing this:thumb.paintIcon(previs,previs.getGraphics(), 0, 0);Just add the icon to the panel and let it paint itself.
zadoka at 2007-7-8 8:47:28 > top of Java-index,Desktop,Core GUI APIs...
# 2
Add a JLabel with the icon to your JPanel.
Rodney_McKaya at 2007-7-8 8:47:28 > top of Java-index,Desktop,Core GUI APIs...
# 3
Add the icon to a JLabel and add the label to the panel.
camickra at 2007-7-8 8:47:28 > top of Java-index,Desktop,Core GUI APIs...
# 4
Well, my teacher told me to do that. I used that to paint a background on another JPanel(not in this class) and it worked. How do you want to add the icon to the panel?
paxtora at 2007-7-8 8:47:28 > top of Java-index,Desktop,Core GUI APIs...
# 5
JLabel label = new JLabel(thumb);previs.add(label);
Rodney_McKaya at 2007-7-8 8:47:28 > top of Java-index,Desktop,Core GUI APIs...
# 6

> I used that to paint a background on another JPanel(not in this class) and it worked

If you override the paintComponent() method of some component, then yes it will work, but thats not what you are doing here.

> How do you want to add the icon to the panel?

Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/label.html]How to Use Labels[/url] for working examples.

The tutorial also has a section that explains how painting is done.

camickra at 2007-7-8 8:47:28 > top of Java-index,Desktop,Core GUI APIs...
# 7

I created a Jlabel with an imageicon(I make a casting to Icon) but I have the same problem. Now I don't have a nullpointerexception but nothing is displayed. Here's the code I typed:

ImageIcon prueba = new ImageIcon("images/mes/classict.gif");

thumb = new JLabel((Icon)prueba);

previs.add(thumb);

cambiocampo.add("East", previs);

> If you override the paintComponent() method of some component, then yes it will work, but thats not what you are doing here.

Ok, I understand. As you said, the other case I told before is a subclass of JPanel and the paintcomponent method is overridden.

I'm pretty sure that it is something wrong with the imageicon but I don't know what.

Message was edited by:

paxtor

paxtora at 2007-7-8 8:47:28 > top of Java-index,Desktop,Core GUI APIs...
# 8
That cast is unnecessary.Anyway it should work. Why don't you create a simple example program that just has a panel with an icon inside a frame. (Like 15 lines of code). Then try to get that to work. It will narrow down your problem.
zadoka at 2007-7-8 8:47:28 > top of Java-index,Desktop,Core GUI APIs...
# 9

Well, I debug a simple example program and I saw the following:

accessibleContextnull

description"images/mes/classict.gif"

filename"images/mes/classict.gif"

count23

hash-1369040797

offset0

valuechar[23] (id=73)

height150

imageToolkitImage (id=70)

accelerationPriority0.5

availinfo7

height150

imagerepImageRepresentation (id=75)

propertiesHashtable<K,V> (id=77)

sourceFileImageSource (id=79)

src FileImageSource (id=79)

width270

imageObservernull

loadStatus8

locationnull

width270

The location and the accesiblecontext are null. Maybe that's the problem?

paxtora at 2007-7-8 8:47:28 > top of Java-index,Desktop,Core GUI APIs...
# 10
This means that it can't find your icon. Depending on how your project is structured, you'll have to play with different slashes to make it work.
kirillga at 2007-7-8 8:47:28 > top of Java-index,Desktop,Core GUI APIs...
# 11
But the width and the height are correct. And if I print the path of the imageicon, that is the right path.
paxtora at 2007-7-8 8:47:28 > top of Java-index,Desktop,Core GUI APIs...
# 12

> But the width and the height are correct. And if I print the path of the imageicon, that is the right path.

There is nothing tricky about this. The entire program should be:

JFrame frame = new JFrame();

JLabel label = new JLabel( new ImageIcon(...) );

frame.getContentPane().add(label);

frame.setSize(300, 300);

frame.setVisible(true);

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-8 8:47:28 > top of Java-index,Desktop,Core GUI APIs...