How to reload a JPanel?
Asked Answered
Y

2

8

I'm a java newbie... I need to know how to reload a JPanel? I extended the JPanel class and created a panel that will run in a cardlayout in an Applet. I want this panel to reload/refresh after the user clicks a button in this panel. I tried including the revalidate() and repaint() methods (methods I don't understand well) in the ActionListener for the button but nothing happened. Can anyone throw some light on how to correctly refresh the whole panel?

Yogh answered 12/7, 2013 at 6:6 Comment(7)
please whats is reload, for better help sooner post an SSCCE, short, runnable, compilableTacitus
What do you mean by "reload" and "refresh" exactly? Show some code, explain with an example what you want to do.Shortcircuit
refresh/reload/update all the components in the panel... some of the components are displaying info that is being updated through jdbcYogh
you should just use the Graphics instance of the JPanel.Hydroplane
Let's take a JLabel for example, whose text is reloaded from the database. To make it display the new text, use label.setText(). For a JTable, use table.setModel(), or update the data in its model. For a JTextArea, setText(), etc.Shortcircuit
@Manish Doshi s/he want to setText based on value from JDBC, then Graphics haven't something withTacitus
@user2108393 : If you are looking for something like, an event that can clear all the fields and other components of their previous values, you can have a look at this example, hopefully this might be of some help.Pizarro
P
22

This works fine for me. But after an other

_panel.revalidate();
_panel.repaint();
Prud answered 12/7, 2013 at 10:52 Comment(1)
first removeAll() to remove all components _panel.removeAll(); _panel.revalidate(); _panel.repaint();Vinosity
A
0

You could also refresh the entire JFrame to update every component, and not just the panel and its components:

this.revalidate();

this being the instance of your JFrame, if your class extends JFrame, just use this.

Aliber answered 27/6 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.