Set Bounds for JProgressBar in Java Swing

Java 8Object Oriented ProgrammingProgramming

Use the setBounds() method to set Bounds for JProgressBar in Java Swing −

JProgressBar progressBar;
progressBar.setBounds(70, 50, 120, 30);

The following is an example to set bounds for JProgressBar −

Example

package my;
import javax.swing.*;
public class SwingDemo extends JFrame {
   JProgressBar progressBar;
   int i = 0;
   SwingDemo() {
      progressBar = new JProgressBar(0, 1000);
      progressBar.setBounds(70, 50, 120, 30);
      progressBar.setValue(0);
      progressBar.setStringPainted(true);
      add(progressBar);
      setSize(550, 150);
      setLayout(null);
   }
   public void inc() {
      while (i <= 1000) {
         progressBar.setValue(i);
         i = i + 50;
         try {
            Thread.sleep(100);
         } catch (Exception e) {}
      }
   }
   public static void main(String[] args) {
      SwingDemo s = new SwingDemo();
      s.setVisible(true);
      s.inc();
   }
}

The output displays a ProgessBar that fills completely −

Output

raja
Published on 09-May-2019 09:08:50
Advertisements