简体   繁体   中英

How can I set up my JProgressbar to show the progress of my program loading

When my program starts, my JProgressBar runs just like I want it and shows the progress of my program loading, but I have a restart function for the database and I have to use the dispose() to reset all the components and thus reloading all the components.

The restart process is exactly the same as startup, but in this instance the JProgressbar Doesn't load at all(Just a blank window), the whole class doesn't want to work.

a run through the below code: the If statement tests if the database file exists, from there it loads the progress bar class and sets i's max parameters and values. As the database and GUI loads it then increases the value of the Progress Bar and when the GUI is done loading, it uses the dispose() to close the window the JProgressBar uses. It goes on that if it is not there it will restart it with a new JProgressBar.

What I know is that when it the progressbar is loaded the first time, it will work. BUT when I need it a second time, it fails. Even if I reset the Values, it still doesn't want to work.

GUIBackEnd()
    {
        if(ec.hasDatabase())
        {
            pbc = new ProgressBarController(23, "Initializing E.M.M.A.", true);
            pbc.setProgressText("Start-up");

            update();

            pbc.increaseBar();
            pbc.setProgressText("Loading Equipment");

            equipmentData = ec.viewEquipment();
            pbc.increaseBar();
            pbc.setProgressText("Loading Customers");

            customerData = cc.viewCustomer();
            pbc.increaseBar();
            pbc.setProgressText("Loading Services");

            serviceData = mc.viewService();
            pbc.increaseBar();
            pbc.setProgressText("Loading GUI");

            frameGen();

            pbc.dispose();
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Main database was not found. \nE.M.M.A. will attempt to load the backup...", "WARNING", 
                    JOptionPane.WARNING_MESSAGE);

            String feed = ec.loadMainBackup();

            JOptionPane.showMessageDialog(null, feed, "Loading", 
                    JOptionPane.INFORMATION_MESSAGE);

            if(!EquipmentController.hasLoaded)
            {                
                JOptionPane.showMessageDialog(null, EquipmentController.mainLoaded, "RESTART", JOptionPane.PLAIN_MESSAGE);

                restartGUI(true);
            }
            else
            {
                pbc = new ProgressBarController(23, "Initializing E.M.M.A.", true);
                pbc.setProgressText("Start-up");

                update();

                pbc.increaseBar();
                pbc.setProgressText("Loading Equipment");

                equipmentData = ec.viewEquipment();
                pbc.increaseBar();
                pbc.setProgressText("Loading Customers");

                customerData = cc.viewCustomer();
                pbc.increaseBar();
                pbc.setProgressText("Loading Services");

                serviceData = mc.viewService();
                pbc.increaseBar();
                pbc.setProgressText("Loading GUI");

                frameGen();

                pbc.dispose();
            }

        }
    }

What I need from this, is that I should be able to call the JProgressBar anytime during the program and have it show the user the progress the program is taking while working in the background... reason is that, as the database grows or when imports are done, it takes time and The program has to show that it is working on the task and not just idling there.

The likely cause:

The first time around, the code above is called from the main method, so is not being called from the Swing event thread and so does not block this thread, allowing it to do its work, including drawing the JProgressBar and the rest of the GUI.

The second time that the code is called it's likely being called from a Swing event, and likely not specifically called from within a SwingWorker or other background thread, and so this time it is called on the GUI event thread and so does block this thread, meaning that the progress bar does not get graphically updated

Solution: learn about and use a SwingWorker to do your long-running background work, update the worker's progress property (it is allowed to go from 0 to 100), attach a PropertyChangeListener to this same worker listing for changes to progress, and update your JProgressBar from within this property change listener.

Do read Concurrency in Swing to better understand the workings of the Swing event thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM