JProgressBar nie wyświetla postępu.

JProgressBar nie wyświetla postępu.
GC
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 249
0

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

Kopiuj
                        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);
Kopiuj
    class ProgressBar2Thread implements Runnable {
        private int n;
        
        public void run() {
            jProgressBar2.setValue(n);
        }

        public void setN(int n) {
            this.n = n;
        } 
    }
Jaca777
  • Rejestracja: dni
  • Ostatnio: dni
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?

GC
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 249
0

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

Kopiuj
                    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ę:

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

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

Jaca777
  • Rejestracja: dni
  • Ostatnio: dni
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?

Jaca777
  • Rejestracja: dni
  • Ostatnio: dni
0

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

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.