s
Posts: 23
Nickname: codemonkey
Registered: Nov, 2003
|
|
Re: about JFrame.setVisible
|
Posted: Dec 12, 2003 1:06 PM
|
|
You have to set visibility on all components in a container The following is how i generally do it... this little trick is also great for disabling all components in a container (think setEnabled instead of setVisible)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Temp implements ActionListener{
JFrame frame = new JFrame("Temp");
Container cp = frame.getContentPane();
JPanel p = new JPanel();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
CheckboxGroup cbg = new CheckboxGroup();
Button b1 = new Button("b1");
Button b2 = new Button("b2");
Button b3 = new Button("b3");
Button b4 = new Button("b4");
public Temp() {
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
System.exit(0);
}
});
p1.setBackground(Color.BLACK);
p2.setBackground(Color.BLUE);
p1.add(b1);
p1.add(new Checkbox("a", cbg, true));
p1.add(new Checkbox("b", cbg, false));
p2.add(b2);
p2.add(p1);
p1.setVisible(false);
cp.add(p);
p.add(p2);
p.add(b3);
p.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
frame.setVisible(true);
frame.setResizable(true);
frame.pack();
}
private void setV(Container c,boolean v){
Component[] components = c.getComponents();
for(int y=0;y<components.length;y++){
components[y].setVisible(v);
if(components[y] instanceof Container){
setV((Container)components[y],v);
}
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b3) {
//p1.setVisible(false);
setV(p1,false);
frame.pack();
} else if(e.getSource() == b4) {
//p1.setVisible(true);
setV(p1,true);
frame.pack();
}
}
public static void main(String [] args) {
new Temp();
}
}
the magic of recursion ...ahhh
|
|