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

问题描述 投票:-1回答:1

当我的程序启动时,我的JProgressBar就像我想要的那样运行并显示程序加载的进度,但我有一个数据库的重启函数,我必须使用dispose()来重置所有组件,从而重新加载所有组件组件。

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

运行以下代码:If语句测试数据库文件是否存在,从那里加载进度条类并设置i的最大参数和值。当数据库和GUI加载它然后增加进度条的值,并且当GUI完成加载时,它使用dispose()来关闭JProgressBar使用的窗口。它继续,如果不存在,它将使用新的JProgressBar重新启动它。

我所知道的是,当第一次加载进度条时,它会起作用。但是当我第二次需要它时,它失败了。即使我重置了值,它仍然不想工作。

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并让它向用户显示程序在后台工作时所取得的进展...原因是,随着数据库的增长或何时导入完成,需要时间,程序必须表明它正在处理任务,而不仅仅是在那里闲置。

java multithreading swing jprogressbar
1个回答
1
投票

可能的原因:

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

第二次调用代码时,它很可能是从Swing事件中调用的,并且可能没有在SwingWorker或其他后台线程中专门调用,所以这次在GUI事件线程上调用它,因此阻止了这个线程,意味着进度条没有以图形方式更新

解决方案:了解并使用SwingWorker完成长时间运行的后台工作,更新worker的progress属性(允许从0到100),将PropertyChangeListener附加到同一个worker列表以获取进度更改,并更新JProgressBar从此属性内部更改侦听器。

请阅读Concurrency in Swing以更好地理解Swing事件线程的工作方式。

© www.soinside.com 2019 - 2024. All rights reserved.