Senario: Swing components Two class files in one package simple object panel display on frame I am trying to create a generic panel to go into another panel, just change some values for display as well as to decide how to process the multiple, (not shown), textfields inputs. i.e., screen is the same, field counts are the same, but data is treated differently based on "str" value passed to mkSubPanel wich drives the processing.
QUESTION: Code works fine, until the second call to file2Obj.mkSubPanel(str). When the subPanel is displayed in the JFrame-panel, because everything is opaque(false), I can see the previous button and label objects text undeneath and it is very ugly. I know that the file2Obj is creating multiple 'new' items because I can count them on the frame after each call. The JFrame obj count just keeps increasing it's count, so the labels and the buttons overlay the previous instance, so the text gets whacky.
I tried a few ways to do this, but the correct procedure seems to be eluding me. I tried to panelObj.removeAll() on the subPanel before the next pass, but that didn't work. I tried to perform the new obj on the obj in the class declaration as statics, but that didn't work.
Must be something dumb.
PSUEDO-CODE (NOTE: Syntax invalid for simplification only. So don't pick on that.) **************************************************************************************** ========================================================== file1.java package x; class file1 extends JFrame implements.... { private file2 file2Obj;
public JPanel mkSubPanel(String strVal) { file2Panel = new JPanel(); aa = new JLabel(strVal); bb - new JButton(strval); // everything is set opaque(false), so i's hard to see file2Panel.add(aa); file2Panel.add(bb); return file2Panel; } }
Whenever you make a change in the layout of an already-visible container, you have to call validate() on it to get Java to redo the layout and fix things up.
Thank you for the info about the validate()! But part of my question still remains. Concerning the way the methods are called and the "new obj" in each call. This adds a new instance of the component to the getContentPane().getComponentCount() of the JFrame. So inevitably this will become a memory hog. If you could reread my original question, forget about the visible component problem, and help with the issue of not creating a new instance every time I want to reuse the same obj, I would really appreciate it. No tutorial, API spec or other board covers this very well. Right now it just keep overlaying a new panel on top of the old panel, etc, etc... I'm sure you can see the potential problems coming. Because the getComponentCount keeps increasing, I am concerned about the memory usage implications. Maybe it's me, but it makes sense that this should not be handled the way I'm doing it and making the count increase. The number of components should not increase, I don't think. Thanks again.
Ernest Friedman-Hill
author and iconoclast
Marshal