Tuesday, January 10, 2017

How to make progress bar in swing and making it disable for closing, minimizing and maximizing?



How to make progress bar in swing and making it disable for closing, minimizing and maximizing. It will automatically dispose off after complete application execution?


package net.codejava.swing.download;

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class ProgressBarDemo extends JFrame{
              /**
        *
        */
       private static final long serialVersionUID = 1L;
              JProgressBar jb;
              int i = 0, num = 0;

              ProgressBarDemo() {
                     setTitle("Launching ");
                     jb = new JProgressBar(0, 1000);
                     jb.setBounds(30, 30, 150, 40);
                     jb.setValue(0);
                     jb.setStringPainted(true);
                     add(jb);
                     setSize(250, 150);
                     setLayout(new FlowLayout());

//user can not close the progress bar and stop application processing
                     setEnabled(false);
              }

              public void iterate() {
                     while (i <= 1000) {
                           if(jb.isEnabled()){
                           jb.setValue(i);
                           i = i + 20;
                           System.out.println(i);
                           }
                           else{         }

              public void progress () {
                     while (i <= 1000) {
                         
                           jb.setValue(i);
                           i = i + 30;
                        

                           try {
                                  Thread.sleep(100);
                           } catch (Exception e) {
                           }
                     }
              }

              public static void main(String[] args) {
                     ProgressBarDemo m = new ProgressBarDemo();
                     m.setVisible(true);
                     m.progress();
//                  closing the frame after 100%
                     m.dispose();
                    
              }
}

----------------------------------------------------------------------------------------------

Output: User can close this progress window and program will be stop immediately.



No comments:

Post a Comment

Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...