diff --git a/modules/swing/src/main/java/javafx/embed/swing/JFXPanel.java b/modules/swing/src/main/java/javafx/embed/swing/JFXPanel.java --- a/modules/swing/src/main/java/javafx/embed/swing/JFXPanel.java +++ b/modules/swing/src/main/java/javafx/embed/swing/JFXPanel.java @@ -195,10 +195,8 @@ // Initialize FX runtime when the JFXPanel instance is constructed private synchronized static void initFx() { // Note that calling PlatformImpl.startup more than once is OK - PlatformImpl.startup(new Runnable() { - @Override public void run() { - // No need to do anything here - } + PlatformImpl.startup(() -> { + // No need to do anything here }); } @@ -254,12 +252,9 @@ setSceneImpl(newScene); } else { final CountDownLatch initLatch = new CountDownLatch(1); - Platform.runLater(new Runnable() { - @Override - public void run() { - setSceneImpl(newScene); - initLatch.countDown(); - } + Platform.runLater(() -> { + setSceneImpl(newScene); + initLatch.countDown(); }); try { initLatch.await(); @@ -717,19 +712,13 @@ } } - private final AWTEventListener ungrabListener = new AWTEventListener() { - @Override - public void eventDispatched(AWTEvent event) { - if (event instanceof sun.awt.UngrabEvent) { - SwingFXUtils.runOnFxThread(new Runnable() { - @Override - public void run() { - if (JFXPanel.this.stagePeer != null) { - JFXPanel.this.stagePeer.focusUngrab(); - } - } - }); - } + private final AWTEventListener ungrabListener = event -> { + if (event instanceof sun.awt.UngrabEvent) { + SwingFXUtils.runOnFxThread(() -> { + if (JFXPanel.this.stagePeer != null) { + JFXPanel.this.stagePeer.focusUngrab(); + } + }); } }; @@ -743,21 +732,16 @@ super.addNotify(); registerFinishListener(); - AccessController.doPrivileged(new PrivilegedAction() { - public Void run() { - JFXPanel.this.getToolkit().addAWTEventListener(ungrabListener, - sun.awt.SunToolkit.GRAB_EVENT_MASK); - return null; - } + AccessController.doPrivileged((PrivilegedAction) () -> { + JFXPanel.this.getToolkit().addAWTEventListener(ungrabListener, + SunToolkit.GRAB_EVENT_MASK); + return null; }); updateComponentSize(); // see RT-23603 - SwingFXUtils.runOnFxThread(new Runnable() { - @Override - public void run() { - if ((stage != null) && !stage.isShowing()) { - stage.show(); - sendMoveEventToFX(); - } + SwingFXUtils.runOnFxThread(() -> { + if ((stage != null) && !stage.isShowing()) { + stage.show(); + sendMoveEventToFX(); } }); } @@ -776,12 +760,9 @@ * chain of parent components are removed. */ @Override public void removeNotify() { - SwingFXUtils.runOnFxThread(new Runnable() { - @Override - public void run() { - if ((stage != null) && stage.isShowing()) { - stage.hide(); - } + SwingFXUtils.runOnFxThread(() -> { + if ((stage != null) && stage.isShowing()) { + stage.hide(); } }); @@ -791,11 +772,9 @@ super.removeNotify(); - AccessController.doPrivileged(new PrivilegedAction() { - public Void run() { - JFXPanel.this.getToolkit().removeAWTEventListener(ungrabListener); - return null; - } + AccessController.doPrivileged((PrivilegedAction) () -> { + JFXPanel.this.getToolkit().removeAWTEventListener(ungrabListener); + return null; }); /* see CR 4867453 */ @@ -828,26 +807,20 @@ } scenePeer = embeddedScene; if (scenePeer == null) { - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - dnd.removeNotify(); - dnd = null; - } + SwingUtilities.invokeLater(() -> { + dnd.removeNotify(); + dnd = null; }); return; } if (pWidth > 0 && pHeight > 0) { scenePeer.setSize(pWidth, pHeight); } - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - dnd = new SwingDnD(JFXPanel.this, scenePeer); - dnd.addNotify(); - if (scenePeer != null) { - scenePeer.setDragStartListener(dnd.getDragStartListener()); - } + SwingUtilities.invokeLater(() -> { + dnd = new SwingDnD(JFXPanel.this, scenePeer); + dnd.addNotify(); + if (scenePeer != null) { + scenePeer.setDragStartListener(dnd.getDragStartListener()); } }); } @@ -877,12 +850,9 @@ @Override public void repaint() { - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - JFXPanel.this.repaint(); - } - }); + SwingUtilities.invokeLater(() -> { + JFXPanel.this.repaint(); + }); } @Override @@ -893,11 +863,8 @@ @Override public void setCursor(CursorFrame cursorFrame) { final Cursor cursor = getPlatformCursor(cursorFrame); - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - JFXPanel.this.setCursor(cursor); - } + SwingUtilities.invokeLater(() -> { + JFXPanel.this.setCursor(cursor); }); } @@ -919,14 +886,11 @@ @Override public boolean grabFocus() { - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - Window window = SwingUtilities.getWindowAncestor(JFXPanel.this); - if (window != null) { - if (JFXPanel.this.getToolkit() instanceof SunToolkit) { - ((SunToolkit)JFXPanel.this.getToolkit()).grab(window); - } + SwingUtilities.invokeLater(() -> { + Window window = SwingUtilities.getWindowAncestor(JFXPanel.this); + if (window != null) { + if (JFXPanel.this.getToolkit() instanceof SunToolkit) { + ((SunToolkit)JFXPanel.this.getToolkit()).grab(window); } } }); @@ -936,14 +900,11 @@ @Override public void ungrabFocus() { - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - Window window = SwingUtilities.getWindowAncestor(JFXPanel.this); - if (window != null) { - if (JFXPanel.this.getToolkit() instanceof SunToolkit) { - ((SunToolkit)JFXPanel.this.getToolkit()).ungrab(window); - } + SwingUtilities.invokeLater(() -> { + Window window = SwingUtilities.getWindowAncestor(JFXPanel.this); + if (window != null) { + if (JFXPanel.this.getToolkit() instanceof SunToolkit) { + ((SunToolkit)JFXPanel.this.getToolkit()).ungrab(window); } } }); diff --git a/modules/swing/src/main/java/javafx/embed/swing/SwingDnD.java b/modules/swing/src/main/java/javafx/embed/swing/SwingDnD.java --- a/modules/swing/src/main/java/javafx/embed/swing/SwingDnD.java +++ b/modules/swing/src/main/java/javafx/embed/swing/SwingDnD.java @@ -227,30 +227,22 @@ } HostDragStartListener getDragStartListener() { - return new HostDragStartListener() { - @Override - public void dragStarted(final EmbeddedSceneDSInterface dragSource, - final TransferMode dragAction) - { - assert Toolkit.getToolkit().isFxUserThread(); - assert dragSource != null; + return (dragSource, dragAction) -> { + assert Toolkit.getToolkit().isFxUserThread(); + assert dragSource != null; + + // The method is called from FX Scene just before entering + // nested event loop servicing DnD events. + // It should initialize DnD in AWT EDT. + SwingUtilities.invokeLater(() -> { + assert fxDragSource == null; + assert swingDragSource == null; + assert fxDropTarget == null; - // The method is called from FX Scene just before entering - // nested event loop servicing DnD events. - // It should initialize DnD in AWT EDT. - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - assert fxDragSource == null; - assert swingDragSource == null; - assert fxDropTarget == null; - - fxDragSource = dragSource; - startDrag(me, dndTransferable, dragSource. - getSupportedActions(), dragAction); - } - }); - } + fxDragSource = dragSource; + startDrag(me, dndTransferable, dragSource. + getSupportedActions(), dragAction); + }); }; } diff --git a/modules/swing/src/main/java/javafx/embed/swing/SwingFXUtils.java b/modules/swing/src/main/java/javafx/embed/swing/SwingFXUtils.java --- a/modules/swing/src/main/java/javafx/embed/swing/SwingFXUtils.java +++ b/modules/swing/src/main/java/javafx/embed/swing/SwingFXUtils.java @@ -230,10 +230,8 @@ @Override public boolean enter() { if (isRunning.compareAndSet(false, true)) { - PlatformImpl.runAndWait(new Runnable() { - @Override public void run() { - Toolkit.getToolkit().enterNestedEventLoop(FwSecondaryLoop.this); - } + PlatformImpl.runAndWait(() -> { + Toolkit.getToolkit().enterNestedEventLoop(FwSecondaryLoop.this); }); return true; } @@ -242,10 +240,8 @@ @Override public boolean exit() { if (isRunning.compareAndSet(true, false)) { - PlatformImpl.runAndWait(new Runnable() { - @Override public void run() { - Toolkit.getToolkit().exitNestedEventLoop(FwSecondaryLoop.this, null); - } + PlatformImpl.runAndWait(() -> { + Toolkit.getToolkit().exitNestedEventLoop(FwSecondaryLoop.this, null); }); return true; } @@ -269,11 +265,7 @@ private static EventQueue getEventQueue() { return AccessController.doPrivileged( - new PrivilegedAction() { - @Override public EventQueue run() { - return java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue(); - } - }); + (PrivilegedAction) () -> java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue()); } //Called with reflection from PlatformImpl to avoid dependency diff --git a/modules/swing/src/main/java/javafx/embed/swing/SwingNode.java b/modules/swing/src/main/java/javafx/embed/swing/SwingNode.java --- a/modules/swing/src/main/java/javafx/embed/swing/SwingNode.java +++ b/modules/swing/src/main/java/javafx/embed/swing/SwingNode.java @@ -142,11 +142,8 @@ setEventHandler(KeyEvent.ANY, new SwingKeyEventHandler()); setEventHandler(ScrollEvent.SCROLL, new SwingScrollEventHandler()); - focusedProperty().addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observable, Boolean oldValue, final Boolean newValue) { - activateLwFrame(newValue); - } + focusedProperty().addListener((observable, oldValue, newValue) -> { + activateLwFrame(newValue); }); //Workaround for RT-34170 @@ -168,11 +165,8 @@ public void setContent(final JComponent content) { this.content = content; - SwingFXUtils.runOnEDT(new Runnable() { - @Override - public void run() { - setContentImpl(content); - } + SwingFXUtils.runOnEDT(() -> { + setContentImpl(content); }); } @@ -206,20 +200,14 @@ lwFrame.addWindowFocusListener(new WindowFocusListener() { @Override public void windowGainedFocus(WindowEvent e) { - SwingFXUtils.runOnFxThread(new Runnable() { - @Override - public void run() { - SwingNode.this.requestFocus(); - } + SwingFXUtils.runOnFxThread(() -> { + SwingNode.this.requestFocus(); }); } @Override public void windowLostFocus(WindowEvent e) { - SwingFXUtils.runOnFxThread(new Runnable() { - @Override - public void run() { - ungrabFocus(true); - } + SwingFXUtils.runOnFxThread(() -> { + ungrabFocus(true); }); } }); @@ -228,14 +216,11 @@ lwFrame.setSize((int)width, (int)height); lwFrame.setVisible(true); - SwingFXUtils.runOnFxThread(new Runnable() { - @Override - public void run() { - locateLwFrame(); // initialize location + SwingFXUtils.runOnFxThread(() -> { + locateLwFrame(); // initialize location - if (focusedProperty().get()) { - activateLwFrame(true); - } + if (focusedProperty().get()) { + activateLwFrame(true); } }); } @@ -251,11 +236,8 @@ final int w, final int h, final int linestride) { - Runnable r = new Runnable() { - @Override - public void run() { - peer.setImageBuffer(IntBuffer.wrap(data), x, y, w, h, linestride); - } + Runnable r = () -> { + peer.setImageBuffer(IntBuffer.wrap(data), x, y, w, h, linestride); }; if (peer != null) { SwingFXUtils.runOnFxThread(r); @@ -269,11 +251,8 @@ * Called on EDT */ void setImageBounds(final int x, final int y, final int w, final int h) { - Runnable r = new Runnable() { - @Override - public void run() { - peer.setImageBounds(x, y, w, h); - } + Runnable r = () -> { + peer.setImageBounds(x, y, w, h); }; if (peer != null) { SwingFXUtils.runOnFxThread(r); @@ -286,12 +265,9 @@ * Called on EDT */ void repaintDirtyRegion(final int dirtyX, final int dirtyY, final int dirtyWidth, final int dirtyHeight) { - Runnable r = new Runnable() { - @Override - public void run() { - peer.repaintDirtyRegion(dirtyX, dirtyY, dirtyWidth, dirtyHeight); - impl_markDirty(DirtyBits.NODE_CONTENTS); - } + Runnable r = () -> { + peer.repaintDirtyRegion(dirtyX, dirtyY, dirtyWidth, dirtyHeight); + impl_markDirty(DirtyBits.NODE_CONTENTS); }; if (peer != null) { SwingFXUtils.runOnFxThread(r); @@ -322,12 +298,9 @@ this.height = height; impl_geomChanged(); impl_markDirty(DirtyBits.NODE_GEOMETRY); - SwingFXUtils.runOnEDT(new Runnable() { - @Override - public void run() { - if (lwFrame != null) { - lwFrame.setSize((int) width, (int) height); - } + SwingFXUtils.runOnEDT(() -> { + if (lwFrame != null) { + lwFrame.setSize((int) width, (int) height); } }); } @@ -400,42 +373,30 @@ return true; } - private final InvalidationListener locationListener = new InvalidationListener() { - @Override - public void invalidated(Observable observable) { - locateLwFrame(); + private final InvalidationListener locationListener = observable -> { + locateLwFrame(); + }; + + private final EventHandler ungrabHandler = event -> { + if (!skipBackwardUnrgabNotification) { + AccessController.doPrivileged(new PostEventAction(new UngrabEvent(lwFrame))); } }; - private final EventHandler ungrabHandler = new EventHandler() { - @Override - public void handle(FocusUngrabEvent event) { - if (!skipBackwardUnrgabNotification) { - AccessController.doPrivileged(new PostEventAction(new UngrabEvent(lwFrame))); - } + private final ChangeListener windowVisibleListener = (observable, oldValue, newValue) -> { + if (!newValue) { + disposeLwFrame(); + } else { + setContent(content); } }; - private final ChangeListener windowVisibleListener = new ChangeListener() { - @Override - public void changed(ObservableValue observable, Boolean oldValue, Boolean newValue) { - if (!newValue) { - disposeLwFrame(); - } else { - setContent(content); - } + private final ChangeListener sceneWindowListener = (observable, oldValue, newValue) -> { + if (oldValue != null) { + removeWindowListeners(oldValue); } - }; - - private final ChangeListener sceneWindowListener = new ChangeListener() { - @Override - public void changed(ObservableValue observable, Window oldValue, Window newValue) { - if (oldValue != null) { - removeWindowListeners(oldValue); - } - if (newValue != null) { - addWindowListeners(newValue); - } + if (newValue != null) { + addWindowListeners(newValue); } }; @@ -482,29 +443,23 @@ addSceneListeners(getScene()); } - sceneProperty().addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observable, Scene oldValue, Scene newValue) { - if (oldValue != null) { - // Removed from scene - removeSceneListeners(oldValue); - disposeLwFrame(); + sceneProperty().addListener((observable, oldValue, newValue) -> { + if (oldValue != null) { + // Removed from scene + removeSceneListeners(oldValue); + disposeLwFrame(); + } + if (newValue != null) { + // Added to another scene + if (content != null && lwFrame == null) { + setContent(content); } - if (newValue != null) { - // Added to another scene - if (content != null && lwFrame == null) { - setContent(content); - } - addSceneListeners(newValue); - } + addSceneListeners(newValue); } }); - impl_treeVisibleProperty().addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observable, Boolean oldValue, Boolean newValue) { - setLwFrameVisible(newValue); - } + impl_treeVisibleProperty().addListener((observable, oldValue, newValue) -> { + setLwFrameVisible(newValue); }); return peer; @@ -537,13 +492,10 @@ final int sceneX = (int)getScene().getX(); final int sceneY = (int)getScene().getY(); - SwingFXUtils.runOnEDT(new Runnable() { - @Override - public void run() { - if (lwFrame != null) { - lwFrame.setLocation(windowX + sceneX + (int) loc.getX(), - windowY + sceneY + (int) loc.getY()); - } + SwingFXUtils.runOnEDT(() -> { + if (lwFrame != null) { + lwFrame.setLocation(windowX + sceneX + (int) loc.getX(), + windowY + sceneY + (int) loc.getY()); } }); } @@ -552,12 +504,9 @@ if (lwFrame == null) { return; } - SwingFXUtils.runOnEDT(new Runnable() { - @Override - public void run() { - if (lwFrame != null) { - lwFrame.emulateActivation(activate); - } + SwingFXUtils.runOnEDT(() -> { + if (lwFrame != null) { + lwFrame.emulateActivation(activate); } }); } @@ -566,13 +515,10 @@ if (lwFrame == null) { return; } - SwingFXUtils.runOnEDT(new Runnable() { - @Override - public void run() { - if (lwFrame != null) { - lwFrame.dispose(); - lwFrame = null; - } + SwingFXUtils.runOnEDT(() -> { + if (lwFrame != null) { + lwFrame.dispose(); + lwFrame = null; } }); } @@ -581,12 +527,9 @@ if (lwFrame == null) { return; } - SwingFXUtils.runOnEDT(new Runnable() { - @Override - public void run() { - if (lwFrame != null) { - lwFrame.setVisible(visible); - } + SwingFXUtils.runOnEDT(() -> { + if (lwFrame != null) { + lwFrame.setVisible(visible); } }); } @@ -634,67 +577,49 @@ } @Override public void focusGrabbed() { - SwingFXUtils.runOnFxThread(new Runnable() { - @Override - public void run() { - if (getScene() != null && - getScene().getWindow() != null && - getScene().getWindow().impl_getPeer() != null) { - getScene().getWindow().impl_getPeer().grabFocus(); - grabbed = true; - } + SwingFXUtils.runOnFxThread(() -> { + if (getScene() != null && + getScene().getWindow() != null && + getScene().getWindow().impl_getPeer() != null) { + getScene().getWindow().impl_getPeer().grabFocus(); + grabbed = true; } }); } @Override public void focusUngrabbed() { - SwingFXUtils.runOnFxThread(new Runnable() { - @Override - public void run() { - ungrabFocus(false); - } + SwingFXUtils.runOnFxThread(() -> { + ungrabFocus(false); }); } @Override public void preferredSizeChanged(final int width, final int height) { - SwingFXUtils.runOnFxThread(new Runnable() { - @Override - public void run() { - SwingNode.this.prefWidth = width; - SwingNode.this.prefHeight = height; - SwingNode.this.impl_notifyLayoutBoundsChanged(); - } - }); + SwingFXUtils.runOnFxThread(() -> { + SwingNode.this.prefWidth = width; + SwingNode.this.prefHeight = height; + SwingNode.this.impl_notifyLayoutBoundsChanged(); + }); } @Override public void maximumSizeChanged(final int width, final int height) { - SwingFXUtils.runOnFxThread(new Runnable() { - @Override - public void run() { - SwingNode.this.maxWidth = width; - SwingNode.this.maxHeight = height; - SwingNode.this.impl_notifyLayoutBoundsChanged(); - } - }); + SwingFXUtils.runOnFxThread(() -> { + SwingNode.this.maxWidth = width; + SwingNode.this.maxHeight = height; + SwingNode.this.impl_notifyLayoutBoundsChanged(); + }); } @Override public void minimumSizeChanged(final int width, final int height) { - SwingFXUtils.runOnFxThread(new Runnable() { - @Override - public void run() { - SwingNode.this.minWidth = width; - SwingNode.this.minHeight = height; - SwingNode.this.impl_notifyLayoutBoundsChanged(); - } - }); + SwingFXUtils.runOnFxThread(() -> { + SwingNode.this.minWidth = width; + SwingNode.this.minHeight = height; + SwingNode.this.impl_notifyLayoutBoundsChanged(); + }); } public void setCursor(Cursor cursor) { - SwingFXUtils.runOnFxThread(new Runnable() { - @Override - public void run() { - SwingNode.this.setCursor(SwingCursors.embedCursorToCursor(cursor)); - } + SwingFXUtils.runOnFxThread(() -> { + SwingNode.this.setCursor(SwingCursors.embedCursorToCursor(cursor)); }); } }