简体   繁体   English

使用SwingWorker更新JTextArea

[英]Update JTextArea with SwingWorker

I'm trying use the SwinWorker to update one TextArea with the status of the execution of one button actionPerform method. 我正在尝试使用SwinWorker用一个按钮actionPerform方法的执行状态更新一个TextArea。

I get various samples from internet and nothing work form me. 我从互联网上获得了各种各样的样本,但我没有任何工作。 Anybody can help me? 有人可以帮助我吗?

bellow my button method: 我的按钮方法如下:

private void buttonActionPerformed(java.awt.event.ActionEvent evt) {                                                
        atualizaTextArea("", true);
        button.setEnabled(false);

        SearchDataBase cd = new SearchDataBase();
        textAreaField.setForeground(Color.BLUE);
        try {
            refreshTextArea("......Process Started......\n\n", true);
            cd.search();
            String textt = textAreaField.getText();
            refreshTextArea("Finish\n\n", false);
        } catch (Exception e) {
            button.setEnabled(true);
            StringWriter errors = new StringWriter();
            e.printStackTrace(new PrintWriter(errors));

            textAreaField.setText("Error - Contact the developer");
            textAreaField.setForeground(Color.red);
        }

        if (comboBoxSearchType.getSelectedIndex() == 1) {
            try {
                refreshTextArea("......History search started (too slow!!)......\n\n", false);
                cd.searchHistoryTable(dateChoserPesquisa.getDate().getTime());
                refreshTextArea("Finish\n\n", false);
            } catch (Exception e) {
                button.setEnabled(true);
                StringWriter errors = new StringWriter();
                e.printStackTrace(new PrintWriter(errors));

                textAreaField.setText("Error - Contact the developer");
                textAreaField.setForeground(Color.red);
            }
        }
        refreshTextArea("...............Finish..............", false);
        button.setEnabled(true);
    }

TextAreaAviso is the JtextArea that I'm trying to update. TextAreaAviso是我要更新的JtextArea。

refreshTextArea(String, boolean) is the method that I have created to update the JTextArea refreshTextArea(String,boolean)是我创建的用于更新JTextArea的方法

  private void refreshTextArea(String text, boolean rep) {
        this.texttt = text;
        this.replace = rep;


        // define a SwingWorker to run in background  
        SwingWorker<String, String> worker = new SwingWorker<String, String>() {
            @Override
            public String doInBackground() {
                String t = textAreaField.getText();
                if (replace) {
                    publish(texttt);
                    t = texttt;
                } else {
                    publish(t + texttt);
                    t+= texttt;
                }
                System.out.println(texttt);

                return "";
            }

            @Override
            protected void process(final List<String> chunks) {
                for (String text : chunks) {
                    TextAreaAviso.setText(texttt);
                    System.out.println("HERE!!!!");
                }
            }

        };
        worker.execute();

       // (new AtualizaTextArea(TextAreaAviso, texto, replace)).execute();
    }

In this case I have created all process in the method scope, but I was tried using a separated class, as bellow: 在这种情况下,我已经在方法范围内创建了所有进程,但是尝试使用单独的类,如下所示:

private void refreshTextArea(String text, boolean rep) {
       (new RefreshTextArea(TextAreaAviso, texto, replace)).execute();
 }

class RefreshTextArea extends SwingWorker<Integer, String> {

    private JTextArea textArea;
    private String texto;
    private boolean replace;

    public RefreshTextArea(JTextArea textArea, String texto, boolean replace) {
        this.textArea = textArea;
        this.texttt = texto;
        this.replace = replace;
    }

    @Override
    protected Integer doInBackground() throws Exception {

        int i = 0;
        String t = "";
        if (!replace) {
            t = texttt + textArea.getText();
        } else {
            t = texttt;
        }

        System.out.println(t);
        publish(t);

        return i;
    }

    @Override
    protected void process(final List<String> chunks) {
        for (String text : chunks) {
            textArea.setText(texttt);
        }
    }
}

In both implementations I have tried using with and without execution of revalidate method of textarea. 在这两种实现中,我都尝试使用和不执行textarea的revalidate方法。 In both implementations I have tried update the textarea value in doBackground method and/or process method. 在这两种实现方式中,我都尝试过在doBackground方法和/或过程方法中更新textarea值。

Your SwingWorker should not access or update the JTextArea in your implementation of doInBackground() . SwingWorker 不应访问或更新JTextArea在执行doInBackground() Instead publish() intermediate results and update the JTextArea only in process() . 而是publish()中间结果, process()更新JTextArea Moreover, setText() will have the effect of replacing the existing text; 而且, setText()将具有替换现有文本的作用; you may want append() instead. 您可能需要append()代替。 A complete example is shown here . 这里显示一个完整的示例。 Note that BackgroundTask has access to the JTextArea in the enclosing scope, but you can also pass it as a parameter to the SwingWorker . 请注意, BackgroundTask可以访问封闭范围内的JTextArea ,但是您也可以将其作为参数传递给SwingWorker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM