SwingWorker process()不会实时更新JTextArea

时间:2018-01-12 04:10:24

标签: 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;
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:

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");
    }


}

1 个答案:

答案 0 :(得分:0)

3 年多之后,它可能完全没用,但它不是你有一天想要的:

为我工作的 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!");
    }

}

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();

            }
        });
    }
}

有些东西与您在原始代码中使用的东西不完全一样,例如对象可见性。如果你愿意,做一些改变。