【问题标题】:Updating my progressbar while processing [duplicate]处理时更新我的​​进度条[重复]
【发布时间】:2017-01-10 10:45:31
【问题描述】:

我正在用 Java 构建一个小应用程序,它基本上检查一些粘贴文本中的每一行的邮政地址,并检查它们在数据库中的本地化。

一切正常,但检查过程可能需要很长时间,我想添加一个进度条以在每次检查一行时更新其值。

尽管阅读了有关此主题的其他线程,但我不明白为什么我的进度条仅在进程结束时更新。 我收集到我需要以某种不会冻结 GUI 的方式将我的进度条与我的进程分开,但我真的很感激一些帮助并解释如何做到这一点,以及为什么。所以提前感谢任何可以提供帮助的人!

这里是主要代码,进度条中我要更新的进程是button2监听器启动的进程。

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import static sun.security.krb5.Confounder.intValue;

public class mainform {
private JButton button1;
private JTextField txt_1;
private JPanel panelmain;
private JLabel outlab;
private JTextField codepostal;
private JTextArea paste_lignes;
private JButton button2;
private JProgressBar progressBar1;
private JLabel progress;




public mainform() {

    button2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String[] lines = paste_lignes.getText().split("\\n");
            String resultQPV = "";
            int numLines = 0;

            for (int i = 0; i < lines.length; i++) {
                numLines++;
            }

            System.out.println(numLines);

            for (int i = 0; i < lines.length; i++) {
                String adresse = "";
                String CP = "";
                String[] cells = lines[i].split("\\t");
                float percent_d = ((float) i / (float) numLines) * 100;
                System.out.println(i + " " + numLines + " " + percent_d);
                int percent = (int) percent_d;

                progressBar1.setValue(percent);


                try {
                    Thread.sleep(50);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }


                for (int j = 0; j < cells.length; j++) {

                    if (CheckAdresse.identify_me(cells[j]) == "CP") {
                        CP = cells[j];
                    }

                    if (CheckAdresse.identify_me(cells[j]) == "adresse") {
                        adresse = cells[j];
                    }

                }


                String QPV = null;
                String voie = null;
                String numero = null;

                if (adresse == "" || CP == "") {
                    QPV = "erreur sur la ligne";
                } else {
                    voie = ParserAdresse.voie(adresse);
                    numero = Integer.toString(ParserAdresse.numVoie(adresse));
                    try {
                        QPV = CheckAdresse.Check(CP, voie, numero);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }


                resultQPV = resultQPV + QPV + "\n";


                //System.out.println(i + " " + percent + " " + voie + " " + CP + " " + numero);

            }

            paste_lignes.setText(resultQPV);


        }
    });
}


public static void main(String[] args) {
    JFrame frame = new JFrame("mainform");
    frame.setContentPane(new mainform().panelmain);
    frame.setSize(900, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}

}

【问题讨论】:

  • 以下链接可能对您有所帮助Example
  • 成功了!感谢您提供此链接,引用线程中的解决方案对我有用!

标签: java swing progress-bar event-dispatch-thread


【解决方案1】:

这是因为想要两个动作但只在一个线程中运行。所以他们需要共享执行,不能并行工作。
您需要的是在单独的线程中运行您的进度条。
所以你会有

  • 一个应用线程
  • 进度条一个线程

如何在java中使用线程你可以在这里阅读https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多