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;
}
}