我正在使用Netbeans IDE構建一個帶有Java Swing的小型測試工具。使用setText在jLabel上設置文本被延遲
我想更新一個標籤,這是某種程度上沒有得到「重新粉刷」/「刷新」。我研究了幾個類似的問題,但無法解決我的問題。
private void excelFileChooserActionPerformed(java.awt.event.ActionEvent evt)
{
if(!JFileChooser.CANCEL_SELECTION.equals(evt.getActionCommand()))
{
String selectedFile = excelFileChooser.getSelectedFile().getAbsolutePath();
loaderLabel.setText("Please Wait..");
try {
//This is sort of a blocking call, i.e. DB calls will be made (in the same thread. It takes about 2-3 seconds)
processFile(selectedFile);
loaderLabel.setText("Done..");
missingTransactionsPanel.setVisible(true);
}
catch(Exception e) {
System.out.println(e.getMessage());
loaderLabel.setText("Failed..");
}
}
}
loaderLabel
是JLabel
和所使用的佈局是AbsoluteLayout
。
因此,我的問題是「請稍候...」從不顯示。儘管調用方法processFile
需要大約2-3秒,但是「請稍候...」從不顯示。但是,「完成...」/「失敗...」顯示。
如果我添加了調用processFile
前popup
(JOptionPane
),「請等待。」所示。我無法清楚地理解爲什麼會發生這種情況。
在沉重的方法調用之前,我應該遵循「良好做法」嗎?我是否需要調用顯式重繪/刷新/重新驗證?
同意。不要阻塞EDT(Event Dispatch Thread) - 當發生這種情況時,GUI將「凍結」。另請參閱:爲長時間運行的任務實施「SwingWorker」。有關更多詳細信息,請參見[Swing中的併發](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)。 –