Basic Undefined JProgress Panel

I just want to have an undefined JProgressBar animate in the lower left corner of my frame when a long load is running.

I looked through many textbooks, none of which are clear to me. I just want it to be animated when the file loads in the background. Every time I tried this, it does not update the progress bar until the download is complete.

I need help finding out where to place my call ().

class MyFunClass extends JFrame { JProgressBar progressBar = new JProgressBar(); public void buttonClicked() { progressBar.setVisible(true); progressBar.setIndeterminate(true); SwingUtilities.invokeLater(new Runnable() { public void run() { progressBar.setIndeterminate(true); progressBar.setVisible(true); // Do I do my download() in here?? }}); // Do download() here??? progressBar.setVisible(false); } } 

Thanks in advance!



Decision

Edit: for those who have a similar problem for me in the future, this is the main solution for the main problem. This is not my code verbatim, but a general sketch. Inside buttonClicked() :

 public void buttonClicked() { class MyWorker extends SwingWorker(String, Object) { protected String doInBackground() { progressBar.setVisible(true); progressBar.setIndeterminate(true); // Do my downloading code return "Done." } protected void done() { progressBar.setVisible(false) } } new MyWorker().execute(); } 
+6
source share
1 answer

No background thread is created in your current code, but it is shown that you are trying to queue a Swing thread from a Swing thread that does not make sense for this problem (although sometimes there are times when you can do this, but again, not here) . The only way to succeed is to use a background thread. The standard tutorial on Oracle JProgressBar and Concurrency in Swing goes through all this.

Most importantly, you must update the JProgressBar from the Swing Thread, which will execute your lengthy process in the background thread, for example, using the SwingWorker object. There are too many details for us to view everything here, and therefore all I can do is provide a link, but we will be happy to help you understand the details after viewing the tutorials. Just check out the tutorials and come back with your specific questions if you're still stuck.

Change 1
You indicate:

Is it possible to create a new stream object inside the buttonClicked () function?

Yes, you can create a SwingWorker object inside the buttonClicked() method and execute it there.

The fact is that I have an API and a library of all the functionality with which I am developing a graphical interface, and it seems like a long decision to wrap this function call in a stream.

Sorry, but I have no idea what you're saying here, or what kind of problems you think streams will cause. The buttonClicked() method buttonClicked() work on EDT, and not in the background thread.

Also note that in most of my more complex Swing GUIs, I often upload files in another (model) object and create my SwingWorker in another object that is still (control) from the GUI object (view). It may seem harder to do it this way, but it's a lot easier to debug, maintain, and improve my program when I do it this way, especially when I use interfaces extensively to allow me to test all software components in isolation.

Edit 2
Some corrections in your solution. You have sent:

 public void buttonClicked() { class MyWorker extends SwingWorker(String, Object) { protected String runInBackground() { progressBar.setVisible(true); progressBar.setIndeterminate(true); // ... 

who has problems

  • it doInBackground() , not runInBackground()
  • but more importantly, you are invoking Swing calls from a background thread, which should never be executed (unless the call is thread safe and even then ...).

So change it:

 public void buttonClicked() { progressBar.setVisible(true); progressBar.setIndeterminate(true); class MyWorker extends SwingWorker<String, Void> { protected String doInBackground() { // ... 
+9
source

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


All Articles