我有一个Jtree,节点代表用户可以调用和查看的图像。有时它需要加载四到五秒的图像。理想情况下,我希望在用户等待时等待光标显示,并在树中选择要选择的节点。然而,我发现用户点击节点,似乎没有任何事情发生,然后图像出现,然后选择节点(等待光标永远不会出现或更可能出现非常短暂,然后立即消失。我'尝试重新绘制树和小程序,试图按照我想要的顺序强制行为发生。到目前为止,我没有运气。有什么建议吗? 以下是一些Swing代码的一部分,它给了我一些问题:
thisApplet.setCursor(new Cursor(Cursor.WAIT_CURSOR));
selectdocumentTreeLeaf(); // a JTree with nodes representing images
tree.repaint();
thisApplet.repaint();
tree.setEnabled(false); //disabled so users don't keep clicking on it.
result = createImage(queue.q[pointer].currentPage); //where the image is fetched
thisApplet.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
答案 0 :(得分:4)
我认为佩斯是有钱的。使用后台线程,如SwingWorker,您的问题将得到解决。有关EDT和线程问题的更多详细信息,请查看Concurrency in Swing。如,
thisApplet.setCursor(new Cursor(Cursor.WAIT_CURSOR));
selectdocumentTreeLeaf();
tree.repaint();
thisApplet.repaint();
tree.setEnabled(false);
new SwingWorker<Image, Void>(){
@Override
protected Image doInBackground() throws Exception {
return createImage(queue.q[pointer].currentPage);;
}
@Override
protected void done() {
try {
result = get();
thisApplet.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
tree.setEnabled(true);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}.execute();