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() {