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?
-
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 ThompsonOct 2, 2011 at 17:49
-
are you add/remove JComponent on Runtime, or change Size, needed to re-layout...,– mKorbelOct 2, 2011 at 18:08
-
what exactly you are doing with JFrame. Kindly elobrate it. ?– Hemang RamiOct 2, 2011 at 18:24
-
1This question is completely unanswerable as written. Can you share the necessary detail?– Hovercraft Full Of EelsOct 2, 2011 at 19:11
-
2There's an example here that may get you started.– trashgodOct 2, 2011 at 20:05
5 Answers
Try
SwingUtilities.updateComponentTreeUI(frame);
If it still doesn't work then after completing the above step try
frame.invalidate();
frame.validate();
frame.repaint();
-
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. Oct 8, 2013 at 11:56
-
2reload 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 Jan 4, 2014 at 17:22
just use frame.setVisible(false); frame.setVisible(true); I've had this problem with JLabels with images, and this solved it
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 4, 2018 at 6:59
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);
}
}
}
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
-
10bad 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? Aug 23, 2014 at 9:26
-
1@kleopatra No he won't.– user6490462Dec 4, 2017 at 10:41