JPanel remove does not work

Hi!

I'm creating an interface which content is created dynamically. I have a JInternalFrame divided in two by a jsplitpane, in which on the left there is a Jtree and on the right I have a JPanel which content i need to be dynamically created depending on the JTree node selected.

I have tried with remove, revalidate/validate, add, validate, repaint but it seems not to work: the old button still remain and the others are painted over the old ones.

I didn't try the CardLayout, as someone suggested in this forum, because I have too many "cards" to create, and it will cost too much ram!

I post here the portion of the code where I try to create the new content:

EventQueue.invokeLater(new Runnable(){

publicvoid run(){

/*for (int i=nodePane.getComponentCount()-1; i>=0; i--)

nodePane.remove(i);*/

nodePane.removeAll();

nodePane.revalidate();

nodePane.setLayout(new SpringLayout());

//nodePane.add(title);

nodePane.add(new JButton(currNode.toString()));

SpringUtilities.makeCompactGrid(nodePane,

2 , 1,//rows, cols

6, 6,//initX, initY

6, 6);//xPad, yPad

nodePane.repaint();

nodePane.revalidate();

}

});

Help me please!!!

[1690 byte] By [Shadow75a] at [2007-10-3 4:16:39]
# 1

Should work. The key is to revalidate() after all the changes are done (ie. no need to revalidate() after removing the components). Sometimes repaint() is needed as well. I usually do revalidate() then repaint(), but thats the only difference I see.

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] 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-14 22:18:12 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for your help! I moved the call to revalidate at the end, after removing, adding all the components I need and calling repaint, it seems to be the correct way because now it works.And thanks for the suggestions on how post messages, you got reason!
Shadow75a at 2007-7-14 22:18:12 > top of Java-index,Desktop,Core GUI APIs...