2011-02-09 69 views
0

我有一個Jtree節點代表圖像,用戶可以調用和查看。有時需要將圖像加載四到五秒鐘。理想情況下,我希望在用戶正在等待時顯示等待光標,並選擇樹中選定的節點。然而,我發現用戶點擊節點,沒有任何事情發生,然後出現圖像,然後選擇節點(等待光標從不出現或更可能出現非常短暫,然後立即消失,我試圖重新繪製樹和applet,試圖強制行爲按我想要的順序發生。到目前爲止,我還沒有碰到任何問題。問題:鞦韆更新拼圖

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)); 

+1

有沒有可能在加載圖像時阻塞主Swing線程?圖像加載代碼是否在組件的paint方法中? – Pace 2011-02-09 04:59:55

回答

4

我覺得吳佩慈就是對資金使用後臺線程,如SwingWorker的,你的問題將得到解決,請有關EDT和線程p更多詳情請查看Concurrency in Swing。 roblems。例如,

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(); 
+0

再一次,我很驚訝我仍然需要學習多少Java。這爲我打開了一個全新的視野。謝謝。 – Elliott 2011-02-09 13:55:36