Java Swingworker: not as an encapsulated class

I am having problems communicating, updating progress, and indicating done with the SwingWorker class, which is not an encapsulated class .

I have a simple class that processes files and directories on a hard drive. The user clicks the Start button and launches the SwingWorker instance.

I would like to print the names of the files that are being processed on JTextArea in an event-driven thread from SwingWorker, how to update the progress bar. All examples on the Internet are related to a nested class, and a nested class refers to variables in an external class (for example, a method done). I would also like to tell the Driven Thread event stream that SwingWorker will be completed so that the EDT can perform actions such as turning on the Start button (and the cleanup field).

Here are my questions: 1. How does the SwingWorker class put text in the JTextArea Driven event stream and update the progress bar?

  1. How does the EDT determine when the {external} SwingWorker thread is complete?

{I don't want SwingWorker to be a nested class because a lot of code (and processing) has been done.}

+3
source share
1 answer

A SwingWorkeris still a class that you can distribute and pass on in any information required to do its job, regardless of whether it has been encapsulated in another class. Thus, you can pass JTextAreaas the "target text area" in the constructor, save it as a member variable, and then update the text area in the method process(List<V>).

EDT , : , , doInBackground() . , , doInBackground(), done() EDT. ( , .)

done() , - - , -, -.

. SwingWorker , . .

+2

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


All Articles