SwingNode doesn't respond to Swing component resize?

Brian Bolstad bbolstad at ikonscience.com
Thu Apr 28 18:03:36 UTC 2016

Thanks for the response, Stijn.

Even without setting the size directly and with the revalidate call, I am still unable to get the SwingNode to resize.  The problem to me seems to be that a SwingNode caches the size constraints of its Swing component, but those caches don’t get refreshed when the size constraints of the Swing component are changed, even when a layout pass is performed.  There are listeners for the size constraint properties (down in JLightweightFrame), but as nearly as I can tell, these only get called when you first set the content, or when a component is added or removed from the LW frame.

I’ve found that if I explicitly call SwingNode.setContent(component) each time I update the size constraints of component, then the SwingNode will resize, but that is wasteful (destroys and recreates the LW frame) and shouldn’t be necessary.

There may be a concept that I’m completely missing here, but this feels like a bug with SwingNode.


From: Stijn Debruyckere [mailto:Stijn.Debruyckere at luciad.com]
Sent: Friday, April 22, 2016 9:56 AM
To: Brian Bolstad
Cc: openjfx-dev at openjdk.java.net
Subject: Re: SwingNode doesn't respond to Swing component resize?

You should call component.revalidate() after changing it's preferred size I believe (didn't check your program though).

If that doesn't help, you could check if you maybe tampered with the Direct3D settings:

// Verify that Direct3D is enabled (the default setting), to avoid resize issues.

if ( Boolean.valueOf(System.getProperty("sun.java2d.noddraw", "false")) ||

     !Boolean.valueOf(System.getProperty("sun.java2d.d3d", "true")) ) {

      throw new IllegalStateException("Do not change the Direct3D default settings (Windows specific), "

                                      + "it may cause glitches during frame re-sizing.");


Note: you shouldn't call setSize directly on Swing components, layout managers should take care of that.

Kind regards,

From: "Brian Bolstad" <bbolstad at ikonscience.com<mailto:bbolstad at ikonscience.com>>
To: openjfx-dev at openjdk.java.net<mailto:openjfx-dev at openjdk.java.net>
Sent: Monday, April 18, 2016 10:08:10 PM
Subject: SwingNode doesn't respond to Swing component resize?

Hello everyone,

In the SwingNode#resize(double, double) documentation, it states:

"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."

However, I'm not seeing this behavior-the SwingNode doesn't resize for me unless I remove and re-add the Swing component to the SwingNode after resizing.

In the implementation, the private class SwingNodeContent has methods like preferredSizeChanged that I assume should be called when the Swing component size constraints are modified.  However, it looks to me like JLightweightFrame only installs pref/max/min size property change listeners on components added after the first one.  So, SwingNode never gets notified of the size changes and never updates it's cached values for the swing component's sizes and never updates its layoutBounds.

Below is a sample that demonstrates the problem I'm seeing; if you click the button, it will double its size, but the enclosing SwingNode doesn't change size.  I'm running JavaFX 8u66 on Windows 10.

I'm very new to JavaFX, so what am I doing wrong?


import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

import javax.swing.*;
import java.awt.*;

public class SwingNodeResize extends Application
    public void start(Stage primaryStage)
        SwingNode swingNode = new SwingNode();


        StackPane root = new StackPane(new Rectangle(500, 500, Color.RED), swingNode);
        Scene scene = new Scene(root);

        primaryStage.setTitle("SwingNode Resize");

    private void init(final SwingNode node)
        SwingUtilities.invokeLater(() ->
            JButton button = new JButton("Click me!");
            button.addActionListener(event ->
                Dimension buttonSize = button.getSize();
                buttonSize.setSize(buttonSize.getWidth() * 2, buttonSize.getHeight() * 2);


                System.out.println("Button size: " + button.getPreferredSize() + "; " +
                                   "SwingNode size: " + new Dimension((int) node.prefWidth(-1), (int) node.prefHeight(-1)));


    public static void main(String[] args)

More information about the openjfx-dev mailing list