简体   繁体   中英

How I can update a panel with the same panel?

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!

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

Java Swing revalidate() vs repaint()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM