2012-04-27 67 views
1

我正在使用重繪來觸發SwingWorker。根據應用程序中的自動調整選項,該工作人員自動重新創建圖像以適應封閉的JPanel的寬度。暫停SwingWorker執行,直到組件停止調整大小

問題是導致內存不足異常。這是因爲圖像具有較大的分辨率,並且在窗口被拖動調整大小時工作人員正在被連續調用。

我試圖避免這種情況只有​​當isDone()爲真。但是我覺得有一個更好的方法來做到這一點。也許通過使用某種計時器?你有什麼建議?

+1

我懷疑我的建議是否有效。雖然你可以嘗試,但是你可以在你的'JPanel'中添加一個[HierarchyBoundsListener](http://docs.oracle.com/javase/7/docs/api/java/awt/event/HierarchyBoundsListener.html)你可以寫''ancestorResized(...)','swingWorkerObject.cancel(true); swingWorkerObject.execute();',這可能會給人一種重新啓動的感覺,不知道該解決方案有多好! ! – 2012-04-27 08:46:27

+0

+1,對於更多的注意,對於能夠幫助的人,如果我從未工作:( – 2012-04-27 09:20:42

回答

1

這是一個有點左的字段,但基本上我使用的是javax.swing.Timer。基本思想是隻在計時器啓動時執行一個動作。

private Timer timer; 

. 
. 
. 

timer = new Timer(250, new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     // Perform required action 
     // Start the swing worker ?? 

    } 
}); 
timer.setCoalesce(true); 
timer.setRepeats(false); 

. 
. 
. 

public void setBounds(int x, int y, int width, int height) { 

    // Force the time to start/reset 
    timer.restart(); 

    super.setBounds(x, y, width, height); 

}