Swap panes

Hi everybody, Im new here and I hope I can solve all my doubts ;).

About this,

The main Frame has a JPanel (panel) ,2 JButton (button1 & button2)

I have also in the same project tow classes more: Panel1 and Panel2 (they are JPanel).

What i want to do is the following:

- When I click in button1 the content of panel should be the content of Panel1

- When I click in button2 the content of panel should be the content of Panel2.

How can I do this? Thanks in advance

[519 byte] By [naskara] at [2007-11-26 13:37:13]
# 1
Try making the layout of the "panel" a CardLayout. Then put the Panel1 and Panel2 into that panel. Then use the methods of the CardLayout to set which JPanel (Panel1 or Panel2) is active.
doremifasollatidoa at 2007-7-7 22:23:50 > top of Java-index,Java Essentials,Java Programming...
# 2
http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html
es5f2000a at 2007-7-7 22:23:50 > top of Java-index,Java Essentials,Java Programming...
# 3
In the future, Swing related questions should be posted in the Swing forum.Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]Using a Card Layout[/url]
camickra at 2007-7-7 22:23:50 > top of Java-index,Java Essentials,Java Programming...
# 4

One way is to add an action listener to the buttons.

So it would look something down this line...

button1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Panel1 panel = new JPanel1(); // or whatever other constructor sig you have for Panel1

Frame.this.add(panel); // or whatever name of the class that main is in...which I assume extends JFrame?

}

});

youaresofakingwetoddeda at 2007-7-7 22:23:50 > top of Java-index,Java Essentials,Java Programming...
# 5

> One way is to add an action listener to the buttons.

>

> So it would look something down this line...

>

> > button1.addActionListener(new ActionListener() {

>public void actionPerformed(ActionEvent e) {

> Panel1 panel = new JPanel1(); // or whatever other

> constructor sig you have for Panel1

> Frame.this.add(panel); // or whatever name of the

> class that main is in...which I assume extends

> JFrame?

> }

> );

>

I proved that way but it doesnt work for me.

In the constructor of the main frame I do:

private Panel1 panel1;

private Panel2 panel2;

...

panel1=new Panel1();

panel2=new Panel2();

And the action listener for button1 is:

panel=panel1;

But it does not work...

I磍l take a glance to the card layout tutorial.

Sorry, I dont see the swing forum, my mistake ;).

Thanks ;)

naskara at 2007-7-7 22:23:50 > top of Java-index,Java Essentials,Java Programming...