如何设置JProgressbar以显示程序加载的进度 [英] How can I set up my JProgressbar to show the progress of my program loading

查看:305
本文介绍了如何设置JProgressbar以显示程序加载的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的程序启动时,我的JProgressBar会按照我想要的方式运行,并显示我的程序加载进度,但是我具有数据库的重启功能,因此必须使用dispose()来重置所有组件,因此重新加载所有组件.

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根本不加载(只是一个空白窗口),整个类都不希望工作.

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.

运行以下代码:If语句测试数据库文件是否存在,从那里加载进度条类并设置i的max参数和值.当数据库和GUI加载时,它会增加进度栏的值,当GUI完成加载时,它将使用dispose()关闭JProgressBar使用的窗口.继续进行下去,如果不存在,它将使用新的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. 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();
            }

        }
    }

我需要做的是,我应该能够在程序执行过程中随时调用JProgressBar,并让其向用户显示程序在后台工作时正在取得的进度...原因是,作为数据库增长或完成导入后,这需要时间,并且程序必须显示它正在执行任务,而不仅仅是在此处闲逛.

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.

推荐答案

可能的原因:

第一次,上面的代码是从main方法调用的,因此不会从Swing事件线程调用,因此不会阻塞该线程,从而允许它执行其工作,包括绘制JProgressBar和其余的代码GUI.

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.

第二次调用该代码可能是从Swing事件调用的,并且可能不是在SwingWorker或其他后台线程中专门调用的,因此这一次是在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

解决方案:了解并使用SwingWorker进行长期运行的后台工作,更新worker的progress属性(允许从0到100),将PropertyChangeListener附加到该同一worker列表中以进行进度更改,并从此属性更改侦听器中更新您的JProgressBar.

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.

请阅读 Swing的并发性,以便更好地了解Swing的工作原理摆动事件线程.

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

这篇关于如何设置JProgressbar以显示程序加载的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆