在我的下面的代码中,我希望在应用程序加载数据时清除TextArea.我还添加了一个重绘()但仍然没有清除.我是否必须以不同方式通知它以强制重画?
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
textArea.setText("");
textArea.repaint();
String result = //call a REST API
textArea.setText(result);
}
});
Run Code Online (Sandbox Code Playgroud)
我认为你想做的是在另一个线程中调用其余的api.您可以使用SwingWorker旨在在另一个线程中运行大量任务而不会阻止gui的操作.这是一个完整的例子,我非常喜欢Swing Worker Example
例:
class Worker extends SwingWorker<Void, String> {
@Override
protected Void doInBackground() throws Exception {
//here you make heavy task this is running in another thread not in EDT
// call REST API here
return null;
}
@Override
protected void done() {
//this is executed in the EDT
//here you update your textArea with the result
}
}
Run Code Online (Sandbox Code Playgroud)
后doInBackground方法结束的done方法被执行.然后SwingWorker通知任何PropertyChangeListeners有关状态属性更改StateValue.DONE.因此,您可以在此处覆盖此方法,或使用propertyChangeListener实现来执行您想要的操作.
| 归档时间: |
|
| 查看次数: |
3567 次 |
| 最近记录: |