2015-04-05 104 views
2

我正在用Java創建一個Transliterating工具。它幾乎完成。但是,當我輸入速度如此之快的GUI凍結。調用modelToView()方法凍結Java Swing應用程序

因此,我調試應用程序以查明它發生的地方。

對方法JTextField.modelToView(pos)的調用導致了這種情況,這進一步調用TextUI.modelToView(JTextComponent c, int pos)

public synchronized final void readLock() { 
    try { 
     while (currWriter != null) { 
      if (currWriter == Thread.currentThread()) { 
       // writer has full read access.... may try to acquire 
       // lock in notification 
       return; 
      } 
      wait(); 
     } 
     numReaders += 1; 
    } catch (InterruptedException e) { 
     throw new Error("Interrupted attempt to aquire read lock"); 
    } 
} 

如果當前線程不是寫線程其中要求wait()

它的出現,這種方法首先通過調用AbstractDocument::readLock()方法,其源代碼是獲取關於文本組件的文件鎖。

但是,我只是在空格鍵被按下時突變文檔,我相信這是在EDT中發生的。而且,Swing也會在EDT中改變它。另外,當調用DocumentListener.insertUpdate(DocumentEvent)時,我打電話給JTextField.modelToView(pos)

那麼,是什麼原因,應用程序凍結!

以下是截圖:

應用截圖 enter image description here

當申請凍結 enter image description here

如果我最小化和最大化窗口 enter image description here

+2

考慮提供一個[runnable示例](https://stackoverflow.com/help/mcve),它演示了您的問題。這不是代碼轉儲,而是您正在做的事情的一個例子,它突出了您遇到的問題。這會減少混淆和更好的反應 – MadProgrammer 2015-04-05 03:30:58

+1

好吧,讓我看看,如果我可以使用一小段代碼重現問題! – Akshat 2015-04-05 03:32:55

+2

「*我相信發生在EDT *」這可以通過打印布爾方法'EventQueue.isDispatchThread()' – 2015-04-05 04:07:06

回答

2

我打電話的JTextField .modelToView(POS)當調用DocumentListener.insertUpdate(DocumentEvent)時。

當DocumentEvent被觸發時,Document沒有被完全更新。將你的代碼包裝在SwingUtilities.invokeLater(...)的DocumentListener中。這應該會導致您的事件代碼在文檔完成自身更新後執行。

+0

好吧,讓我試試看。但是,我在文檔更新後的某處讀取了這些方法!這不是總是如此嗎? – Akshat 2015-04-05 04:40:16