JProgressBar

7 respostas
A

Ola Galera, sou novo por aki e meio iniciante em Java!
O problema eh o seguinte, estou desenvolvendo um programinha para a faculdade! utilizo o netbeans para fazer os frames, mas de modo pratico, utilizando as ferramentas do programa! Meu programa possui varios frames, e quero fazer um frame inicial, onde eu inicio meu programa, e quero colocar um jprogressbar para fingir um carregamento, da seguinte forma: Clico no botao, finge um carregamento no JprogressBar e chama o proximo frame!!

Como eu Faço isso??
Me ajudem por favor!!

:cry:

7 Respostas

C
import javax.swing.SwingWorker;

/*
 * ProgressTest.java
 *
 * Created on 25 de Abril de 2008, 01:03
 */



/**
 *
 * @author  Tadeu
 */
public class ProgressTest extends javax.swing.JFrame {
    
    /** Creates new form ProgressTest */
    public ProgressTest() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jProgressBar1.setStringPainted(true);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(77, Short.MAX_VALUE)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62))
            .addGroup(layout.createSequentialGroup()
                .addGap(155, 155, 155)
                .addComponent(jButton1)
                .addContainerGap(172, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(75, 75, 75)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(jButton1)
                .addContainerGap(149, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /*Tudo acontece aqui*/
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        
        SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                for( int i = 0; i <= 100;  )
                {
                    jProgressBar1.setValue( i++ );

                    try
                    {
                        Thread.sleep( 100 );
                    }catch( InterruptedException e )
                    {
                             e.printStackTrace();
                    }
                  }
                return null;
             }
        };
        worker.execute();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ProgressTest().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JProgressBar jProgressBar1;
    // End of variables declaration
    
}

Exemplo tosco mas que mostra bem a idéia da coisa =)
A parte essencial é o evento do botão ( linha 71 )…fiz isso rapidão no NetBeans…

Javabuntu

assim ele não vai aprender direito…

A

Ola amigo, obrigado pela ajuda

mas quando implemento esse codigo que você me mandou, da um erro na importação:
import javax.swing.SwingWorker; no SwingWorker

e conseguentemente onde tiver esse SwingWorker!

O que pode esta acontecendo??

Abraços

A

Valeu Carnevali!!

Consegui!!

Brigadão!!

:smiley:

A

Mais Uma Duvida!!!

Não Consegui chamar o proximo frame, depois que termina o jprogressbar!!!

Como eu faço??

Abraços

C
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/*
 * ProgressTest.java
 *
 * Created on 25 de Abril de 2008, 01:03
 */



/**
 *
 * @author  Tadeu
 */
public class ProgressTest extends javax.swing.JFrame {
    
    /** Creates new form ProgressTest */
    public ProgressTest() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jProgressBar1.setStringPainted(true);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(77, Short.MAX_VALUE)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62))
            .addGroup(layout.createSequentialGroup()
                .addGap(155, 155, 155)
                .addComponent(jButton1)
                .addContainerGap(172, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(75, 75, 75)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(jButton1)
                .addContainerGap(149, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /*Tudo acontece aqui*/
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        
        SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                for( int i = 0; i <= 100;  )
                {
                    jProgressBar1.setValue( i++ );

                    try
                    {
                        Thread.sleep( 100 );
                    }catch( InterruptedException e )
                    {
                             e.printStackTrace();
                    }
                  }
                return null;
             }

            @Override
            protected void done() {
                JOptionPane.showMessageDialog( null, "Finalizado!", "Fim", JOptionPane.INFORMATION_MESSAGE );
            }
        };
        worker.execute();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ProgressTest().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JProgressBar jProgressBar1;
    // End of variables declaration
}
Sobrescreva o método done() do SwingWorker.
A

Valeu Carnevalli!!!

Deu Certo!!!

Valeu!!

Criado 24 de abril de 2008
Ultima resposta 26 de abr. de 2008
Respostas 7
Participantes 3