• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Repeat class method call 'new obj' question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
J Botac
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, well, store a reference to a single copy in a member variable, and have the getXXX() method return that reference.
 
reply
    Bookmark Topic Watch Topic
  • New Topic