Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More Info on Panels: add/remove etc.

Status
Not open for further replies.

KevinWeav

Programmer
Dec 9, 2001
8
0
0
GB
Perhaps I wasn't really clear in my last posting yesterday.

I am not using Swing packages yet (stuck with Microsoft J++) and so the changePanel.updateUI(); suggestion is not an option, surely there must be a way without using Swing.

All I want to do is remove a control and then add another to a panel (see case 2 below). However, all that happens is that the original control first disappears with the second control not appearing. (When case 1 runs however the first control reappears???) What am I doing wrong and/or how else can I solve this problem when I want to change controls in a panel? (This is a simplified version of my actual problem of switching panels in the center of a BorderLayout).

Code:

changePanel.add(one, "Center");
...
switch(number) {
case 1:
changePanel.remove(two);
changePanel.add(one);
break;

case 2:
changePanel.remove(one);
changePanel.add(two);
break;
}
...
Many thanks in advance (again)

Kevin
 
If you add to a BorderLayout panel using add with no location parameter, it gets added to the "end" of the panel. On a BorderLayout, I'm not sure where it would interpret that to be.

Have you tried changePanel.add( two, "Center" ) in case 2?
 
Thanks for the suggestion, but on the code fragment illustrated I am not using a BorderLayout, that's just in my real applet. Any other ideas?
 
I wrote a small app using jdk1.2.2 that does what you're trying to do without a problem, but I wasn't using a layout manager. As soon as I added a layout manager to the panel (I tried FlowLayout, GridLayout and BorderLayout), then it wouldn't work.

So I think the layout manager is to blame.

Have you considered adding all the components to the panel at the start and just making the unwanted ones invisible with setVisible(false) and setEnabled(false)?
 
Seems so easy in theory doesn't it?
Unfortunately each panel I want to swap has a lot of stuff on it, so making components invisible is not really an option. Thanks for trying anyway.

Kevin
 
I've never done this, but if the components you want to swap have to be in the same place on the panel, you could put a CardLayout panel in that place and put the buttons on it. Then you could use the CardLayout show() method to display the button you want.

Anyway, good luck.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top