The Artima Developer Community
Sponsored Link

Java Answers Forum
about JFrame.setVisible

2 replies on 1 page. Most recent reply: Dec 12, 2003 1:06 PM by s

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
aj3423 aj

Posts: 16
Nickname: aj3423
Registered: Aug, 2003

about JFrame.setVisible Posted: Dec 11, 2003 7:08 PM
Reply to this message Reply
Advertisement
i met a problem that while i add a JPanel into another and set it invisible,it will still appear abnormity.
i made an example to show it,compile and run the follow code then you will see what i mean..

//the code <Temp.java>


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class Temp {

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(new clicked());
b2.addActionListener(new clicked());
b3.addActionListener(new clicked());
b4.addActionListener(new clicked());

frame.setVisible(true);
frame.setResizable(true);
frame.pack();
}

class clicked implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b3) {
p1.setVisible(false);
frame.pack();
} else if(e.getSource() == b4) {
p1.setVisible(true);
frame.pack();
}
}
}


public static void main(String [] args) {
new Temp();
}
}


aj3423 aj

Posts: 16
Nickname: aj3423
Registered: Aug, 2003

Re: about JFrame.setVisible Posted: Dec 11, 2003 7:17 PM
Reply to this message Reply
i had set the "p1" invisible,but when it runs,"p1" will still appear at the left. does anyone know why ?? and how to resolve it ??
(after that,when i click the "b3" and "b4" to hide and show the "p1",it goes correctly.. )

s

Posts: 23
Nickname: codemonkey
Registered: Nov, 2003

Re: about JFrame.setVisible Posted: Dec 12, 2003 1:06 PM
Reply to this message Reply
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

Flat View: This topic has 2 replies on 1 page
Topic: little help please.  i am sure u could do it in secs. Previous Topic   Next Topic Topic: linking a class file into the main java file

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use