How do I change Panels in a container during runtime?

Hi,

I'm trying to change a panel in a container to show different panel with different components when a user selects an item in a combobox. Ive tried this (below) but the components just seem to disappear when I select an item in the combobox. Can some1 help? Thanks...

cbBusinessCode.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

centerPanel.removeAll();

getContentPane().remove(centerPanel);

if (cbBusinessCode.getSelectedItem() == "Health")

{

centerPanel.add(healthPanel);

getContentPane().add(centerPanel, BorderLayout.CENTER);

}

else

{

centerPanel.add(nonHealthPanel);

getContentPane().add(centerPanel, BorderLayout.CENTER);

}

}

});

[804 byte] By [glow360a] at [2007-11-27 6:33:04]
# 1

When you make changes that may affect a containers layout such as adding or removing child components you must tell the parent to do a new layout of its children. The two easy ways to do this are with the JComponent method revalidate and with the Container method validate.

centerPanel.add(healthPanel);

centerPanel.revalidate();

If you must remove and add centerPanel each time you may have to do

getContentPane().add(centerPanel, BorderLayout.CENTER);

getContentPane().validate();

For the combination you might get by with

centerPanel.add(healthPanel);

getContentPane().add(centerPanel, BorderLayout.CENTER);

centerPanel.revalidate();

Sometimes you may need to add a call to repaint on a container after validation.

You have to experiment to find the minimum that does what you want.

CardLayout and JTabbedPane can be elegant alternatives.

Comparing Strings is better done with the equals method

cbBusinessCode.getSelectedItem().equals("Health"))

crwooda at 2007-7-12 17:58:51 > top of Java-index,Desktop,Core GUI APIs...
# 2
hi,the reply is correct. but i guess you need to look at [url= http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html]How to Use CardLayout[/url] and this is your basic requirement.regardsAniruddha
Aniruddha-Herea at 2007-7-12 17:58:51 > top of Java-index,Desktop,Core GUI APIs...
# 3

Hi,

CardLayout is the better option here.

I am sending a sample program with the comments.

See the code... It is very easy to use....

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class CardDemo extends JFrame implements ActionListener

{

private JButton next,previous;

private JPanel p1,p2,mainPanel;

JLabel l1,l2;

CardLayout cl;

public CardDemo()

{

//These are the two different panels with controls

p1=new JPanel();

p2=new JPanel();

//The mainPanel which takes care about the cardlayout

mainPanel=new JPanel();

next=new JButton("Next");

previous=new JButton("Previous");

l1=new JLabel("Control for the first panel");

l2=new JLabel("Control for the second panel");

//Adding controls to the first panel

p1.add(l1);

p1.add(next);

//Adding controls to the second panel

p2.add(l2);

p2.add(previous);

cl=new CardLayout();

mainPanel.setLayout(cl);

//Adding the cards to the mainPanel with the names

mainPanel.add("first",p1);

mainPanel.add("second",p2);

//Adding mainPanel to the ContentPane of the Frame

add(mainPanel);

next.addActionListener(this);

previous.addActionListener(this);

setVisible(true);

setTitle("CardLayout Demo - First Card");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pack();

}

public void actionPerformed(ActionEvent ae)

{

//If the call is coming from the next button... Showing the next card

if(ae.getSource()==next)

{

cl.next(mainPanel);

setTitle("CardLayout Demo - Second Card");

}

//If the call is coming from the previous button... Showing the previous card

else if(ae.getSource()==previous)

{

cl.previous(mainPanel);

setTitle("CardLayout Demo - First Card");

}

}

public static void main(String[] args)

{

new CardDemo();

}

}

If you have any queries please let me know...

Thanks & Regards,

Santhosh Reddy Mandadi

santhosh-mcaa at 2007-7-12 17:58:51 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks for the suggestion guys...I'll be trying out wat you suggest now..
glow360a at 2007-7-12 17:58:51 > top of Java-index,Desktop,Core GUI APIs...
# 5
CardLayout works just perfect! Thanks again guys
glow360a at 2007-7-12 17:58:51 > top of Java-index,Desktop,Core GUI APIs...