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

江枫思渺然 提交于 2019-12-31 07:47:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/54716324/how-can-i-set-up-my-jprogressbar-to-show-the-progress-of-my-program-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!