1

I need to reload the frame to the state in which it was earlier loaded that means to return all frame components to the state they were when first put in the frame. Below are the things which I tried which is mentioned in question How can I refresh or reload the JFrame?.

frame.invalidate();
frame.validate();
frame.repaint(); 

I also tried to remove all the components of frame using frame.removeAll() and reload it but it is also not working ?

Any help is highly appreciated. Thanks

Community
  • 1
  • 1
Naseem
  • 911
  • 5
  • 18
  • 48
  • Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). This is the type of thing that `CardLayout` is **made for**. – Andrew Thompson Jun 16 '16 at 12:03
  • BTW - is the intention to change the components that are in it, (as in, swap a panel with tree and text area for another with a table) or is it simply to restore the initial **state** of the components that appeared for the user (as in, close the nodes of the tree and clear the text from the text area). If the latter, it is best **not to** remove the components but instead change the state or model of the current components. Oh, & please be sure to *answer* my question. I (and others) cannot best tell how to approach this - without more detail. – Andrew Thompson Jun 16 '16 at 12:07
  • Hi Andrew, thanks for the solution. My intention is to simply restore the initial state of the component so that the user can add values again in the frame. Apologies for not responding earlier as I was trying Greenthor solution. I'll take a look at your Card Layout solution as well. – Naseem Jun 16 '16 at 12:16
  • 1
    *"My intention is to simply restore the initial state of the component .."* OK.. in that case, ignore all the advice so far about removing components ***or*** using a `CardLayout`. The best way to do it is to keep references to each component, and just reset the content or model (shown in the view) of each of them. – Andrew Thompson Jun 16 '16 at 13:13
  • BTW - I changed the title to *"How to refresh all components to their default or initial values?"* because that's what it seems the actual goal is. – Andrew Thompson Jun 16 '16 at 13:15
  • Ok....I am trying the same. – Naseem Jun 16 '16 at 13:15
  • @AndrewThompson, Thanks Andrew for providing the help, it worked for me. Can you post your answer as a separate comments so that I can accept it as an answer ? – Naseem Jun 17 '16 at 00:16

2 Answers2

2

How to refresh all components to their default or initial values?

The best way to do it is to keep references to each component, and just reset the content or model (shown in the view) of each of them.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

If I get you right, for example you have a text field, someone enters text in it and then you want to redo that, along with other components.

In this case there is no built-in solution. You have to do it manually.

I recommend the following behavior: When you built the frame the first time (creating the components, setting the layout, configure the action etc.), put that into a seperate method, e.g. void builtFrame(). Then you can have a method, for instance void revertFrame(), which first removes all components and then calls builtFrame(). If you do not inherit JFrame but rather just have a field, this even gets easier, as you can just have the method produce the initial-state frame with for example JFrame builtFrame() (instead of the void variant).

GreenThor
  • 456
  • 5
  • 14
  • Thanks for providing the solution. Let me try it. Will let you know. – Naseem Jun 16 '16 at 11:52
  • I did not removed the components from JFrame but instead I resetted the component values to their Original state. Thanks for the help. – Naseem Jun 17 '16 at 00:14