JProgressBar nie wyświetla postępu.

0

Witam,
JProgressBar nie wyświetla postępu. W ogóle nie widać żadnych zmian. Co robię źle?

                        jProgressBar2.setMinimum(0);
                        jProgressBar2.setMaximum(authors.size()-1);
                        int nOfAuthor = 0;
                        ProgressBar2Thread progressBar2Thread = new ProgressBar2Thread();
                        Thread t = new Thread(progressBar2Thread);
                        t.start();
                        for (modelRsds.Author author : authors) {
                            nOfAuthor += 1;
                            String expertName = author.getName() + " " + author.getLastname();
                            progressBar2Thread.setN(nOfAuthor);
                           try {
                                System.out.println(expertName);
    class ProgressBar2Thread implements Runnable {
        private int n;
        
        public void run() {
            jProgressBar2.setValue(n);
        }

        public void setN(int n) {
            this.n = n;
        } 
    }
0

Wywołując t.start() wywołujesz run raz, gdy n jest równe zero. Nie powtarzasz tego, więc progressbar stoi w miejscu. Dlaczego tworzysz tu nowy wątek?

0

@Jaca777 Zatrzymuję się na 3%, a potem error. No, ale przynajmniej ruszył.

                    if(authors!=null){
                        resultFromArnetMiner = new ArrayList<Result>();
                        jProgressBar2.setMinimum(0);
                        jProgressBar2.setMaximum(authors.size()-1);
                        jProgressBar2.setStringPainted(true);
                        int nOfAuthor = 0;
                        
                        //Thread t = new Thread(progressBar2Thread);
                        ProgressBar2Thread progressBar2Thread = new ProgressBar2Thread();
                        Thread t = new Thread(progressBar2Thread);
                        for (modelRsds.Author author : authors) {
                            progressBar2Thread.setN(nOfAuthor+100);
                            t.start();
                            String expertName = author.getName() + " " + author.getLastname();
                            
                            jProgressBar2.repaint();
 

Dostaję:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException

A w jaki inny sposób najprostszy mogę to zrobić?

1

Nie możesz uruchomić dwa razy jednego wątku. Może będziesz po prostu ustawiał wartość tego progress baru? Po co Ci tutaj nowy wątek?

0

Daj kod, który nie działa i nie tworzysz nowego wątku. Nie musisz używać repaint.

1 użytkowników online, w tym zalogowanych: 0, gości: 1