public class JFXPanel
extends javax.swing.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();
}
});
}
}
| Modifier and Type | Class and Description |
|---|---|
private class |
JFXPanel.HostContainer |
javax.swing.JComponent.AccessibleJComponent| Modifier and Type | Field and Description |
|---|---|
private java.util.concurrent.atomic.AtomicInteger |
disableCount |
private SwingDnD |
dnd |
private static PlatformImpl.FinishListener |
finishListener |
private static boolean |
fxInitialized |
private JFXPanel.HostContainer |
hostContainer |
private static java.util.concurrent.atomic.AtomicInteger |
instanceCount |
private boolean |
isCapturingMouse |
private static sun.util.logging.PlatformLogger |
log |
private float |
opacity |
private int |
pHeight |
private java.awt.image.BufferedImage |
pixelsIm |
private int |
pPreferredHeight |
private int |
pPreferredWidth |
private int |
pWidth |
private int |
scaleFactor |
private Scene |
scene |
private EmbeddedSceneInterface |
scenePeer |
private int |
screenX |
private int |
screenY |
private EmbeddedWindow |
stage |
private EmbeddedStageInterface |
stagePeer |
private java.awt.event.AWTEventListener |
ungrabListener |
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW| Constructor and Description |
|---|
JFXPanel()
Creates a new
JFXPanel object. |
| Modifier and Type | Method and Description |
|---|---|
void |
addNotify()
Notifies this component that it now has a parent component.
|
private void |
deregisterFinishListener() |
private int |
getDefaultScale(sun.java2d.SurfaceData surfaceData) |
java.awt.im.InputMethodRequests |
getInputMethodRequests() |
java.awt.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. |
private static void |
initFx() |
private void |
invokeOnClientEDT(java.lang.Runnable r) |
private boolean |
isFxEnabled() |
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(java.awt.Graphics g)
Overrides the
JComponent.paintComponent(Graphics)
method to paint the content of the JavaFX scene attached to this
JFXpanel. |
protected void |
processComponentEvent(java.awt.event.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(java.awt.event.FocusEvent e)
Overrides the
Component.processFocusEvent(FocusEvent)
method to dispatch focus events to the JavaFX scene attached to this
JFXPanel. |
protected void |
processHierarchyBoundsEvent(java.awt.event.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 |
processHierarchyEvent(java.awt.event.HierarchyEvent e) |
protected void |
processInputMethodEvent(java.awt.event.InputMethodEvent e) |
protected void |
processKeyEvent(java.awt.event.KeyEvent e)
Overrides the
Component.processKeyEvent(KeyEvent)
method to dispatch the key event to the JavaFX scene attached to this
JFXPanel. |
protected void |
processMouseEvent(java.awt.event.MouseEvent e)
Overrides the
Component.processMouseEvent(MouseEvent)
method to dispatch the mouse event to the JavaFX scene attached to this
JFXPanel. |
protected void |
processMouseMotionEvent(java.awt.event.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(java.awt.event.MouseWheelEvent e)
Overrides the
Component.processMouseWheelEvent(MouseWheelEvent)
method to dispatch the mouse wheel event to the JavaFX scene attached
to this JFXPanel. |
private void |
registerFinishListener() |
void |
removeNotify()
Notifies this component that it no longer has a parent component.
|
private void |
resizePixelBuffer(int newScaleFactor) |
private void |
sendFocusEventToFX(java.awt.event.FocusEvent e) |
private void |
sendInputMethodEventToFX(java.awt.event.InputMethodEvent e) |
private void |
sendKeyEventToFX(java.awt.event.KeyEvent e) |
private void |
sendMouseEventToFX(java.awt.event.MouseEvent e) |
private void |
sendMoveEventToFX() |
private void |
sendResizeEventToFX() |
private void |
setFxEnabled(boolean enabled) |
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. |
private void |
setSceneImpl(Scene newScene) |
private void |
updateComponentSize() |
private boolean |
updateScreenLocation() |
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, 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, updateUIadd, 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, validateTreeaction, 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, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, 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, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycleprivate static final sun.util.logging.PlatformLogger log
private static java.util.concurrent.atomic.AtomicInteger instanceCount
private static PlatformImpl.FinishListener finishListener
private JFXPanel.HostContainer hostContainer
private volatile EmbeddedWindow stage
private volatile Scene scene
private SwingDnD dnd
private EmbeddedStageInterface stagePeer
private EmbeddedSceneInterface scenePeer
private int pWidth
private int pHeight
private int scaleFactor
private volatile int pPreferredWidth
private volatile int pPreferredHeight
private volatile int screenX
private volatile int screenY
private java.awt.image.BufferedImage pixelsIm
private volatile float opacity
private java.util.concurrent.atomic.AtomicInteger disableCount
private boolean isCapturingMouse
private static boolean fxInitialized
private final java.awt.event.AWTEventListener ungrabListener
public JFXPanel()
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.
private void registerFinishListener()
private void deregisterFinishListener()
private static void initFx()
public Scene getScene()
JFXPanel.Scene attached to this JFXPanelpublic void setScene(Scene newScene)
Scene object to display in this JFXPanel. This method can be called either on the event
dispatch thread or the JavaFX application thread.newScene - a scene to display in this JFXpanelEventQueue.isDispatchThread(),
Platform.isFxApplicationThread()private void setSceneImpl(Scene newScene)
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.setOpaque in class javax.swing.JComponentopaque - must be falsepublic 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.isOpaque in class javax.swing.JComponentfalse valueprivate void sendMouseEventToFX(java.awt.event.MouseEvent e)
protected void processMouseEvent(java.awt.event.MouseEvent e)
Component.processMouseEvent(MouseEvent)
method to dispatch the mouse event to the JavaFX scene attached to this
JFXPanel.processMouseEvent in class javax.swing.JComponente - the mouse event to dispatch to the JavaFX sceneprotected void processMouseMotionEvent(java.awt.event.MouseEvent e)
Component.processMouseMotionEvent(MouseEvent)
method to dispatch the mouse motion event to the JavaFX scene attached to
this JFXPanel.processMouseMotionEvent in class javax.swing.JComponente - the mouse motion event to dispatch to the JavaFX sceneprotected void processMouseWheelEvent(java.awt.event.MouseWheelEvent e)
Component.processMouseWheelEvent(MouseWheelEvent)
method to dispatch the mouse wheel event to the JavaFX scene attached
to this JFXPanel.processMouseWheelEvent in class java.awt.Componente - the mouse wheel event to dispatch to the JavaFX sceneprivate void sendKeyEventToFX(java.awt.event.KeyEvent e)
protected void processKeyEvent(java.awt.event.KeyEvent e)
Component.processKeyEvent(KeyEvent)
method to dispatch the key event to the JavaFX scene attached to this
JFXPanel.processKeyEvent in class javax.swing.JComponente - the key event to dispatch to the JavaFX sceneprivate void sendResizeEventToFX()
protected void processComponentEvent(java.awt.event.ComponentEvent e)
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.processComponentEvent in class java.awt.Componente - the component event to dispatch to the JavaFX sceneprivate void updateComponentSize()
private boolean updateScreenLocation()
private void sendMoveEventToFX()
protected void processHierarchyBoundsEvent(java.awt.event.HierarchyEvent e)
Component.processHierarchyBoundsEvent(HierarchyEvent)
method to process HierarchyEvent.ANCESTOR_MOVED
events and update the JavaFX scene location to match the JFXPanel location on the screen.processHierarchyBoundsEvent in class java.awt.Componente - the hierarchy bounds event to processprotected void processHierarchyEvent(java.awt.event.HierarchyEvent e)
processHierarchyEvent in class java.awt.Componentprivate void sendFocusEventToFX(java.awt.event.FocusEvent e)
protected void processFocusEvent(java.awt.event.FocusEvent e)
Component.processFocusEvent(FocusEvent)
method to dispatch focus events to the JavaFX scene attached to this
JFXPanel.processFocusEvent in class java.awt.Componente - the focus event to dispatch to the JavaFX sceneprivate void resizePixelBuffer(int newScaleFactor)
protected void processInputMethodEvent(java.awt.event.InputMethodEvent e)
processInputMethodEvent in class java.awt.Componentprivate void sendInputMethodEventToFX(java.awt.event.InputMethodEvent e)
private int getDefaultScale(sun.java2d.SurfaceData surfaceData)
protected void paintComponent(java.awt.Graphics g)
JComponent.paintComponent(Graphics)
method to paint the content of the JavaFX scene attached to this
JFXpanel.paintComponent in class javax.swing.JComponentg - the Graphics context in which to paintisOpaque()public java.awt.Dimension getPreferredSize()
JFXPanel, either
previously set with JComponent.setPreferredSize(Dimension) or
based on the content of the JavaFX scene attached to this JFXPanel.getPreferredSize in class javax.swing.JComponentJFXPanel preferred sizeprivate boolean isFxEnabled()
private void setFxEnabled(boolean enabled)
public void addNotify()
addNotify in class javax.swing.JComponentpublic java.awt.im.InputMethodRequests getInputMethodRequests()
getInputMethodRequests in class java.awt.Componentpublic void removeNotify()
removeNotify in class javax.swing.JComponentprivate void invokeOnClientEDT(java.lang.Runnable r)