简体   繁体   English

如何设置我的JProgressbar以显示程序加载的进度

[英]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. 当我的程序启动时,我的JProgressBar会按照我想要的方式运行,并显示我的程序加载进度,但是我具有数据库的重新启动功能,因此必须使用dispose()重置所有组件,从而重新加载所有组件。

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. 重新启动过程与启动过程完全相同,但是在这种情况下,JProgressbar根本不加载(只是一个空白窗口),整个类都不想工作。

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. 通过以下代码运行:If语句测试数据库文件是否存在,从那里加载进度条类并设置i的max参数和值。 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. 在数据库和GUI加载时,它会增加进度栏的值,并且在GUI完成加载时,它使用dispose()关闭JProgressBar使用的窗口。 It goes on that if it is not there it will restart it with a new JProgressBar. 继续进行下去,如果不存在,它将使用新的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. 我需要的是,我应该能够在程序执行过程中随时调用JProgressBar,并让其向用户显示程序在后台工作时正在取得的进展...原因是,随着数据库的增长或何时导入已完成,这需要时间,并且程序必须显示它正在执行任务,而不仅仅是闲着。

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. 第一次,上面的代码是从main方法调用的,因此不会从Swing事件线程调用,因此不会阻塞该线程,从而允许它执行其工作,包括绘制JProgressBar和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 第二次调用该代码时,很可能是通过Swing事件调用的,而可能不是从SwingWorker或其他后台线程中专门调用的,因此这次是在GUI事件线程上调用的,因此确实会阻塞该线程,表示进度条不会以图形方式更新

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. 解决方案:了解并使用SwingWorker进行长时间的后台工作,更新worker的progress属性(允许从0到100),将PropertyChangeListener附加到同一worker列表以进行进度更改,并更新您的此属性内的JProgressBar更改侦听器。

Do read Concurrency in Swing to better understand the workings of the Swing event thread. 请阅读Swing中的并发性,以更好地了解Swing事件线程的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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