Results 1 to 5 of 5

Thread: How to Use Progress Bars in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    201

    How to Use Progress Bars in Java?

    Hello friends,
    I am new to this forum. I have made a small project using the Java. I have just used some basic functions of the Java for creating that project. Now I want to add the progress bars into that project. So that there will be an attractive and also professional look to my project. But I don't know to add the progress bars. Can anyone explain me how to use Progress Bars in Java? If you provide the coding for the same, that would be much helpful for me..!!
    Don't be afraid of darkness, it's in your heart anyway.

  2. #2
    Join Date
    Mar 2008
    Posts
    349

    Re: How to Use Progress Bars in Java?

    There are three classes provided by the Swing for using the Progress Bar. The following are the classes provided by the Swing :
    1. JProgressBar - This is a visible component to graphically display how much of a total task has completed. This can be useful to tell you how to animate a progress bar to show activity before the task's scope is known.
    2. ProgressMonitor - This class is not a visible component. If necessary, an instance of this class monitors the progress of a task and pops up a dialog.
    3. ProgressMonitorInputStream - This class is an input stream with an attached progress monitor, which monitors reading from the stream.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to Use Progress Bars in Java?

    JProgressBar is the class which creates the progress bar using it's constructor JProgressBar() to show the status of your process completion. Sometimes you can't immediately determine the length of a long-running task, or the task might stay stuck at the same state of completion for a long time. The constructor JProgressBar() takes two argument as parameter in which are as follows :
    • The initial value of the progress bar which is shown in the starting
    • The counter value by which the value of the progress bar is incremented.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to Use Progress Bars in Java?

    I have provided you with a code that creates and sets up the progress bar, you can check the code below :

    Code:
    JProgressBar progressBar;
        ...
        progressBar = new JProgressBar(0, task.getLengthOfTask());
        progressBar.setValue(0);
        progressBar.setStringPainted(true);

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to Use Progress Bars in Java?

    The following is the full code for the Progress Bars in Java :
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.html.*;
    
    public class SwingProgressBar{
        final static int interval = 2000;
      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, 30);
            pb.setValue(0);
            pb.setStringPainted(true);
    
        label = new JLabel("Techarena.in");
        
        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(30, 30, 30, 30));
            frame.setContentPane(panel1);
            frame.pack();
            frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            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=\"#FFFF00\">" + "<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=\"#088000\">" + "<b>" + 
    "Downloading is in process......." + "</b>" + "</font>" + "</html>";
          label.setText(str);
                timer.start();
            }
        }
        
        public static void main(String[] args) {
            SwingProgressBar spb = new SwingProgressBar();
        }
    }

Similar Threads

  1. Summary progress bars in Gantt view don't match % complete
    By mbbackus in forum Microsoft Project
    Replies: 1
    Last Post: 27-10-2011, 02:25 PM
  2. iPhone iOS 4.0.1 How many bars
    By Layton in forum Portable Devices
    Replies: 4
    Last Post: 15-09-2010, 02:10 PM
  3. Windows Vista Blue Progress Bars
    By Adel123 in forum Customize Desktop
    Replies: 3
    Last Post: 26-08-2009, 06:37 PM
  4. Uninstalling ie tool bars
    By nilimarani in forum Windows Software
    Replies: 4
    Last Post: 12-08-2009, 09:42 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,326,977.86619 seconds with 17 queries