-1

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 Answers1

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • The thing is that the progress bar is not associated with any events. The first start up calls the loading class that creates a JFrame and adds the Components, while the main program loads it mainly uses BackEnd classes to load data and during this time it uses a method from the loader to update the JProgressBar using the setValue(). once done it uses the dispose() to rid the JFrame and its components. Sadly I can't find a way to send data to the SwingWorker's doInBackground() while generating the JFrame with its components and Sending data between the BackEnds while loading the JProgressBar. – Chris Toerien Feb 18 '19 at 19:04
  • @ChrisToerien: How does the user indicate that they wish to "restart" the database? – Hovercraft Full Of Eels Feb 18 '19 at 19:59
  • The user has the option to backup the database or load the database from the backup. When loading the backup database, the Swing tends to hold on to old data with the backup data. The user gets notified that the program will restart for the data to take effect. restarting the Swing helps the backends to apply the new database without having any null Exceptions or OS issues(File being used by another program). The event handling it calls the backend for the transfer and then uses the dispose() the Swing ends there and when the backend is done, the Swing starts up. – Chris Toerien Feb 19 '19 at 20:28
  • I have been able to reload the image and the JProgressBar to display,... using a simple println(), I can see the getValue() increasing as I want it too, but visually the GUI of the JProgressBar is not increasing. – Chris Toerien Feb 19 '19 at 20:32
  • @ChrisToerien: Then that's your event -- when the user chooses to load the database. Regardless, if you need more specific help, you're going to need to create and post your [mcve] one with `Thread.sleeps` to mock the long-running calls. – Hovercraft Full Of Eels Feb 20 '19 at 02:38