Java GUI在SwingWorker线程中冻结长时间运行的任务
我有一个长时间运行的任务,当用户单击GUI应用程序中的按钮时,winch会执行:Java GUI在SwingWorker线程中冻结长时间运行的任务,java,swing,freeze,swingworker,unresponsive-progressbar,Java,Swing,Freeze,Swingworker,Unresponsive Progressbar,我有一个长时间运行的任务,当用户单击GUI应用程序中的按钮时,winch会执行: simplfyButton.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { simplfyAllText(); }//mouse evebt }); Simple
simplfyButton.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
simplfyAllText();
}//mouse evebt
});
SimpleFyallText()方法执行一些GUI调用,然后执行一个长时间运行的任务,因此我将其放置为SwingWorker:
private void simplfyAllText() {
/*
* User has imported a text document for simplfying Depending on the size of the
* document this could be time consuming So we will perform this on a
* SwingWorker thread
*/
// If user selects 'Simplfy' before any import is done
if (!clearTextArea) {
message_textArea.setText("");
clearTextArea = true;
displayMessage("You must import your text doucument to simplfy.", "Notice!");
return;
} else if (message_textArea.getText().length() <= 20) {
displayMessage("You must import your text doucument to simplfy.", "Notice!");
return;
}
// Set up stuff outside the worker thread
exchangedText.setText("");
progressBar.setIndeterminate(false);
progressBar.setStringPainted(true);
progressBar.setVisible(true);
message_textArea.setEditable(false);
// Create our worker to all heavy tasks off the event dispatch thread
SwingWorker<String, Integer> sw = new SwingWorker<String, Integer>() {
String simplifiedText = "";
@Override
protected String doInBackground() throws Exception {
// Simplfy the text form message TA
int size = message_textArea.getText().length();
for (int i = 0; i < size; i++) {
publish(i);
}
simplifiedText = textSimplfier.simplyFullText(message_textArea.getText());
return "finished";
}
@Override
protected void process(List<Integer> chunks) {
// define what the event dispatch thread
// will do with the intermediate results received
// while the thread is executing
for (int val : chunks) {
progressBar.setValue(val);
countButton.setText(Integer.toString(val));
System.out.println("PROCESS : " + val);
}
}
@Override
protected void done() {
// this method is called when the background
// thread finishes execution
outPut_TextArea.setText(simplifiedText);
String exchanged = textSimplfier.getSwappedWords().toString();
exchangedText.setText(exchanged);
// remove 1/3 for : between words
String wordsWithoutHTML = "";
try {
wordsWithoutHTML = exchangedText.getDocument().getText(0, exchangedText.getDocument().getLength());
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int noExchanged = ((wordsWithoutHTML.length() / 3) * 2);
int noOfWords = message_textArea.getText().length();
keyWord_TextField.setText(
"Complete!. " + noExchanged + "/" + noOfWords + " simplfied. Click /'Clear/' to continue.");
}
};
sw.run();
}
private void simplefyalltext(){
/*
*用户已导入一个文本文档,以便根据文档的大小进行简化
*记录这可能会很耗时,因此我们将在
*摇臂螺纹
*/
//如果用户在完成任何导入之前选择“Simpfy”
如果(!clearTextArea){
message_textArea.setText(“”);
clearTextArea=true;
displayMessage(“您必须将文本文档导入simplfy。”,“注意!”);
返回;
}else if(message_textArea.getText().length())
您应该使用:
sw.execute();
在另一个线程上开始执行
有关更多信息和工作示例,请阅读上Swing教程的部分
您应该使用:
sw.execute();
在另一个线程上开始执行
有关更多信息和工作示例,请阅读上Swing教程的部分