Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.

Progressbar Uisng Swing

Through the given example you can understand how the progress bar is created for showing your work is in progress. This program shows you a frame in which a button labeled by the string "Start", a progress bar and another is the label which has been used to display some messages. When you click on the "Start" button progress bar is started to progress the completed process in percent and the label which holds the text "Roseindia.net" is change to with the label text "Downloading is in process......" in green color and the button is disabled. When the value of the progress bar is become 100% then the label text of the label is changed with the text "Downloading completed." in red color and "Start" button is enabled.The label display the information about the Downloading process whether completed or not. But here, nothing is downloading through the program. This message on the label has been used only for showing in output of the program. For completion the process, there are some methods and APIs are used in the program . They are:

JProgressBar
setStringPainted(boolean)
setValue()
Timer()

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.html.*;

public class SwingProgressBar{
    final static int interval = 1000;
  int i;
  JLabel label;
    JProgressBar pb;
    Timer timer;
    JButton button;

    public SwingProgressBar() {
    JFrame frame = new JFrame("Swing Progress Bar");
        button = new JButton("Start");
        button.addActionListener(new ButtonListener());

    pb = new JProgressBar(0, 20);
        pb.setValue(0);
        pb.setStringPainted(true);

    label = new JLabel("Roseindia.net");
    
    JPanel panel = new JPanel();
        panel.add(button);
        panel.add(pb);

        JPanel panel1 = new JPanel();
        panel1.setLayout(new BorderLayout());
        panel1.add(panel, BorderLayout.NORTH);
    panel1.add(label, BorderLayout.CENTER);
        panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        frame.setContentPane(panel1);
        frame.pack();
        frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create a timer.
        timer = new Timer(interval, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
        if (i == 20){
          Toolkit.getDefaultToolkit().beep();
          timer.stop();
          button.setEnabled(true);
          pb.setValue(0);
          String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + 
"Downloading completed." + "</b>" + "</font>" + "</html>";
          label.setText(str);
        }
        i = i + 1;
                pb.setValue(i);
            }
        });
    }

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
            button.setEnabled(false);
      i = 0;
      String str = "<html>" + "<font color=\"#008000\">" + "<b>" + 
"Downloading is in process......." + "</b>" + "</font>" + "</html>";
      label.setText(str);
            timer.start();
        }
    }
    
    public static void main(String[] args) {
        SwingProgressBar spb = new SwingProgressBar();
    }
}


 
 
Job or extra money for students

Search Engine Rank of your blog or websites