aquaballoon

Java: Timer with ProgressBar

Feb 28th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.text.html.*;
  5.  
  6. public class NewMain{
  7.     final static int interval = 1000;
  8.     int i;
  9.     JLabel label;
  10.     JProgressBar pb;
  11.     Timer timer;
  12.     JButton button;
  13.  
  14. public NewMain() {
  15.     JFrame frame = new JFrame("Swing Progress Bar");
  16.    
  17.     button = new JButton("Start");
  18.     button.addActionListener(new ButtonListener());
  19.  
  20.     pb = new JProgressBar(0, 20);
  21.     pb.setValue(0);
  22.     pb.setStringPainted(true);
  23.  
  24.     label = new JLabel("c");
  25.  
  26.     JPanel panel = new JPanel();
  27.     panel.add(button);
  28.     panel.add(pb);
  29.  
  30.     JPanel panel1 = new JPanel();
  31.     panel1.setLayout(new BorderLayout());
  32.     panel1.add(panel, BorderLayout.NORTH);
  33.     panel1.add(label, BorderLayout.CENTER);
  34.     panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  35.     frame.setContentPane(panel1);
  36.     frame.pack();
  37.     frame.setVisible(true);
  38.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.  
  40.     //Create a timer. (Timer Stop with Action)
  41.     timer = new Timer(interval, new ActionListener() {
  42.         public void actionPerformed(ActionEvent evt) {
  43.             if (i == 20){
  44.                 Toolkit.getDefaultToolkit().beep();
  45.                 timer.stop();
  46.                 button.setEnabled(true);
  47.                 pb.setValue(0);
  48.                 String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" +
  49.                 "Downloading completed." + "</b>" + "</font>" + "</html>";
  50.                 label.setText(str);
  51.             }
  52.            
  53.             i = i + 1;
  54.             pb.setValue(i);
  55.            
  56.             }
  57.         });
  58.       }
  59.  
  60.   //Timer Event (Timer Start)
  61.   class ButtonListener implements ActionListener {
  62.     public void actionPerformed(ActionEvent ae) {
  63.         button.setEnabled(false);
  64.         i = 0;
  65.         String str = "<html>" + "<font color=\"#008000\">" + "<b>" +
  66.         "Downloading is in process......." + "</b>" + "</font>" + "</html>";
  67.         label.setText(str);
  68.         timer.start();
  69.         }
  70.     }
  71.  
  72.  
  73.   public static void main(String[] args) {
  74.     NewMain spb = new NewMain();
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment