簡體   English   中英

GUI 在 SwingWorker 線程中因長時間運行的任務而凍結

[英]GUI freezes with long running task in a SwingWorker Thread

當用戶單擊 GUI 應用程序中的按鈕時,我會執行長時間運行的任務絞盤:

simplfyButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {


                simplfyAllText();
            }//mouse evebt
        });

simplfyAlltext() 方法執行一些 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();
    }

即使長時間運行的后台任務在 SwingWorker 線程上運行,GUI 在其執行期間仍然凍結,並且來自 SwingWorker.process 方法的進度似乎無法更新 ProgressBar。 任何輸入表示贊賞。

sw.run();

你應該使用:

sw.execute();

在另一個線程上開始執行。

有關更多信息和工作示例,請閱讀 Swing 教程中關於並發的部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM