import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.Random; import javax.swing.JOptionPane; import javax.swing.SwingWorker; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * ProgressBarDemo.java * * Created on Jul 10, 2010, 11:35:31 PM */ /** * * @author Wilson Leong */ public class ProgressBarDemo extends javax.swing.JFrame { class Task extends SwingWorker{ public Void doInBackground(){ int progress = 0; Random random = new Random(); setProgress(0); jtaList.setText(""); while(progress < 100){ try{ Thread.sleep(random.nextInt(1000)); } catch(InterruptedException ex){ } progress += (Integer)random.nextInt(10); setProgress(Math.min(progress,100)); jtaList.append(String.format("%d%% complete.", progress) + "\n"); } JOptionPane.showMessageDialog(null, "The loading is complete.", "Complete", JOptionPane.INFORMATION_MESSAGE); jbtOk.setEnabled(true); return null; } } /** Creates new form ProgressBarDemo */ public ProgressBarDemo() { 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. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { jpbProgressBar = new javax.swing.JProgressBar(); jbtOk = new java.awt.Button(); jScrollPane1 = new javax.swing.JScrollPane(); jtaList = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("JProgressBarDemo"); jbtOk.setLabel("Ok"); jbtOk.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbtOkActionPerformed(evt); } }); jtaList.setColumns(20); jtaList.setEditable(false); jtaList.setRows(5); jScrollPane1.setViewportView(jtaList); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(82, 82, 82) .addComponent(jbtOk, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jpbProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jpbProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jbtOk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); pack(); }// //GEN-END:initComponents private void jbtOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtOkActionPerformed jpbProgressBar.setStringPainted(true); task = new Task(); task.addPropertyChangeListener(new PropertyChangeListener(){ public void propertyChange(PropertyChangeEvent e){ if("progress".equals(e.getPropertyName())){ jbtOk.setEnabled(false); int progress = (Integer)e.getNewValue(); jpbProgressBar.setValue(progress); } } }); task.execute(); }//GEN-LAST:event_jbtOkActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ProgressBarDemo().setVisible(true); } }); } private Task task; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JScrollPane jScrollPane1; private java.awt.Button jbtOk; private javax.swing.JProgressBar jpbProgressBar; private javax.swing.JTextArea jtaList; // End of variables declaration//GEN-END:variables }