GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Repeat class method call 'new obj' question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Repeat class method call Watch "Repeat class method call New topic
Author

Repeat class method call 'new obj' question

J Botac
Greenhorn

Joined: Nov 19, 2003
Posts: 3
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 void control()
{
yuck();
otherYuck();
}

public void yuck()
{
file2Obj = new file2();

}
otherYuck()
{
getContentPane().add(labelStuff,Borderlayout.PAGE_START);
getCOntentPane().add(mainPanel,BorderLayout.CENTER);
setJMenuBar(mkMenu()); // obvious...
}

public void actionPerformed(ActionEvent e)
{
// psuedoCode
gotOneMenuChoice(a);
getContentPane().add(file2Obj.mkSubPanel(a),BorderLayout.CENTER);
gotAnotherMenuChocie(b);
getContentPane().add(file2Obj.mkSubPanel(b),BorderLayout.CENTER);
show();
}
}
==========================================================
file2.java
package x;
class file2 extends JFrame impelements ...
{
private JLabel aa;
private JButton bb;
private JPanel file2Panel;
...blah blah blah...

..... some other stuff...

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;
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
J Botac
Greenhorn

Joined: Nov 19, 2003
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

Right, well, store a reference to a single copy in a member variable, and have the getXXX() method return that reference.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Repeat class method call 'new obj' question