[英]SwingWorker process() doesn't update JTextArea in real time
I tried many solutions provided online, but still not work.我尝试了许多在线提供的解决方案,但仍然无效。 I am creating a JAR and hope the text could be update in JTextArea in real time.
我正在创建一个 JAR 并希望文本可以在 JTextArea 中实时更新。 But the text only append to textarea after finish the program.
但文本仅在程序完成后附加到 textarea 。
My UI:我的用户界面:
package Test;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import Test.SearchForWorker;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import java.awt.SystemColor;
import java.awt.Font;
public class TestTesr {
private JFrame frame;
private JTextField textField;
private JTextArea console;
private int input;
public int getInput() {
return input;
}
public void setInput(int input) {
this.input = input;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestTesr window = new TestTesr();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestTesr() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 537, 360);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 233, 338);
frame.getContentPane().add(panel);
panel.setLayout(null);
textField = new JTextField();
textField.setBounds(5, 6, 130, 26);
panel.add(textField);
textField.setColumns(10);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(233, 6, 298, 326);
frame.getContentPane().add(scrollPane);
console = new JTextArea();
console.setEditable(false);
scrollPane.setViewportView(console);
JButton btnNewButton = new JButton("Submit");
btnNewButton.setBounds(140, 5, 88, 29);
panel.add(btnNewButton);
JLabel show = new JLabel("");
show.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
show.setForeground(SystemColor.textHighlight);
show.setBounds(49, 115, 100, 53);
panel.add(show);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("Data Submitted");
new SearchForWorker("Data Submitted", console).execute();
;
String y = textField.getText();
int g = Integer.parseInt(y);
setInput(g);
for (int u = 1; u <= 10; u++) {
try {
Thread.sleep(3000);
} catch (Exception q) {
System.out.println("Error occur for program time break" + q.toString());
}
g++;
new SearchForWorker("Inout plus " + u + " is :" + g, console).execute();
;
}
}
});
}
}
SwingWorker: SwingWorker:
package Test;
import java.awt.Color;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
public class SearchForWorker extends SwingWorker<String, String> {
private final String display;
private final JTextArea messageTextArea;
TestTesr test = new TestTesr();
SearchForWorker(final String output, final JTextArea area) {
// initialize
this.display = output;
this.messageTextArea = area;
}
@Override
protected String doInBackground() throws InterruptedException {
System.out.println("Inside do in background for: "+display);
publish(display);
return display;
}
protected void done() {
}
@Override
protected void process(List<String> showText){
System.out.println("ShowText is: "+showText.toString());
messageTextArea.append(showText.toString());
messageTextArea.append("\n");
}
}
after more than 3 years it might be thoroughly useless but isn't it what you wanted one day: 3 年多之后,它可能完全没用,但这不是您有一天想要的:
SwingWorker that worked for me为我工作的 SwingWorker
import java.util.List;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
public class SearchForWorker extends SwingWorker<String, String> {
private int display;
private final JTextArea messageTextArea;
TestTesr test = new TestTesr();
SearchForWorker(final String output, final JTextArea area) {
// initialize
this.display = Integer.parseInt(output);
this.messageTextArea = area;
}
@Override
protected String doInBackground() throws InterruptedException {
System.out.println("Inside do in background for: " + display);
// publish(display);
int n = display;
for (int u = 1; u <= 10; u++) {
n++;
publish(String.valueOf(n));
test.panel.updateUI();
try {
Thread.sleep(100);
} catch (Exception q) {
System.out.println("Error occur for program time break" + q.toString());
}
}
return String.valueOf(n);
}
@Override
protected void process(List<String> showText) {
System.out.println("ShowText is: " + showText.toString());
messageTextArea.append("Inout plus " + showText + " is :" + showText.toString());
messageTextArea.append("\n");
}
protected void done() {
System.out.println("job Done!");
}
}
and the UI with a little bit of change和 UI 有一点变化
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestTesr {
private JFrame frame;
private JTextField textField;
private JTextArea console;
protected JPanel panel;
private int input;
SearchForWorker sfw;
public int getInput() {
return input;
}
public void setInput(int input) {
this.input = input;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestTesr window = new TestTesr();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestTesr() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 537, 360);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
panel = new JPanel();
panel.setBounds(0, 0, 233, 338);
frame.getContentPane().add(panel);
panel.setLayout(null);
textField = new JTextField();
textField.setBounds(5, 6, 130, 26);
panel.add(textField);
textField.setColumns(10);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(233, 6, 298, 326);
frame.getContentPane().add(scrollPane);
console = new JTextArea();
console.setEditable(false);
scrollPane.setViewportView(console);
JButton btnNewButton = new JButton("Submit");
btnNewButton.setBounds(140, 5, 88, 29);
panel.add(btnNewButton);
JLabel show = new JLabel("");
show.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
show.setForeground(SystemColor.textHighlight);
show.setBounds(49, 115, 100, 53);
panel.add(show);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("Data Submitted\n");
console.append("Data Submitted\n");
String y = textField.getText();
int g = Integer.parseInt(y);
setInput(g);
try {
Thread.sleep(100);
} catch (Exception q) {
System.out.println("Error occur for program time break" + q.toString());
}
g++;
sfw = new SearchForWorker(String.valueOf(g), console);
sfw.execute();
}
});
}
}
some stuff is not exactly like what you have used in the original code like objects visibility.有些东西与您在原始代码中使用的东西不完全一样,例如对象可见性。 do some changes if you want.
如果你愿意,做一些改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.