Java SwingWorker进程()不';t实时更新JTextArea
我尝试了许多在线提供的解决方案,但仍然不起作用。 我正在创建一个JAR,希望文本可以在JTextArea中实时更新。 但文本仅在完成程序后附加到textarea 我的用户界面:Java SwingWorker进程()不';t实时更新JTextArea,java,swing,user-interface,updates,swingworker,Java,Swing,User Interface,Updates,Swingworker,我尝试了许多在线提供的解决方案,但仍然不起作用。 我正在创建一个JAR,希望文本可以在JTextArea中实时更新。 但文本仅在完成程序后附加到textarea 我的用户界面: package Test; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.BorderLayout; import javax.swing.JTextField; im
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();
;
}
}
});
}
}
封装测试;
导入java.awt.EventQueue;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入java.awt.BorderLayout;
导入javax.swing.JTextField;
导入javax.swing.SwingUtilities;
导入Test.SearchForWorker;
导入javax.swing.JButton;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入javax.swing.JTextArea;
导入javax.swing.JScrollPane;
导入javax.swing.JLabel;
导入java.awt.SystemColor;
导入java.awt.Font;
公共类TestTesr{
私有JFrame;
私有JTextField textField;
专用区域控制台;
私有int输入;
公共int getInput(){
返回输入;
}
公共无效设置输入(int输入){
这个输入=输入;
}
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
TestTesr window=newtesttesr();
window.frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建应用程序。
*/
公共TestTesr(){
初始化();
}
/**
*初始化框架的内容。
*/
私有void初始化(){
frame=新的JFrame();
框架.立根(100100537360);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel面板=新的JPanel();
面板.立根(0,0,233,338);
frame.getContentPane().add(面板);
panel.setLayout(空);
textField=新的JTextField();
textField.setBounds(5,6,130,26);
panel.add(textField);
textField.setColumns(10);
JScrollPane scrollPane=新的JScrollPane();
滚动窗格.立根(233,6298326);
frame.getContentPane().add(滚动窗格);
控制台=新的JTextArea();
console.setEditable(false);
scrollPane.setViewportView(控制台);
JButton btnNewButton=新JButton(“提交”);
btnNewButton.setBounds(140,5,88,29);
面板。添加(BtnewButton);
JLabel show=新JLabel(“”);
show.setFont(新字体(“Lucida Grande”,Font.PLAIN,16));
show.setForeground(SystemColor.textHighlight);
显示.挫折(49,115,100,53);
面板。添加(显示);
addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
系统输出打印(“提交的数据”);
新建SearchForWorker(“数据已提交”,控制台).execute();
;
字符串y=textField.getText();
int g=整数。parseInt(y);
设定输入(g);
对于(int u=1;u来说,3年多之后,它可能完全没用,但这不是你有朝一日想要的吗:
那个为我工作的荡秋千工人
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!");
}
}
有些东西与您在原始代码中使用的东西不完全相同,例如对象可见性。如果需要,请进行一些更改。Thread.sleep(3000)ActionListener
中的正在阻止事件调度线程,该线程正在停止更新UI。此功能属于您的SwingWorker
的doInBackgrond
方法,或者更好的是,根本不调用sleep()。这实际上是浪费时间。尽管我删除了sleep(),文本没有在teaxtarea中实时更新。删除睡眠消息后,文本区域会在您按btnNewButton
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!");
}
}
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();
}
});
}
}