0

I have a program that builds the GUI in the constructor. I need a Thread separate from the EDT to run immediately after the object in question is constructed. Could anyone point me in the right direction?

SirensOfTitan
  • 799
  • 1
  • 7
  • 19
  • 1
    By "immediately after", do you mean that this process should be sequential? – mre Jul 13 '11 at 20:41
  • 1
    How do you want the thread to interact with the GUI? Or is it independent? – Jonas Jul 13 '11 at 20:44
  • It needs to modify data in a JTable. The Thread in question opens up a network connection over a _very_ slow protocol, so the GUI needs to display, then the thread needs to sequentially modify entries as data streams in from the network. So in essense: the process should be sequential. – SirensOfTitan Jul 13 '11 at 20:52

4 Answers4

2

I need a Thread separate from the EDT

Threads are separate from the EDT so all you do is create a Runnable and then start it.

You only have to worry if the Thread updates any GUI components. If this is the case then you may want to use a SwingWorker. Read the section from the Swing tutorial on Concurrency for more information.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

build your GUI an open a new window inside a new Runnable invoked called by: SwingUtilities.invokeLater

Pierre
  • 34,472
  • 31
  • 113
  • 192
1

What you want to use is a SwingWorker<T,V>. In the doInBackground method, open the connection and start fetching data. When you have enough data to update the gui, call the publish method. Implement the process method to update the gui with the new data from publish, and finally, implement the done method to notify the user when you're finished fetching data.

The Swing Worker is a generic, so when you construct it you need to provide two types: T and V. V is the type for the data passed between publish and process methods and T is the type returned by doInBackground and passed to done.

Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
1

you have two choises

1) wrap Thread into Runnable as demonstrated here

2) I am not very satisfy with plain SwingExecutor, then I use for that Executor and SwingWorker, monitored by PropertyChangeListener, example here,

please carrefully with number of threads started by Executor. Executor doesn't care if SwingWorker ends or not and there still exists Bug where is pretty possible to overload maximum (somewhere in API) simultaneous jobs live by Executor in same time.

by this reason is there possible implements PropertyChangeListener

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319