0

I would replace the same panel several times , but do not know how to do. I created a class " Grafico " which has a constructor that initializes with more parameters that I calculate in my code . For simplicity I will omit these parameters . "box " is a JComboBox that depending on the selected item is activated and creates these panels For example:

JPanel middle = new JPanel(new BorderLayout());
Grafico graph1 = new Grafico(.......);
JPanel conf1 = new JPanel();

middle.add(graph1, BorderLayout.CENTER);
middle.add(conf1, BorderLayou.EAST);
frame.getContentPane().add(middle);

box.addItemListener(new ItemListener() {

                    @Override
                    public void itemStateChanged(ItemEvent e) {

.........//I do something..I create a new object of type "Grafico" with new parameters........................

Grafico graph2 = new Grafico(.......);
middle.remove(graph1);

                            middle.add(graph2, BorderLayout.CENTER);

                            frame.getContentPane().add(middle);
                            frame.getContentPane().revalidate();
                            frame.getContentPane().repaint();


}
});

And this works , but the second time I click on an object in the JComboBox I wish it would update me graph2 every time, without creating a new one every click , but it does not!

1 Answers1

2

You have to call repaint() and revalidate() in order to refresh the Panel.

Java Swing revalidate() vs repaint()

Community
  • 1
  • 1
Simone C.
  • 369
  • 4
  • 14