gpt4 book ai didi

Java Swing : repaint component during long event

转载 作者:行者123 更新时间:2023-11-30 07:33:09 24 4
gpt4 key购买 nike

我正在使用 Java Swing 为应用程序呈现我的 GUI。我在 JButton 上设置了一个 ActionListener 来触发一系列测试。理想情况下,我想用实时测试信息和结果更新状态区域(当前是 JTextArea)。换句话说,程序会在测试运行和完成时逐行设置文本区域,如下所示:

Running Tests...     Test 1:  Check Something...         Success    Test 2:  Check Something else...         Success    ..    //More testing outputAll tests have completed successfully.  Yay.

The problem is that I can't get the status text to update while my actionPerformed event method is still running. This is is all fine and dandy when all of the tests complete quickly. However, some of my tests take an indefinite amount of time to complete (think "database connection timeout"). When this happens, users are stuck with whatever text was already in the status area and my application freezes until the tests complete or fail.

I've tried calling repaint on my JTextArea every time I update the status, but it seems that just adds it to the event queue (which won't get fired till after my actionPerformed event completes...doh). I've also tried calling revalidate and updateUI, and even tried calling wait() on the ActionListener class running my tests, but nothing has worked thus far. Here is the structure of my current code:

..

JTextArea statusOutput_textArea;

..

public void actionPerformed(ActionEvent e) {
setStatusText("Running Tests...");

//Run Tests
int currentTest = 1;

//Check something
appendStatusText(" Test " + currentTest++ + ": Checking Something...");
.. //Check Something Here
appendStatusText(" Success");

//Check something else
appendStatusText(" Test " + currentTest++ + ": Checking Something else...");
.. //Check Something Else Here
appendStatusText(" Success");

//Other tests
.. //Run other tests here

appendStatusText("\nAll tests have completed successfully. Yay.");
}//End of actionPerformed method

public void setStatusText (String statusText) {
statusOutput_textArea.setText(statusText);
statusOutput_textArea.repaint();
}//End of setStatusText method

public void appendStatusText (String statusTextToAppend) {
statusOutput_textArea.setText(statusOutput_textArea.getText() + "\n" + statusTextToAppend);
statusOutput_textArea.repaint();
}//End of appendStatusText method

任何帮助将不胜感激:)

更新

对于那些对解决方案的一般结构感兴趣的人,这里是:

public class RunTestsButtonActionListener implements ActionListener {
JTextArea statusOutput_textArea;
JButton testDatabaseSettings_JButton;

public RunTestsButtonActionListener(JTextArea statusOutput_textArea){
this.statusOutput_textArea = statusOutput_textArea;
}

public void actionPerformed(ActionEvent e) {
testDatabaseSettings_JButton = (JButton) e.getSource();

Thread tests = new Thread(){
public void run() {
//Disable button and add tooltip
testDatabaseSettings_JButton.setEnabled(false);
testDatabaseSettings_JButton.setToolTipText("Running Tests...");

//Run Tests
try {
statusOutput_textArea.setText("Running Tests...");
int currentTest = 1;

//Check something
statusOutput_textArea.append("\n Test " + currentTest++ + ": Checking Something...");
.. //Check Something Here
statusOutput_textArea.append("\n Success");

//Check something else
statusOutput_textArea.append("\n Test " + currentTest++ + ": Checking Something else...");
.. //Check Something Else Here
statusOutput_textArea.append("\n Success");

//Other tests
.. //Run other tests here

statusOutput_textArea.append("\n\nAll tests have completed successfully. Yay.");
} finally {
//Enable button and remove tooltip
testDatabaseSettings_JButton.setEnabled(false);
testDatabaseSettings_JButton.setToolTipText("");
}
}
};

tests.start();
}
}

最佳答案

您正在对“Swing 线程”进行检查,您最好在不同的 thread 中执行这些任务(如果发生某些事情/发生变化,它会通知 GUI)。

关于Java Swing : repaint component during long event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6062896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com