Search code examples
javaswingjframeswingworker

Use SwingWorker to initialize a JFrame I want to return


I have a simple static method that, when given a list of numbers has to create a JFrame holding a histogram.

I use a SwingWorker (the doInBackground() does the number crunching, the done() creates the frame and the ChartPanel).

Now I'd like to return a reference (or a Future) of the JFrame created in done() to whoever calls this static method. Is there a way to do so? I am saying returning a Future because obviously the JFrame only exists after the done() method is done.


Solution

  • As an alternative to waiting for done(), create your frame and chart panel on the EDT, publish() interim results and update your chart's dataset in process(). As shown in this example, the chart listens to changes in the dataset and should update automatically.