Reputation: 183
I have a JPanel object say panel that holds a reference to a JPanel object. Suppose panel points to panel-1 and on some click (action), it should point to panel-2 and panel-2 must replace panel-1 on the JFrame object frame.
However, it doesn't update. I tried the following, but in vain:
frame.repaint();
panel.revalidate();
Upvotes: 0
Views: 55
Reputation: 4039
I think this code does what you try to do. It has a JPanel that holds either a green JPanel or a red JPanel and a Button to do the flip.
public class Test{
boolean isGreen; // flag that indicates the content
public Test(){
JFrame f = new JFrame ("Test"); // the Frame
f.setLayout (new BorderLayout());
JPanel p = new JPanel(new BorderLayout()); // the content Panel
f.add(p, BorderLayout.CENTER);
JPanel green = new JPanel(); // the green content
green.setBackground(Color.GREEN);
JPanel red = new JPanel(); // the red content
red.setBackground(Color.RED);
p.add(green); // init with green content
isGreen = true;
JButton b = new JButton ("flip"); // the flip button
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p.removeAll(); // remove all of old content
if(isGreen){
p.add(red); // set new red content
isGreen = false;
} else {
p.add(green); // set new green content
isGreen = true;
}
p.revalidate(); // invalidate content panel so component tree will be reconstructed
f.repaint(); // repaint the frame so the content change will be seen
}
});
f.add (b, BorderLayout.SOUTH);
f.pack();
f.setSize(250,330);
f.setVisible (true);
}
public static void main (String [] args){
new Test();
}
}
Upvotes: 2