使用 SwingWorker 时从地图中检索数据 [英] Retrieving data from a map when using a SwingWorker
问题描述
我有一个创建 GUI 的类,并且有一个带有 ItemListener 的 JComboBox
.在 ItemEvent.Selected
事件中,我创建了工作线程并尝试将数据读入地图,但内容始终为空.
我的猜测是,这是由于线程在线程完成之前执行并离开 ItemListener
而发生的.
我已经添加了我的代码的相关部分,希望你们能帮助我确定问题所在.
private void updateUI(String text) {StringUtilities.invokeLater(new Runnable() {@覆盖公共无效运行(){text.append(text + "\n");}}}私有类 ComboBoxListener 扩展了 ItemListener {@覆盖公共无效 itemStateChange(ItemEvent e) {if(e.getStateChange() == ItemEvent.DESELECTED) { return;}if(e.getStateChange() == ItemEvent.SELECTED) {//做东西映射<字符串,字符串>map = new HashMap();新的 MyWorker(var1, var2) {@覆盖公共更新UI(字符串文本){更新主界面(文本);}@覆盖公共更新映射(字符串名称,字符串值){map.put(name, value);}}.执行();for(Map.Entry entry : map.entrySet()) {//对地图内容做一些事情}}}}
updateUI
和 updateMap
是 MyWorker
中的抽象方法,我覆盖了它们,以便我可以向 gui 发送更新.>
简而言之,我怎样才能从工作人员那里取回数据,以便在事件完成之前执行它?
我是凭记忆写的,因为我目前没有实际的代码,所以请忽略轻微的拼写错误和语法问题.
A SwingWorker
在另一个线程上异步执行它的 doInBackground()
方法;结果将在未来不确定的时间可用,但您正在立即检查地图条目.相反,publish()
每个 Map.Entry
在 doInBackground()
和 process() 中可用
可用的值.
如果中间结果可以有用地显示在另一个组件中,请更新该组件的模型,如文本所示此处JLabel
.如果没有,覆盖 done()
以使用完成的结果,如此处所示代码>图标代码>.
I have a class that creates a GUI and has a JComboBox
with an ItemListener. On an ItemEvent.Selected
event I create the worker and try to read data into a map, but the contents is always empty.
My guess is that this is happening due to the thread executing and leaving the ItemListener
before the thread completes.
I've added relevant pieces of my code and hoping you guys can help me determine where the issue is.
private void updateUI(String text) {
StringUtilities.invokeLater(new Runnable() {
@Override
public void run() {
text.append(text + "\n");
}
}
}
private class ComboBoxListener extends ItemListener {
@Override
public void itemStateChange(ItemEvent e) {
if(e.getStateChange() == ItemEvent.DESELECTED) { return; }
if(e.getStateChange() == ItemEvent.SELECTED) {
//do stuff
Map<String, String> map = new HashMap<String, String>();
new MyWorker(var1, var2) {
@Override
public updateUI(String text) {
updateMainUI(text);
}
@Override
public updateMap(String name, String value) {
map.put(name, value);
}
}.execute();
for(Map.Entry<String, String> entry : map.entrySet()) {
// do something with map contents
}
}
}
}
The updateUI
and updateMap
are abstract methods in MyWorker
that I'm overriding so that I can send updates to the gui.
In short, how can I get the data back from the worker so that I can execute it before the event is done?
I wrote most of this from memory since I don't have the actual code in front of me at the moment, so please ignore minor typos and syntax issues.
A SwingWorker
executes its doInBackground()
method asynchronously on another thread; the results will become available at indeterminate times in the future, but you're examining the map entries immediately. Instead, publish()
each Map.Entry<String, String>
as it becomes available in doInBackground()
and process()
the values as they become available.
If the intermediate results can be usefully displayed in another component, update that component's model, as shown here for the text of a JLabel
. If not, override done()
to use the completed result, as shown here for an Icon
.
这篇关于使用 SwingWorker 时从地图中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!