Quick Java Question -- How to replace a component in a frame/container

statik213

Golden Member
Oct 31, 2004
1,654
0
0
If I have a component (textbox, label etc.) that's placed in a container (JFrame, Jpanle whatever) and can to remove it and replace it with a new component in it's place leaving all the other components and layout constraints intact?
 

Bacardi151

Senior member
Dec 15, 2003
540
0
0
are you asking if a component can be removed or replaced with a new one? if so then yes.

if you want to remove it, then you just remove the button object in the code
if you want to replace it, then you keep the object but write a specific method for it...
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Well I know that you can do a contentpane.remove() and contentpane.add() but I want to add the new component exactly where the old one was.
In some layouts the orders in which componets are added matter, so I don't want to affect the layout of the rest of the components too much. So if I do a .remove() on something that was inserted first and do a .add() it'll end up at the bottom-right position instead of the top-left in a grid llayout for example. I want to be able to change the top-left cell's contents, but I want to know if there's a general method for any layout manager.

 

znaps

Senior member
Jan 15, 2004
414
0
0
I don't think any containers have a 'replaceComponent' type of method, so either:

- Use a layout manager where you can specify the location of components explicitly

Or

- If you absolutely must use GridLayout, or FlowLayout etc, Put each component in a panel, and add the panels at the start, then when you need to replace a component remove it from its respective panel and add the replacement component.
 

boran

Golden Member
Jun 17, 2001
1,526
0
76
I did this with gridbaglayout. put two components in same grid location, put one on visible, the other not, and when switching I simply switched visibility between the two. I think that that is a good solution to your problem.
 

Bacardi151

Senior member
Dec 15, 2003
540
0
0
Originally posted by: statik213
Well I know that you can do a contentpane.remove() and contentpane.add() but I want to add the new component exactly where the old one was.
In some layouts the orders in which componets are added matter, so I don't want to affect the layout of the rest of the components too much. So if I do a .remove() on something that was inserted first and do a .add() it'll end up at the bottom-right position instead of the top-left in a grid llayout for example. I want to be able to change the top-left cell's contents, but I want to know if there's a general method for any layout manager.


i wasn't talking about ...remove() or add()... i dont really see how they could have that. cuz if you dont want one you simply remove the code that represented the button, or u just add the code needed for the button.

there should not be any effect in regards to the order of how you lay out the buttons, unless you mean that it effects the looks of the GUI, in fact if you're just replacing a button then, like i said earlier, you just change the method for it.
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: Bacardi151
Originally posted by: statik213
Well I know that you can do a contentpane.remove() and contentpane.add() but I want to add the new component exactly where the old one was.
In some layouts the orders in which componets are added matter, so I don't want to affect the layout of the rest of the components too much. So if I do a .remove() on something that was inserted first and do a .add() it'll end up at the bottom-right position instead of the top-left in a grid llayout for example. I want to be able to change the top-left cell's contents, but I want to know if there's a general method for any layout manager.


i wasn't talking about ...remove() or add()... i dont really see how they could have that. cuz if you dont want one you simply remove the code that represented the button, or u just add the code needed for the button.

there should not be any effect in regards to the order of how you lay out the buttons, unless you mean that it effects the looks of the GUI, in fact if you're just replacing a button then, like i said earlier, you just change the method for it.

??
I'm talking about replacing specific components on the screen while the application is running... how can you change 'code' to accomplish this while the application is running?

In any case, the approach w/ using a panel as a placeholder is probably the best.
I guess for layout managers that do use contraints you could do a layoutManager.getConstraints(oldComponent) and use the same constraints object for the component you are adding.

Thanks guys, i think i've got this one take care of!
 

znaps

Senior member
Jan 15, 2004
414
0
0
The layoutManager.getConstraints(oldComponent) is an idea - let me know if that works. You'll probably need to call container.validate() after adding the new component.
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: znaps
The layoutManager.getConstraints(oldComponent) is an idea - let me know if that works. You'll probably need to call container.validate() after adding the new component.


Reusing the same constaints objects worked without a hitch, the code is below:

GridBagConstraints gdc = mainGridBag.getConstraints(pnlLimits);
this.remove(pnlLimits);
pnlLimits = new JPanel();
pnlLimits.setBackground(Color.BLUE);
this.add(pnlLimits, gdc);
this.validate();

It worked, at least with GridBag.....


 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |