Get current instance of Runnable

本小妞迷上赌 提交于 2019-12-13 20:05:45

问题


I'm making an application that allows users to view task lists stored in different databases. So what happens is that, I have a list of the names of browsable databases (stored as a text file). Program loads the first database in that list and displays contents. Then from a menu, I allow users to select another database in the list. (Kind of like, I want to view the tasks for Andy, and now Bob, and now Carl...).

Problem is, I don't know how to update the UI so that the contents of the new database is displayed. It's trivial to remove the tasks currently listed, read from the DB, and then repopulate. But I can't get to this code (from my Singleton database driver) because I would need access to the instance of my Runnable which was loaded to the EventQueue (all my JComponents are private, as with all tutorials I've so far encountered). I'm looking at the get/peek methods in EventQueue but they don't seem to be what I need since they return AWTEvents and I don't see any relation between AWTEvent and Runnable other than java.lang.Object.

So, is it possible to get the current Runnable? How?

Thanks in advance for any guidance.


回答1:


I suspect that you are going about this wrong. What you want to do is change databases from within a control class (using MVC nomenclature). For instance, say you load the available databases into a JList, then in the control for this component, say a ListSelectionListener, you execute a SwingWorker that loads the new database and then displays the results in the GUI either via its publish/process pair of methods or in the done method. The database code (the model) should know nothing about the view or GUI portion of your code (the Runnable as you call it).




回答2:


there is a good tutorial about Concurency in Swing and nobody can't tell about that, that is out-dated

you have two choises

1) Runnable#Thread, but all output to the GUI must be wrapped into invokeLater(), more details in the tutorial about Concurency in Swing

2) or by using SwingWorker



来源:https://stackoverflow.com/questions/8021809/get-current-instance-of-runnable

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