I am doing project using Java and in that I need to reload whole JFrame
after clicking particular button on that JFrame
. How to do this?
Asked
Active
Viewed 2e+01k times
28

Andrew Thompson
- 168,117
- 40
- 217
- 433

SL_User
- 1,934
- 5
- 24
- 45
-
5what do you mean by 'refresh'? Is it 'return all components to the state they were when first put in the frame', 'change the components', something else? – Andrew Thompson Oct 02 '11 at 17:49
-
are you add/remove JComponent on Runtime, or change Size, needed to re-layout..., – mKorbel Oct 02 '11 at 18:08
-
what exactly you are doing with JFrame. Kindly elobrate it. ? – Hemang Rami Oct 02 '11 at 18:24
-
1This question is completely unanswerable as written. Can you share the necessary detail? – Hovercraft Full Of Eels Oct 02 '11 at 19:11
-
2There's an example [here](http://stackoverflow.com/questions/5812002/removeall-not-removing-at-next-validate/5812981#5812981) that may get you started. – trashgod Oct 02 '11 at 20:05
5 Answers
50
Try
SwingUtilities.updateComponentTreeUI(frame);
If it still doesn't work then after completing the above step try
frame.invalidate();
frame.validate();
frame.repaint();

Mike Park
- 10,845
- 2
- 34
- 50

Alim Ul Gias
- 6,351
- 2
- 28
- 39
-
6-1 for updateComponentTreeUI: that is nearly **always** the wrong thingy to do (it's re-setting the ui-delegates) when the most probable goal is merely reloading the data. – kleopatra Oct 08 '13 at 11:56
-
2_reload frame_ isn't a technical term and whatever it's supposed to mean, updateComponentTree is **NOT** the answer. Actually, the question as-is is way too unspecific to be answerable, so voted to close – kleopatra Jan 04 '14 at 17:22
-
1Well, so *what* is the answer, then? The updateComponentTreeUI call does the job. – foo Dec 29 '22 at 00:15
4
just use frame.setVisible(false); frame.setVisible(true); I've had this problem with JLabels with images, and this solved it

trikib
- 49
- 1
2
Here's a short code that might help.
<yourJFrameName> main = new <yourJFrameName>();
main.setVisible(true);
this.dispose();
where...
main.setVisible(true);
will run the JFrame again.
this.dispose();
will terminate the running window.
-
The question is about reloading the frame (in situ), not showing the frame. The setVisible does not show the frame *again*, it just shows or hides the frame depending on the parameter. – TT. Nov 04 '18 at 06:59
-1
Try this code. I also faced the same problem, but some how I solved it.
public class KitchenUserInterface {
private JFrame frame;
private JPanel main_panel, northpanel , southpanel;
private JLabel label;
private JButton nextOrder;
private JList list;
private static KitchenUserInterface kitchenRunner ;
public void setList(String[] order){
kitchenRunner.frame.dispose();
kitchenRunner.frame.setVisible(false);
kitchenRunner= new KitchenUserInterface(order);
}
public KitchenUserInterface getInstance() {
if(kitchenRunner == null) {
synchronized(KitchenUserInterface.class) {
if(kitchenRunner == null) {
kitchenRunner = new KitchenUserInterface();
}
}
}
return this.kitchenRunner;
}
private KitchenUserInterface() {
frame = new JFrame("Lullaby's Kitchen");
main_panel = new JPanel();
main_panel.setLayout(new BorderLayout());
frame.setContentPane(main_panel);
northpanel = new JPanel();
northpanel.setLayout(new FlowLayout());
label = new JLabel("Kitchen");
northpanel.add(label);
main_panel.add(northpanel , BorderLayout.NORTH);
frame.setSize(500 , 500 );
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private KitchenUserInterface (String[] order){
this();
list = new JList<String>(order);
main_panel.add(list , BorderLayout.CENTER);
southpanel = new JPanel();
southpanel.setLayout(new FlowLayout());
nextOrder = new JButton("Next Order Set");
nextOrder.addActionListener(new OrderUpListener(list));
southpanel.add(nextOrder);
main_panel.add(southpanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
KitchenUserInterface dat = kitchenRunner.getInstance();
try{
Thread.sleep(1500);
System.out.println("Ready");
dat.setList(OrderArray.getInstance().getOrders());
}
catch(Exception event) {
System.out.println("Error sleep");
System.out.println(event);
}
}
}

mike
- 4,929
- 4
- 40
- 80

Autumn's Fall
- 225
- 1
- 4
- 11
-4
You should use this code
this.setVisible(false); //this will close frame i.e. NewJFrame
new NewJFrame().setVisible(true); // Now this will open NewJFrame for you again and will also get refreshed

Frakcool
- 10,915
- 9
- 50
- 89

Veloxigami
- 95
- 1
- 10
-
11bad question attract bad answers (that's why suggested to close the question - unsuccessfully ;-) *NO* - whatever the real problem in the question, creating a new frame is *NOT* the solution. You wouldn't tear down your house just because it needs some new paint, would you? – kleopatra Aug 23 '14 at 09:26
-
1