Java Swing progress bar for boot process

I use the Java function to download a file from the Internet.

public void getLatestRelease() { try { // Function called long startTime = System.currentTimeMillis(); // Open connection System.out.println("Connecting..."); URL url = new URL(latestReleaseUrl); url.openConnection(); // Download routine InputStream reader = url.openStream(); FileOutputStream writer = new FileOutputStream("release.zip"); byte[] buffer = new byte[153600]; int totalBytesRead = 0; int bytesRead = 0; while ((bytesRead = reader.read(buffer)) > 0) { writer.write(buffer, 0, bytesRead); buffer = new byte[153600]; totalBytesRead += bytesRead; } // Download finished long endTime = System.currentTimeMillis(); // Output download information System.out.println("Done."); System.out.println((new Integer(totalBytesRead).toString()) + " bytes read."); System.out.println("It took " + (new Long(endTime - startTime).toString()) + " milliseconds."); // Close input and output streams writer.close(); reader.close(); } // Here I catch MalformedURLException and IOException :) } 

And I have a JProgressBar component in my JPanel , which should visualize the download progress:

 private static void createProgressBar(JPanel panel) { JProgressBar progressBar = new JProgressBar(0, 100); progressBar.setValue(0); progressBar.setStringPainted(true); panel.add(progressBar, BorderLayout.SOUTH); } 

I would like to separate the "back-end" functions from the "front-end" views presented to users, similar to MVC in web applications.

So, the getLatestRelease() function lies in the framework package in the MyFramework class.

Everything related to the generation of the Swing interface, including event listeners, is in the frontend package.

In the main Controller class, I create an instance of MyFramework and an instance of ApplicationFrontend , which is the main class of the frontend package.

Questions: how to update the progressBar value depending on the progress of the download?

+6
source share
1 answer

when you want to make MVC a swing, the SwingWorker class comes to mind.
SwingWorker has a "progress" property that you can listen to with the PropertyChangeListener .

Progress events can be triggered by swingworker using the setProgress(int 0-100) method setProgress(int 0-100) . Thus, here, to download a file in the background, progress is implied (note that you will need to have an idea of ​​the size of the file so that it can calculate the percentage of progress).

Progress display can be done using two options: a JProgressBar for full control or ProgressMonitor to display an almost self-managing pop-up window with a progress bar. See the tutorial to see the differences.

Solution 1

As the saying goes, if you go to ProgressMonitor and the background task is read using InputStream , you can use the ProgressMonitorInputStream class to read and display progress without worrying about calling setProgress or listening to the "progress" property.

Decision 2

If you want to do this manually, create your SwingWorker download task, which calls setProgress as it starts, if necessary create a ProgressMonitor (or JProgressBar), register a PropertyChangeListener on your SwingWorker, which checks the progress changes and updates the monitor / bar accordingly.

Note. It is important to go through the PropertyChangeListener because it separates the model (task) from the view (the swing progress component) and complies with the rules for using EDT.

+7
source

Source: https://habr.com/ru/post/922751/


All Articles