Top Description Inners Fields Constructors Methods
javafx.embed.swing

public Class SwingNode

extends AbstractNode
Class Inheritance
Imports
javax.swing.JComponent, .Timer, java.awt.AWTEvent, .EventQueue, .Toolkit, java.awt.event.InputEvent, .MouseWheelEvent, .WindowEvent, .WindowFocusListener, java.util.ArrayList, .HashSet, .List, .Set, java.util.concurrent.locks.ReentrantLock, javafx.beans.InvalidationListener, javafx.beans.value.ChangeListener, javafx.event.Event, .EventHandler, .EventType, javafx.geometry.Point2D, javafx.scene.Node, .Scene, javafx.scene.input.KeyCode, .KeyEvent, .MouseButton, .MouseEvent, .ScrollEvent, javafx.stage.Window, java.lang.ref.WeakReference, java.nio.IntBuffer, com.sun.javafx.embed.swing.Disposer, .DisposerRecord, .SwingNodeHelper, .SwingEvents, com.sun.javafx.geom.BaseBounds, com.sun.javafx.geom.transform.BaseTransform, com.sun.javafx.scene.AbstractNode, .DirtyBits, .NodeHelper, com.sun.javafx.sg.prism.NGExternalNode, .NGNode, com.sun.javafx.stage.FocusUngrabEvent, .WindowHelper, com.sun.javafx.tk.TKStage, com.sun.javafx.PlatformUtil, com.sun.javafx.embed.swing.newimpl.SwingNodeInteropN

This class is used to embed a Swing content into a JavaFX application. The content to be displayed is specified with the setContent method that accepts an instance of Swing JComponent. The hierarchy of components contained in the JComponent instance should not contain any heavyweight components, otherwise SwingNode may fail to paint it. The content gets repainted automatically. All the input and focus events are forwarded to the JComponent instance transparently to the developer.

Here is a typical pattern which demonstrates how SwingNode can be used:

    public class SwingFx extends Application {

        @Override
        public void start(Stage stage) {
            final SwingNode swingNode = new SwingNode();
            createAndSetSwingContent(swingNode);

            StackPane pane = new StackPane();
            pane.getChildren().add(swingNode);

            stage.setScene(new Scene(pane, 100, 50));
            stage.show();
        }

        private void createAndSetSwingContent(final SwingNode swingNode) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    swingNode.setContent(new JButton("Click me!"));
                }
            });
        }

        public static void main(String[] args) {
            launch(args);
        }
    }
Since
JavaFX 8.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private class
private static class
private class

Field Summary

Modifier and TypeField and Description
private volatile JComponent
private Timer
private WeakReference<T>
private double
private double
private boolean
private Window
private static boolean
private final InvalidationListener
private volatile Object
private final ReentrantLock
private NGExternalNode
private List<Runnable>
private DisposerRecord
private final ChangeListener<Window>
private boolean
private int
private int
private int
private int
private int
private int
private SwingNodeInteropN
private final EventHandler<FocusUngrabEvent>
private EventHandler<T extends Event>
private final ChangeListener<Boolean>

Constructor Summary

AccessConstructor and Description
public
SwingNode()

Constructs a new instance of SwingNode.

Method Summary

Modifier and TypeMethod and Description
private void
activateLwFrame(final boolean activate)

private void
addSceneListeners(final Scene scene)

private void
addWindowListeners(final Window window)

private void
private boolean
doComputeContains(double localX, double localY)

private BaseBounds
private NGNode
private void
public JComponent

Returns:

the Swing component attached to this SwingNode
getContent
()

Returns the JComponent instance attached to this SwingNode.

private final Object
private ReentrantLock
public boolean
isResizable()

Overrides javafx.scene.Node.isResizable.

Indicates whether this node is a type which can be resized by its parent.

private void
public double

Returns:

the maximum height that the node should be resized to during layout
maxHeight
(double width)

Overrides javafx.scene.Node.maxHeight.

Returns the SwingNode's maximum height for use in layout calculations.

public double

Returns:

the maximum width that the node should be resized to during layout
maxWidth
(double height)

Overrides javafx.scene.Node.maxWidth.

Returns the SwingNode's maximum width for use in layout calculations.

public double

Returns:

the minimum height that the node should be resized to during layout
minHeight
(double width)

Overrides javafx.scene.Node.minHeight.

Returns the SwingNode's minimum height for use in layout calculations.

public double

Returns:

the minimum width that the node should be resized to during layout
minWidth
(double height)

Overrides javafx.scene.Node.minWidth.

Returns the SwingNode's minimum width for use in layout calculations.

private void
private static void
public double

Returns:

the preferred height that the node should be resized to during layout
prefHeight
(double width)

Overrides javafx.scene.Node.prefHeight.

Returns the SwingNode's preferred height for use in layout calculations.

public double

Returns:

the preferred width that the node should be resized to during layout
prefWidth
(double height)

Overrides javafx.scene.Node.prefWidth.

Returns the SwingNode's preferred width for use in layout calculations.

private void
private void
pack-priv void
repaintDirtyRegion(final int dirtyX, final int dirtyY, final int dirtyWidth, final int dirtyHeight)

public void
resize(final double
the target layout bounds width
width
,
final double
the target layout bounds height
height
)

Overrides javafx.scene.Node.resize.

Invoked by the SwingNode's parent during layout to set the SwingNode's width and height.

public void
setContent(final JComponent
a Swing component to display in this SwingNode
content
)

Attaches a JComponent instance to display in this SwingNode.

private void
pack-priv void
setImageBounds(final int x, final int y, final int w, final int h)

pack-priv void
setImageBuffer(final int[] data, final int x, final int y, final int w, final int h, final int linestride, final double scaleX, final double scaleY)

private void
setLwFrameScale(final double scaleX, final double scaleY)

private void
setLwFrameVisible(final boolean visible)

private void
ungrabFocus(boolean postUngrabEvent)

Field Detail

contentback to summary
private volatile JComponent content
deactivateback to summary
private Timer deactivate
disposerRecRefback to summary
private WeakReference<T> disposerRecRef
fxHeightback to summary
private double fxHeight
fxWidthback to summary
private double fxWidth
grabbedback to summary
private boolean grabbed
hWindowback to summary
private Window hWindow
isThreadMergedback to summary
private static boolean isThreadMerged
locationListenerback to summary
private final InvalidationListener locationListener
lwFrameback to summary
private volatile Object lwFrame
paintLockback to summary
private final ReentrantLock paintLock
peerback to summary
private NGExternalNode peer

Hides javafx.scene.Node.peer.

peerRequestsback to summary
private List<Runnable> peerRequests
recback to summary
private DisposerRecord rec
sceneWindowListenerback to summary
private final ChangeListener<Window> sceneWindowListener
skipBackwardUnrgabNotificationback to summary
private boolean skipBackwardUnrgabNotification
swingMaxHeightback to summary
private int swingMaxHeight
swingMaxWidthback to summary
private int swingMaxWidth
swingMinHeightback to summary
private int swingMinHeight
swingMinWidthback to summary
private int swingMinWidth
swingPrefHeightback to summary
private int swingPrefHeight
swingPrefWidthback to summary
private int swingPrefWidth
swNodeIOPback to summary
private SwingNodeInteropN swNodeIOP
ungrabHandlerback to summary
private final EventHandler<FocusUngrabEvent> ungrabHandler
windowHiddenHandlerback to summary
private EventHandler<T extends Event> windowHiddenHandler
windowVisibleListenerback to summary
private final ChangeListener<Boolean> windowVisibleListener

Constructor Detail

SwingNodeback to summary
public SwingNode()

Constructs a new instance of SwingNode.

Method Detail

activateLwFrameback to summary
private void activateLwFrame(final boolean activate)
addSceneListenersback to summary
private void addSceneListeners(final Scene scene)
addWindowListenersback to summary
private void addWindowListeners(final Window window)
disposeLwFrameback to summary
private void disposeLwFrame()
doComputeContainsback to summary
private boolean doComputeContains(double localX, double localY)
doComputeGeomBoundsback to summary
private BaseBounds doComputeGeomBounds(BaseBounds bounds, BaseTransform tx)
doCreatePeerback to summary
private NGNode doCreatePeer()
doUpdatePeerback to summary
private void doUpdatePeer()

Hides javafx.scene.Node.doUpdatePeer.

getContentback to summary
public JComponent getContent()

Returns the JComponent instance attached to this SwingNode.

The method can be called either on the JavaFX Application thread or the Event Dispatch thread. Note however, that access to a Swing component must occur from the Event Dispatch thread according to the Swing threading restrictions.

Returns:JComponent

the Swing component attached to this SwingNode

See Also
java.awt.EventQueue#isDispatchThread(), javafx.application.Platform#isFxApplicationThread()
getLightweightFrameback to summary
private final Object getLightweightFrame()
getPaintLockback to summary
private ReentrantLock getPaintLock()
isResizableback to summary
public boolean isResizable()

Overrides javafx.scene.Node.isResizable.

Doc from javafx.scene.Node.isResizable.

Indicates whether this node is a type which can be resized by its parent. If this method returns true, then the parent will resize the node (ideally within its size range) by calling node.resize(width,height) during the layout pass. All Regions, Controls, and WebView are resizable classes which depend on their parents resizing them during layout once all sizing and CSS styling information has been applied.

If this method returns false, then the parent cannot resize it during layout (resize() is a no-op) and it should return its layoutBounds for minimum, preferred, and maximum sizes. Group, Text, and all Shapes are not resizable and hence depend on the application to establish their sizing by setting appropriate properties (e.g. width/height for Rectangle, text on Text, and so on). Non-resizable nodes may still be relocated during layout.

Annotations
@Override
locateLwFrameback to summary
private void locateLwFrame()
maxHeightback to summary
public double maxHeight(double width)

Overrides javafx.scene.Node.maxHeight.

Returns the SwingNode's maximum height for use in layout calculations. This value corresponds to the maximum height of the Swing component.

Returns:double

the maximum height that the node should be resized to during layout

Annotations
@Override
maxWidthback to summary
public double maxWidth(double height)

Overrides javafx.scene.Node.maxWidth.

Returns the SwingNode's maximum width for use in layout calculations. This value corresponds to the maximum width of the Swing component.

Returns:double

the maximum width that the node should be resized to during layout

Annotations
@Override
minHeightback to summary
public double minHeight(double width)

Overrides javafx.scene.Node.minHeight.

Returns the SwingNode's minimum height for use in layout calculations. This value corresponds to the minimum height of the Swing component.

Returns:double

the minimum height that the node should be resized to during layout

Annotations
@Override
minWidthback to summary
public double minWidth(double height)

Overrides javafx.scene.Node.minWidth.

Returns the SwingNode's minimum width for use in layout calculations. This value corresponds to the minimum width of the Swing component.

Returns:double

the minimum width that the node should be resized to during layout

Annotations
@Override
notifyNativeHandleback to summary
private void notifyNativeHandle(Window window)
postAWTEventback to summary
private static void postAWTEvent(AWTEvent event)
prefHeightback to summary
public double prefHeight(double width)

Overrides javafx.scene.Node.prefHeight.

Returns the SwingNode's preferred height for use in layout calculations. This value corresponds to the preferred height of the Swing component.

Returns:double

the preferred height that the node should be resized to during layout

Annotations
@Override
prefWidthback to summary
public double prefWidth(double height)

Overrides javafx.scene.Node.prefWidth.

Returns the SwingNode's preferred width for use in layout calculations. This value corresponds to the preferred width of the Swing component.

Returns:double

the preferred width that the node should be resized to during layout

Annotations
@Override
removeSceneListenersback to summary
private void removeSceneListeners(Scene scene)
removeWindowListenersback to summary
private void removeWindowListeners(final Window window)
repaintDirtyRegionback to summary
pack-priv void repaintDirtyRegion(final int dirtyX, final int dirtyY, final int dirtyWidth, final int dirtyHeight)
resizeback to summary
public void resize(final double width, final double height)

Overrides javafx.scene.Node.resize.

Invoked by the SwingNode's parent during layout to set the SwingNode's width and height. Applications should not invoke this method directly. If an application needs to directly set the size of the SwingNode, it should set the Swing component's minimum/preferred/maximum size constraints which will be propagated correspondingly to the SwingNode and it's parent will honor those settings during layout.

Parameters
width:double

the target layout bounds width

height:double

the target layout bounds height

Annotations
@Override
setContentback to summary
public void setContent(final JComponent content)

Attaches a JComponent instance to display in this SwingNode.

The method can be called either on the JavaFX Application thread or the Event Dispatch thread. Note however, that access to a Swing component must occur from the Event Dispatch thread according to the Swing threading restrictions.

Parameters
content:JComponent

a Swing component to display in this SwingNode

See Also
java.awt.EventQueue#isDispatchThread(), javafx.application.Platform#isFxApplicationThread()
setContentImplback to summary
private void setContentImpl(JComponent content)
setImageBoundsback to summary
pack-priv void setImageBounds(final int x, final int y, final int w, final int h)
setImageBufferback to summary
pack-priv void setImageBuffer(final int[] data, final int x, final int y, final int w, final int h, final int linestride, final double scaleX, final double scaleY)
setLwFrameScaleback to summary
private void setLwFrameScale(final double scaleX, final double scaleY)
setLwFrameVisibleback to summary
private void setLwFrameVisible(final boolean visible)
ungrabFocusback to summary
private void ungrabFocus(boolean postUngrabEvent)
javafx.embed.swing back to summary

private Class SwingNode.SwingKeyEventHandler

extends Object
implements EventHandler<KeyEvent>
Class Inheritance
All Implemented Interfaces
javafx.event.EventHandler, java.util.EventListener

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
handle(KeyEvent event)

Implements javafx.event.EventHandler.handle.

Invoked when a specific event of the type for which this handler is registered happens.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

SwingKeyEventHandlerback to summary
private SwingKeyEventHandler()

Method Detail

handleback to summary
public void handle(KeyEvent event)

Implements javafx.event.EventHandler.handle.

Doc from javafx.event.EventHandler.handle.

Invoked when a specific event of the type for which this handler is registered happens.

Annotations
@Override
javafx.embed.swing back to summary

private Class SwingNode.SwingMouseEventHandler

extends Object
implements EventHandler<MouseEvent>
Class Inheritance
All Implemented Interfaces
javafx.event.EventHandler, java.util.EventListener

Field Summary

Modifier and TypeField and Description
private final Set<MouseButton>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
handle(MouseEvent event)

Implements javafx.event.EventHandler.handle.

Invoked when a specific event of the type for which this handler is registered happens.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

mouseClickedAllowedback to summary
private final Set<MouseButton> mouseClickedAllowed

Constructor Detail

SwingMouseEventHandlerback to summary
private SwingMouseEventHandler()

Method Detail

handleback to summary
public void handle(MouseEvent event)

Implements javafx.event.EventHandler.handle.

Doc from javafx.event.EventHandler.handle.

Invoked when a specific event of the type for which this handler is registered happens.

Annotations
@Override
javafx.embed.swing back to summary

private Class SwingNode.SwingNodeWindowFocusListener

extends Object
implements WindowFocusListener
Class Inheritance
All Implemented Interfaces
java.awt.event.WindowFocusListener, java.util.EventListener

Field Summary

Modifier and TypeField and Description
private WeakReference<SwingNode>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
windowGainedFocus(WindowEvent e)

Implements java.awt.event.WindowFocusListener.windowGainedFocus.

Invoked when the Window is set to be the focused Window, which means that the Window, or one of its subcomponents, will receive keyboard events.

public void
windowLostFocus(WindowEvent e)

Implements java.awt.event.WindowFocusListener.windowLostFocus.

Invoked when the Window is no longer the focused Window, which means that keyboard events will no longer be delivered to the Window or any of its subcomponents.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

swingNodeRefback to summary
private WeakReference<SwingNode> swingNodeRef

Constructor Detail

SwingNodeWindowFocusListenerback to summary
pack-priv SwingNodeWindowFocusListener(SwingNode swingNode)

Method Detail

windowGainedFocusback to summary
public void windowGainedFocus(WindowEvent e)

Implements java.awt.event.WindowFocusListener.windowGainedFocus.

Doc from java.awt.event.WindowFocusListener.windowGainedFocus.

Invoked when the Window is set to be the focused Window, which means that the Window, or one of its subcomponents, will receive keyboard events.

Annotations
@Override
windowLostFocusback to summary
public void windowLostFocus(WindowEvent e)

Implements java.awt.event.WindowFocusListener.windowLostFocus.

Doc from java.awt.event.WindowFocusListener.windowLostFocus.

Invoked when the Window is no longer the focused Window, which means that keyboard events will no longer be delivered to the Window or any of its subcomponents.

Annotations
@Override
javafx.embed.swing back to summary

private Class SwingNode.SwingScrollEventHandler

extends Object
implements EventHandler<ScrollEvent>
Class Inheritance
All Implemented Interfaces
javafx.event.EventHandler, java.util.EventListener

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
handle(ScrollEvent event)

Implements javafx.event.EventHandler.handle.

Invoked when a specific event of the type for which this handler is registered happens.

private void
sendMouseWheelEvent(Object source, double fxX, double fxY, int swingModifiers, double delta)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

SwingScrollEventHandlerback to summary
private SwingScrollEventHandler()

Method Detail

handleback to summary
public void handle(ScrollEvent event)

Implements javafx.event.EventHandler.handle.

Doc from javafx.event.EventHandler.handle.

Invoked when a specific event of the type for which this handler is registered happens.

Annotations
@Override
sendMouseWheelEventback to summary
private void sendMouseWheelEvent(Object source, double fxX, double fxY, int swingModifiers, double delta)