Reputation: 7128
I am trying to determine the best way to pass data and updates between swing components and threads. Below is an example of what I am faced with
This is the main windows that the user interacts with, it holds a JTabbedPanel
with a single JPanel
as a tab of the tabbed panel. It also holds a JTextPane
that is used to show the user some information.
public class MainView extends JFrame
{
JTextPane m_jtOutput;
JTabbedPane m_jtpTabs;
JSortPanel m_jpSortTab;
public LinkedList<DataPoint> data;
DataPoint curPoint;
public MainView()
{
m_jtpTabs = new JTabbedPane();
m_jpSortTab = new JSortPanel(this);
m_jtpTabs.addTab("Sort", m_jpSortTab);
this.add(m_jtpTabs);
this.add(m_jtOutPut);
}
public void newStatus(String inStat)
{
m_jtOutput.setText(inStat);
}
public DataPoint getCurPoint()
{
return curPoint;
}
}
This is the JPanel that is shown in the JTabbedPanel
within the main view. It has a worker thread that does some sorting on the data. It also writes some updates to the main views output text pane.
public class JSortPanel extends JPanel
{
JButton doSort;
MainView mainVw;
SortThread sortThread;
public JSortPanel(MainView inView)
{
mainVw = inView;
this.add(doSort); // also get the listener hooked up
sortThread = new SortThread(mainView);
}
public void doSortClicked(EventArgs e)
{
sortThread.start();
mainView.newStatus("Started sorting");
}
}
This is the sorting thread. It initially reads the data from the main view and then sorts it, periodically writing some information to the main views text pane about the state of the sorting.
public class SortThread extends Thread
{
MainView mainVw;
LinkedList<DataPoint> data;
public SortThread(MainView inView)
{
mainVw = inView;
System.out.println("Cur data " + mainView.getCurData());
}
public void run()
{
data = mainVw.data;
while(true)
{
doASortingStep(data);
mainVw.newStatus("Did a sorting step");
}
}
}
My questions are.
Can JSortPanel
call mainView.newStatus()
safely? Can it only call it in the scope of an event handler, such as when a user clicks the doSort
button?
Can SortThread
call mainView.newStatus()
safely? Would that call be on the Swing EDT?
Can SortThread
call mainView.getCurData()
and get the return value safely, I assume it is also either on the EDT or not based on the answer to the previous question?
If all calls are on the EDT, can you give an example of a call that would not be on the EDT?
If either or both JSortPanel
and SortThread
cannot call mainView.newStatus(..)
safely, then how can they?
Upvotes: 0
Views: 489
Reputation: 35011
Before a component is visualized on the screen / other device, it is generally safe for multi-threading. After a component is shown, all changes should be done on the UIThread via a SwingWorker or SwingUtilities.invokeLater
Upvotes: 1