Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8117632

Concurrency Bug in SimpleSwingBrowser.java Example

XMLWordPrintable

      This concerns the SimpleSwingBrowser application used to demonstrate embedding a JavaFX component in a Swing application:

          http://docs.oracle.com/javafx/2/swing/SimpleSwingBrowser.java.htm

      I believe this example does not follow the dictum that all all Swing objects should be created or modified only on the Event Dispatching Thread (EDT). The usual way to achieve this is:

          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  createAndShowGUI();
              }
          });

       
      However, the SimpleSwingBrowser.java example does it this way:

          public static void main(String[] args) {
              SwingUtilities.invokeLater(new SimpleSwingBrowser());
          }
       

      SimpleSwingBrowser is a runnable, but the problem is that the evaluation of the "new SimpleSwingBrowser()" expression occurs on main's thread, and among other things, it involves the initialization of member fields which create Swing objects. For example:

          public class SimpleSwingBrowser implements Runnable {
              ...
              private JFrame frame = new JFrame();
              private JPanel panel = new JPanel(new BorderLayout());
              private JLabel lblStatus = new JLabel();

      While this will probably work anyway, it is not the best practice and should not be included in example code. This example is included in the following section of the JavaFX for Swing Developers manual: http://docs.oracle.com/javafx/2/swing/swing-fx-interoperability.htm
       

       

            ifedortsova Irina Fedortsova (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: