且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在java中自动刷新gui屏幕,在存储文件中进行更新后?

更新时间:2023-12-03 16:00:28

一个很好的解决方案是将所有的GUI组件存储在ArrayList或其他类型的列表,您可能会喜欢。



您最初将读取文本文件,并将其相应的组件添加到列表中,然后将该列表加载到GUI中。当编辑文件时,您可以再次循环,如果组件不存在于列表中,则可以将其添加到其中。如果列表中的任何组件与文件不对应,可以从列表中删除它们。之后,您可以使用列表中的组件重新加载GUI。


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.

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.