Problema com JProgressBar e Thread

5 respostas
danieldestro

Caros, simplifiquei o código da minha tela que é um JFrame. Ao se clicar no botão eu inicio um processo e quero exibir um JProgressBar num JDialog enquanto o processo executa. Só que a droga do Dialog não renderiza na tela direito. Alguma luz?

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Teste extends JFrame {
    public Teste() {
        getContentPane().setLayout( new FlowLayout() );
        getContentPane().add( new JTextField("FUCK THE WORLD!") );
        JButton b = new JButton("OK");
        getContentPane().add( b );
        setLocationRelativeTo(null);
        b.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        Progress p = new Progress();
                        Thread t = new Thread( p );
                        t.start();
                        //faz algo aqui
                        try {
                            Thread.sleep(5000);
                        } catch( Exception e ) {}
                        p.dispose();
                    }
                }
                );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        pack();
    }

    public static void main(String[] args) {
        Teste t = new Teste();
        t.show();
    }
}

class Progress extends JDialog implements Runnable {
    public Progress() {
        setModal(true);
        setLocationRelativeTo(null);
        JProgressBar j = new JProgressBar();
        j.setIndeterminate(true);
        j.setSize(200,20);
        getContentPane().add(j);
        pack();
    }
    
    public void run() {
        this.show();
    }
}

5 Respostas

brlima

pelo que vi, acho que a Thread que vc esta utilizando para executar o processo ( preenchendo a jprogress ) é o mesmo da JDialog, por isso ela nao esta atualizando.
Alterei seu codigo pra tentar gerar uma Thread para o processo em si:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TesteProgress extends JFrame {
    public TesteProgress() {
        getContentPane().setLayout( new FlowLayout() );
        getContentPane().add( new JTextField("FUCK THE WORLD!") );
        JButton b = new JButton("OK");
        getContentPane().add( b );
        b.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        
                        final Progress p = new Progress();
                        // Mostra antes de iniciar o count ( Thread )
                        p.iniciarProcessos();
                        
                    }
                }
                );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        pack();
    }

    public static void main(String[] args) {
        TesteProgress t = new TesteProgress();
        t.show();
    }
}

class Progress extends JDialog{
    public JProgressBar j;
    public Progress() {
        j = new JProgressBar();
        j.setMaximum(100);
        j.setMinimum(0);
        getContentPane().add(j, BorderLayout.CENTER);
        pack();
    }
    public void iniciarProcessos(){
        new Thread(){
            public void run(){
                // porcesso rola aqui
                // atualização do progress tb
                for( int i=0; i<100; i++){
                    j.setValue(i);
                    try{
                        sleep(10);
                    }catch(Exception e){}
                }
            }
        }.start();
        show();
    }
}

flw!

saoj

Swing é complicado, JDialog é mais complicado ainda com o lance do modal, junte-se a isso thread e eu me ferrei… Não consigo entender porque não funciona…

danieldestro

Caramba! Mudei algumas coisas mas não rolou.
Do jeito que o Bruno fez, eu to suspeitando que na nova Thread deve rolar o processo a ser executado e não a exibição do JDialog.
Estou certo?

brlima

Exatamente… Mas tem um esquema de chamar a janela em outra Thread, mas eu nao to achando. Qdo achar eu posto aqui.

danieldestro

Se eu coloco o processo em uma nova Thread dá certo. Se coloco o JDialog na nova Thread, não dá.

Assim funfa:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Teste extends JFrame {
    public Teste() {
        getContentPane().setLayout( new FlowLayout() );
        getContentPane().add( new JTextField("FUCK THE WORLD!") );
        JButton b = new JButton("OK");
        getContentPane().add( b );
        setLocationRelativeTo(null);
        b.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        final Progress p = new Progress();
                        
                        Thread t = new Thread( new Runnable() {
                            public void run() {
                                //faz algo aqui
                                try {
                                    Thread.sleep(3000);
                                    System.out.println("aaa");
                                    p.hide();
                                } catch( Exception e ) {}
                            }
                        });
                        t.start();
                        p.show();
                    }
                }
                );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        pack();
    }

    public static void main(String[] args) {
        Teste t = new Teste();
        t.show();
    }
}

class Progress extends JDialog {
    public Progress() {
        setModal(true);
        setLocationRelativeTo(null);
        JProgressBar j = new JProgressBar();
        j.setIndeterminate(true);
        j.setSize(200,20);
        getContentPane().add(j);
        pack();
    }
}
Criado 17 de fevereiro de 2005
Ultima resposta 18 de fev. de 2005
Respostas 5
Participantes 3