How to auto refresh gui screen in java, after update taking place in a stored file?

假如想象 提交于 2019-12-24 13:37:48

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/22479566/how-to-auto-refresh-gui-screen-in-java-after-update-taking-place-in-a-stored-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!