0

I am working on a project with GridLayout. Currently, I am having trouble replacing a JPanel with GridLayout with another JPanel with GridLayout in my JFrame. Here is the code.

    public JFrame frame = new JFrame();
    public JPanel pan = new JPanel();

    public void replace() {
      frame.remove(pan);
      pan = new JPanel();
      GridLayout grid = new GridLayout(8,8);
      pan.setLayout(grid);
      frame.add(pan);
      frame.invalidate();
      frame.revalidate();
      frame.repaint();
    }

Much help would be appreciated. Thanks

  • For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Jan 17 '14 at 23:28

1 Answers1

0

Does this help?

public JFrame frame = new JFrame();
public JPanel pan = new JPanel();

public void replace() {
  frame.remove(pan);
  pan = new JPanel();
  GridLayout grid = new GridLayout(8,8);
  pan.setLayout(grid);
  pan.setVisible(true);
  frame.add(pan);
  frame.invalidate();
  frame.revalidate();
  frame.repaint();
  frame.pack();
  frame.setVisible(true);
}

If not, please, explain what is exactly working wrong for you.

aljipa
  • 716
  • 4
  • 6