Working on a large scale SWT-based application I just stumbled upon some code using the AWT/Swing bridge which totally confused me and made me think about the implications of using two GUI threads.
public void createContent(final String html) {
// Bridge to AWT
frame = SWT_AWT.new_Frame(this);
rootPane = new JPanel();
rootPane.setLayout(new GridBagLayout());
JRootPane rp = new JRootPane();
rp.getContentPane().add(rootPane);
rp.validate();
frame.add(rp);
frame.validate();
// Create components in AWT user interface thread (deadlock prevention)
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
initializeLookAndFeel();
initializeToolbar();
initializeHTMLEditor();
setHTML(html, false);
}
});
rootPane.repaint();
rootPane.validate();
}
Without going into detail, as you might have guessed a lot of Swing elements are added to the "bridge frame" inside the initialization methods.
What confuses me in this case is the invocation of the AWT event dispatcher thread (EDT) for creation of the Swing components. I would just have added all GUI elements inside the SWT UI thread. I am not sure why it's preferable to split the GUI creation between both threads.
Probably, someone can elaborate on what happens behind the scenes. Especially on the interaction of both threads using the bridge. Why or when would it make sense to dispatch creation of AWT stuff to the EDT like in the code example?