Swing and JavaFX thread merge
Werner Lehmann
lehmann at media-interactive.de
Mon Aug 12 05:51:00 PDT 2013
Hi,
coincidentally we were experiencing the exact same problem with the
combination 7u25, OSX, Webstart. Also, we arrived at pretty much the
same workaround.
Investigation showed that multiple Swing eventqueues were created in the
above configuration. This would cause threading issues (NPE), paint
issues (flicker etc), drag-drop issues.
See below for a testcase: JFrame with a JButton and an fx button. When
clicked, the former prints its current thread, while the later switches
to EDT first (invokeLater) and then also prints the thread. On Windows,
this turns out to be the same thread as expected. On WebStart 7u25 OSX
we are getting different AWT-EventQueue-X threads.
Werner
> public class TestSwingEDT extends JFrame{
>
> public static void main(final String[] args){
> SwingUtilities.invokeLater(new Runnable() {
> @Override
> public void run() {
> new TestSwingEDT().runTest(args);
> }
> });
> }
>
> private void runTest(String[] args){
>
> System.out.println("runTest in " + Thread.currentThread().getName());
>
> this.setDefaultCloseOperation(EXIT_ON_CLOSE);
> this.setSize(300, 200);
>
> final JFXPanel jfxPanel = new JFXPanel();
> Platform.runLater(new Runnable(){
> @Override
> public void run() {
> Button btn = new Button("click me");
> btn.onActionProperty().set(new EventHandler<ActionEvent>() {
> @Override public void handle(ActionEvent ae) {
> System.out.println("jfx button click in " + Thread.currentThread().getName());
> SwingUtilities.invokeLater(new Runnable() {
> @Override public void run() {
> System.out.println("invokeLater from jfx button click in " + Thread.currentThread().getName());
> }
> });
> }
> });
> HBox hbox = new HBox();
> hbox.getChildren().add(btn);
> jfxPanel.setScene(new Scene(hbox));
> }
> });
>
> JButton jbutton = new JButton("click me");
> jbutton.addActionListener(new ActionListener() {
> @Override public void actionPerformed(java.awt.event.ActionEvent e) {
> System.out.println("jbutton click in " + Thread.currentThread().getName());
> }
> });
>
> JPanel rootPanel = new JPanel();
> rootPanel.add(jbutton);
>
> this.getContentPane().add(jfxPanel, BorderLayout.NORTH);
> this.getContentPane().add(rootPanel, BorderLayout.CENTER);
>
> this.setVisible(true);
>
> Platform.runLater(new Runnable(){
> @Override
> public void run() {
> SwingUtilities.invokeLater(new Runnable() {
> @Override
> public void run() {
> System.out.println("invokeLater in Platform.runLater in " + Thread.currentThread().getName());
> }
> });
> }
> });
>
> }
> }
More information about the openjfx-dev
mailing list