Module javafx.swing
Package javafx.embed.swing

# Class JFXPanel

java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javafx.embed.swing.JFXPanel

All Implemented Interfaces:
  `ImageObserver`, `MenuContainer`, `Serializable`

public class JFXPanel extends JComponent
`JFXPanel` is a component to embed JavaFX content into Swing applications. The content to be displayed is specified with the `setScene(javafx.scene.Scene)` method that accepts an instance of JavaFX `Scene`. After the scene is assigned, it gets repainted automatically. All the input and focus events are forwarded to the scene transparently to the developer.

There are some restrictions related to `JFXPanel`. As a Swing component, it should only be accessed from the event dispatch thread, except the `setScene(javafx.scene.Scene)` method, which can be called either on the event dispatch thread or on the JavaFX application thread.

Here is a typical pattern how `JFXPanel` can used:

         public class Test {

             private static void initAndShowGUI() {
                 // This method is invoked on Swing thread
                 JFrame frame = new JFrame("FX");
                 final JFXPanel fxPanel = new JFXPanel();
                 frame.add(fxPanel);
                 frame.setVisible(true);

                 Platform.runLater(new Runnable() {
                     @Override
                     public void run() {
                         initFX(fxPanel);
                     }
                 });
             }

             private static void initFX(JFXPanel fxPanel) {
                 // This method is invoked on JavaFX thread
                 Scene scene = createScene();
                 fxPanel.setScene(scene);
             }

             public static void main(String[] args) {
                 SwingUtilities.invokeLater(new Runnable() {
                     @Override
                     public void run() {
                         initAndShowGUI();
                     }
                 });
             }
         }

Since:
  JavaFX 2.0
See Also:
  - Serialized Form

- ## Nested Class Summary

  ## Nested classes/interfaces declared in class javax.swing.JComponent

  `JComponent.AccessibleJComponent`

  ## Nested classes/interfaces declared in class java.awt.Container

  `Container.AccessibleAWTContainer`

  ## Nested classes/interfaces declared in class java.awt.Component

  `Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy`

- ## Field Summary

  ### Fields declared in class javax.swing.JComponent

  `listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW`

  ### Fields declared in class java.awt.Component

  `accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT`

  ### Fields declared in interface java.awt.image.ImageObserver

  `ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `JFXPanel()`
  Creates a new `JFXPanel` object.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addNotify()`
  Notifies this component that it now has a parent component.
  `Dimension`
  `getPreferredSize()`
  Returns the preferred size of this `JFXPanel`, either previously set with `JComponent.setPreferredSize(Dimension)` or based on the content of the JavaFX scene attached to this `JFXPanel`.
  `Scene`
  `getScene()`
  Returns the JavaFX scene attached to this `JFXPanel`.
  `final boolean`
  `isOpaque()`
  `JFXPanel`'s opacity is controlled by the JavaFX content which is displayed in this component, so this method overrides `JComponent.isOpaque()` to always return a `false` value.
  `protected void`
  `paintComponent(Graphics g)`
  Overrides the `JComponent.paintComponent(Graphics)` method to paint the content of the JavaFX scene attached to this `JFXpanel`.
  `protected void`
  `processComponentEvent(ComponentEvent e)`
  Overrides the `Component.processComponentEvent(ComponentEvent)` method to dispatch `ComponentEvent.COMPONENT_RESIZED` events to the JavaFX scene attached to this `JFXPanel`.
  `protected void`
  `processFocusEvent(FocusEvent e)`
  Overrides the `Component.processFocusEvent(FocusEvent)` method to dispatch focus events to the JavaFX scene attached to this `JFXPanel`.
  `protected void`
  `processHierarchyBoundsEvent(HierarchyEvent e)`
  Overrides the `Component.processHierarchyBoundsEvent(HierarchyEvent)` method to process `HierarchyEvent.ANCESTOR_MOVED` events and update the JavaFX scene location to match the `JFXPanel` location on the screen.
  `protected void`
  `processKeyEvent(KeyEvent e)`
  Overrides the `Component.processKeyEvent(KeyEvent)` method to dispatch the key event to the JavaFX scene attached to this `JFXPanel`.
  `protected void`
  `processMouseEvent(MouseEvent e)`
  Overrides the `Component.processMouseEvent(MouseEvent)` method to dispatch the mouse event to the JavaFX scene attached to this `JFXPanel`.
  `protected void`
  `processMouseMotionEvent(MouseEvent e)`
  Overrides the `Component.processMouseMotionEvent(MouseEvent)` method to dispatch the mouse motion event to the JavaFX scene attached to this `JFXPanel`.
  `protected void`
  `processMouseWheelEvent(MouseWheelEvent e)`
  Overrides the `Component.processMouseWheelEvent(MouseWheelEvent)` method to dispatch the mouse wheel event to the JavaFX scene attached to this `JFXPanel`.
  `void`
  `removeNotify()`
  Notifies this component that it no longer has a parent component.
  `final void`
  `setOpaque(boolean opaque)`
  `JFXPanel`'s opacity is controlled by the JavaFX content which is displayed in this component, so this method overrides `JComponent.setOpaque(boolean)` to only accept a `false` value.
  `void`
  `setScene(Scene newScene)`
  Attaches a `Scene` object to display in this `JFXPanel`.

  ### Methods declared in class javax.swing.JComponent

  `addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUI, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI`

  ### Methods declared in class java.awt.Container

  `add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree`

  ### Methods declared in class java.awt.Component

  `action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processHierarchyEvent, processInputMethodEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle`

  ### Methods declared in class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### JFXPanel

    public JFXPanel()
    Creates a new `JFXPanel` object.

    Implementation note: when the first `JFXPanel` object is created, it implicitly initializes the JavaFX runtime. This is the preferred way to initialize JavaFX in Swing.

- ## Method Details

  - ### getScene

    public Scene getScene()
    Returns the JavaFX scene attached to this `JFXPanel`.
    Returns:
      the `Scene` attached to this `JFXPanel`

  - ### setScene

    public void setScene(Scene newScene)
    Attaches a `Scene` object to display in this `JFXPanel`. This method can be called either on the event dispatch thread or the JavaFX application thread.
    Parameters:
      `newScene` - a scene to display in this `JFXpanel`
    See Also:
      - `EventQueue.isDispatchThread()`
      - `Platform.isFxApplicationThread()`

  - ### setOpaque

    public final void setOpaque(boolean opaque)
    `JFXPanel`'s opacity is controlled by the JavaFX content which is displayed in this component, so this method overrides `JComponent.setOpaque(boolean)` to only accept a `false` value. If this method is called with a `true` value, no action is performed.
    Overrides:
      `setOpaque` in class `JComponent`
    Parameters:
      `opaque` - must be `false`

  - ### isOpaque

    public final boolean isOpaque()
    `JFXPanel`'s opacity is controlled by the JavaFX content which is displayed in this component, so this method overrides `JComponent.isOpaque()` to always return a `false` value.
    Overrides:
      `isOpaque` in class `JComponent`
    Returns:
      a `false` value

  - ### processMouseEvent

    protected void processMouseEvent(MouseEvent e)
    Overrides the `Component.processMouseEvent(MouseEvent)` method to dispatch the mouse event to the JavaFX scene attached to this `JFXPanel`.
    Overrides:
      `processMouseEvent` in class `JComponent`
    Parameters:
      `e` - the mouse event to dispatch to the JavaFX scene

  - ### processMouseMotionEvent

    protected void processMouseMotionEvent(MouseEvent e)
    Overrides the `Component.processMouseMotionEvent(MouseEvent)` method to dispatch the mouse motion event to the JavaFX scene attached to this `JFXPanel`.
    Overrides:
      `processMouseMotionEvent` in class `JComponent`
    Parameters:
      `e` - the mouse motion event to dispatch to the JavaFX scene

  - ### processMouseWheelEvent

    protected void processMouseWheelEvent(MouseWheelEvent e)
    Overrides the `Component.processMouseWheelEvent(MouseWheelEvent)` method to dispatch the mouse wheel event to the JavaFX scene attached to this `JFXPanel`.
    Overrides:
      `processMouseWheelEvent` in class `Component`
    Parameters:
      `e` - the mouse wheel event to dispatch to the JavaFX scene

  - ### processKeyEvent

    protected void processKeyEvent(KeyEvent e)
    Overrides the `Component.processKeyEvent(KeyEvent)` method to dispatch the key event to the JavaFX scene attached to this `JFXPanel`.
    Overrides:
      `processKeyEvent` in class `JComponent`
    Parameters:
      `e` - the key event to dispatch to the JavaFX scene

  - ### processComponentEvent

    protected void processComponentEvent(ComponentEvent e)
    Overrides the `Component.processComponentEvent(ComponentEvent)` method to dispatch `ComponentEvent.COMPONENT_RESIZED` events to the JavaFX scene attached to this `JFXPanel`. The JavaFX scene object is then resized to match the `JFXPanel` size.
    Overrides:
      `processComponentEvent` in class `Component`
    Parameters:
      `e` - the component event to dispatch to the JavaFX scene

  - ### processHierarchyBoundsEvent

    protected void processHierarchyBoundsEvent(HierarchyEvent e)
    Overrides the `Component.processHierarchyBoundsEvent(HierarchyEvent)` method to process `HierarchyEvent.ANCESTOR_MOVED` events and update the JavaFX scene location to match the `JFXPanel` location on the screen.
    Overrides:
      `processHierarchyBoundsEvent` in class `Component`
    Parameters:
      `e` - the hierarchy bounds event to process

  - ### processFocusEvent

    protected void processFocusEvent(FocusEvent e)
    Overrides the `Component.processFocusEvent(FocusEvent)` method to dispatch focus events to the JavaFX scene attached to this `JFXPanel`.
    Overrides:
      `processFocusEvent` in class `Component`
    Parameters:
      `e` - the focus event to dispatch to the JavaFX scene

  - ### paintComponent

    protected void paintComponent(Graphics g)
    Overrides the `JComponent.paintComponent(Graphics)` method to paint the content of the JavaFX scene attached to this `JFXpanel`.
    Overrides:
      `paintComponent` in class `JComponent`
    Parameters:
      `g` - the Graphics context in which to paint
    See Also:
      - `isOpaque()`

  - ### getPreferredSize

    public Dimension getPreferredSize()
    Returns the preferred size of this `JFXPanel`, either previously set with `JComponent.setPreferredSize(Dimension)` or based on the content of the JavaFX scene attached to this `JFXPanel`.
    Overrides:
      `getPreferredSize` in class `JComponent`
    Returns:
      prefSize this `JFXPanel` preferred size

  - ### addNotify

    public void addNotify()
    Notifies this component that it now has a parent component. When this method is invoked, the chain of parent components is set up with KeyboardAction event listeners.
    Overrides:
      `addNotify` in class `JComponent`

  - ### removeNotify

    public void removeNotify()
    Notifies this component that it no longer has a parent component. When this method is invoked, any KeyboardActions set up in the the chain of parent components are removed.
    Overrides:
      `removeNotify` in class `JComponent`