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

