1

I have a java swing application which reads the values for its components from a .text file. I'm trying to update one of its value and store it in .text file and then need a refresh possibly in the GUI screen as well. Value is modified in the file but GUI could not detect the change without a restart. I have went through repaint(), re-validate() with the use of timers. Can anyone specify what i could do? Thanks in advance.

deepak
  • 25
  • 7
  • 1
    First, Swing will repaint automatically if you change the values of the components with, for example, `setText`. Second, forcing the GUI to repaint will not re-run your code that sets their value in the first place. So, first you need to [monitor your file for changes](http://stackoverflow.com/a/16251508/2071828), then you need to call the code that reads the file and sets your field values when that file changes. – Boris the Spider Mar 18 '14 at 12:40
  • did you tried update() method?? – Stunner Mar 18 '14 at 12:48
  • Hi @BoristheSpider, Thanks for your reply. My file modifies as soon as i update using my jbutton and im sure of it. how could i call the modified value which i updated currently in a textfield? – deepak Mar 18 '14 at 13:01
  • No, @Stunner, i'm new to java swing, could u explain how it works. thanks – deepak Mar 18 '14 at 13:03
  • just invoke update method with your jframe/window object like frame.update(); – Stunner Mar 18 '14 at 13:06
  • @Stunner it does the same job as repaint() or revalidate() do in my case., just flickering is avoided. – deepak Mar 18 '14 at 13:15

2 Answers2

0

You need to watch your file and fire an event if your file changes. This event must set the component values which will force the component to automatically refresh.

sheu
  • 284
  • 5
  • 13
0

A good solution to this would be to store all of your GUI components in an ArrayList or some other sort of List that you may prefer.

You would initially read the text file and add it's respective components into the list, and then you would load that list into your GUI. When the file is edited, you can loop through it again and if a component does not exists in the list you can add it in. If any components in the list don't correspond to the file you can remove them from the list. After that happens, you can reload the GUI with the components in the list.

BigBerger
  • 1,765
  • 4
  • 23
  • 43