import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* JProgressMonitor.java
*
* Created on Jul 28, 2010, 12:37:13 AM
*/
/**
*
* @author Wilson Leong
*/
public class JProgressMonitor extends javax.swing.JFrame {
/** Creates new form JProgressMonitor */
public JProgressMonitor() {
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() {
jLabel1 = new javax.swing.JLabel();
jbtStart = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Progress Monitor");
jLabel1.setText("Please click me");
jbtStart.setText("Start");
jbtStart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtStartActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jbtStart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(61, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbtStart)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// //GEN-END:initComponents
private void jbtStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtStartActionPerformed
/*display the progress monitor*/
progressMonitor = new ProgressMonitor(JProgressMonitor.this, "Content loading...","", 0, 100);
task = new Task(); // invoke the task method
/*perform the progress monitor loading*/
task.addPropertyChangeListener(new PropertyChangeListener(){
public void propertyChange(PropertyChangeEvent e){
if("progress".equals(e.getPropertyName())){
int progress = (Integer)e.getNewValue();
progressMonitor.setProgress(progress);
}
}
});
task.execute(); // execute swingworker
}//GEN-LAST:event_jbtStartActionPerformed
/*Task Class for SwingWorker*/
public class Task extends SwingWorker{
public Void doInBackground(){ // Code run on background thread
Random random = new Random(); // invoke the random method
int progress = 0; // initialize the progress to 0
setProgress(0);
while(progress < 100){
try{
Thread.sleep(random.nextInt(1000));
}
catch(InterruptedException ex){
}
progress += (Integer)random.nextInt(10);
setProgress(Math.min(progress,100));
}
JOptionPane.showMessageDialog(null, "The load content is complete", "Complete", JOptionPane.PLAIN_MESSAGE);
return null; // doInBackground must return null
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JProgressMonitor().setVisible(true);
}
});
}
private Task task;
private ProgressMonitor progressMonitor;
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JButton jbtStart;
// End of variables declaration//GEN-END:variables
}