I have a Text Area swing element where I want to display some execution logs. messages should appear after each execution step. eg "Fetching has been started". But the problem is that all logs messages appear only once after the button action performed event is completed.Below is the way I used to display some text
getXmlButton = new JButton("Fetch Reports");
getXmlButton.addActionListener((new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createDirectory();
stutusArea.append("Fetching has been started");
final String password = passwordTextField.getText();
final String username = loginTextField.getText();
OperatingSystemDriver os = new OperatingSystemDriver();
driver = os.getDriver();
stutusArea.append("getting some url");
driver.get(URL);
try {
LoginPage login = new LoginPage(driver);
login.loginAs(username, password);
stutusArea.append("successful login");
} catch (Exception e1) {
stutusArea.append("login error");
}
insertToDbButton.setEnabled(true);
}
}));
You have to use a different thread, otherwise your GUI blocks.
Please note that updating the GUI from an other thread is a bad idea, use SwingUtilities.invokeLater to avoid some strange errors/bugs.
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
stutusArea.append("Fetching has been started");
}
})
final String password = passwordTextField.getText();
final String username = loginTextField.getText();
// ...
}
}).start();
}
那是因为你在GUI线程中执行了所有ActionPerformed
东西,所以GUI冻结了,等待你的ActionPerformed
东西完成,一种避免这种情况的方法是使用Threads ,有一个例子: Thread Example
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.