javaswingjtablejprogressbartablecelleditor

Progress Bar Java


I am using JProgressBar to show progress. But, How to show the progressBar as loading from 0 to 100? I got the code from internet and its working except the progressBar is not loading.

code

progressFrame = new JFrame(); // frame to display progress bar
progressBar = new JProgressBar(0,100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressFrame.add(progressBar);

new SwingWorker<Void,Void>()
    {
        protected Void doInBackground() throws SQLException, ClassNotFoundException
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            progressBar.setValue(0);
            frame.setEnabled(false); // frame = main frame

            //tableclass creates a JTable with data from database
            tableclass = new TheDatabaseTable(deptName);//it takes time to create
            progressBar.setValue(50);
            frame.getContentPane().removeAll();
            frame.setContentPane(tableclass);

            frame.validate();
            frame.repaint();

            progressBar.setValue(100);
            //progressFrame.dispose();
            return null;
        }; 
        protected void done()
        {
            //progressFrame.setVisible(false);
            frame.setVisible(true);
            progressFrame.dispose();
            frame.setEnabled(true);
        }

    }.execute();

I would appreciate if anyone edit the above code to work. Thank you.


Solution

  • You have to use threads for that. Design a class that implements Runnable interface which will update the values like this.

    class ProgressBarUpdator implements java.lang.Runnable {
    
        /**
         * Progress bar that shows the current status
         */
        private javax.swing.JProgressBar jpb = null;
        /**
         * Progress bar value
         */
        private java.lang.Integer value = null;
    
        /**
         * Constructor
         * @param jpb The progress bar this has to update
         */
        public ProgressBarUpdator(javax.swing.JProgressBar jpb) {
            this.jpb = jpb;
            jpb.setMaximum(100);
        }
    
        /**
         * Sets the value to the progress bar
         * @param value Value to set
         */
        public void setValue(java.lang.Integer value) {
            this.value = value;
        }
    
        /**
         * Action of the thread will be executed here. The value of the progress bar will be set here.
         */
        public void run() {
            do {
                if (value != null) {
                    jpb.setValue((int)java.lang.Math.round(java.lang.Math.floor(value.intValue() * 100 / maximum)));
                }
                try {
                    java.lang.Thread.sleep(100L);
                } catch (java.lang.InterruptedException ex) {
                    ex.printStackTrace();
                }
            } while (value == null || value.intValue() < jpb.getMaximum());
        }
    }
    

    and in your frame class use progressBar with the new class like this

    ProgressBarUpdator ju = new ProgressBarUpdator(progressBar);
    new java.lang.Thread(ju).start();
    

    Whenever you want to change the value just use the statement

    ju.setValue([Value to set]);