package mint.javafx; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javafx.application.Platform; import javafx.embed.swing.JFXPanel; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.Rectangle; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class JFXPanelResizeTest { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { initAndShowGUI(); } }); } private static void initAndShowGUI() { final JFrame f = new JFrame("Test JFXPanel resize"); f.setSize(600, 200); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setLayout(new FlowLayout()); final MyJFXPanel fxPanel = new MyJFXPanel(); JButton button = new JButton("Toggle Rectangle Width"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent paramActionEvent) { fxPanel.toggle(); } }); f.add(button); f.add(fxPanel); f.add(new JLabel("Label1")); f.setVisible(true); } private static class MyJFXPanel extends JFXPanel { private final Rectangle r = new Rectangle(160, 120); public MyJFXPanel() { Platform.runLater(new Runnable() { @Override public void run() { setScene(new Scene(new Group(r))); } }); } // Toggle rectangle width 120px / 160px. public void toggle() { Platform.runLater(new Runnable() { @Override public void run() { r.setWidth(r.getWidth() == 160 ? 120 : 160); Scene scene = getScene(); setScene(null); setScene(scene); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } invalidate(); } }); } } }