2

None of the following components have min, max or preferred heights unless otherwise specified: I have a JPanel with a BorderLayout. At the CENTER of the BorderLayout there is a JPanel. At PAGE_START there is a JFXPanel. The JFXPanel has been set with a scene comprising a BorderPane which in turn contains another BorderPane (with a min, max and preferred height defined by FXML) and a FlowPane.

When elements are added to the FlowPane, however, the JFXPanel does not resize. The height of the FlowPane remains as the sum of its top and bottom padding - presumably since this is its correct height when the scene is first set in the JFXPanel.

Consequently, the elements of the FlowPane are cut off (or hidden completely after wrapping). (If I manually set the JFXPanel to be much bigger, I can see that the FlowPane is correctly resizing, so this is not the problem.)

Why is my JFXPanel not resizing? Are JFXPanels simply not designed to resize with their Scene, or is some other method call required (e.g. invalidate())? Or is the source of the problem one of the LayoutManagers?

MCVE

The following code produces a frame that looks like this:

enter image description here

Whereas, if I add the elements to the FlowPane before setting the JFXPanel's scene, it looks like this (which is desirable):

enter image description here

Obviously, in the real code, new elements are being added to and removed from the FlowPane as we go along, so cannot wait until after to set the scene. Want the JFXPanel to dynamically resize as the FlowPane does.

public static void main(String[] args)
{
    JFXPanel topPanel = new JFXPanel();

    JPanel centerPanel = new JPanel();
    centerPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN));

    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(topPanel, BorderLayout.PAGE_START);
    mainPanel.add(centerPanel, BorderLayout.CENTER);

    JFrame frame = new JFrame("Main Frame");
    frame.setContentPane(mainPanel);
    frame.setUndecorated(true);
    frame.setSize(new Dimension(300, 300));

    BorderPane root = new BorderPane();
    BorderPane borderPane = new BorderPane();
    borderPane.setMinHeight(100);
    borderPane.setMaxHeight(100);
    borderPane.setStyle("-fx-border-color: red;");

    FlowPane flowPane = new FlowPane();
    flowPane.setStyle("-fx-border-color: blue;");

    root.setCenter(borderPane);
    root.setBottom(flowPane);

    Platform.runLater(() -> topPanel.setScene(new Scene(root)));

    SwingUtilities.invokeLater(() -> frame.setVisible(true));

    // Now add some elements to the FlowPane
    Platform.runLater(() ->
    {
        for (int i = 0; i < 5; i++)
        {
            flowPane.getChildren().add(new PinkSquare());
        }
    });
}

private static class PinkSquare extends HBox
{
    private PinkSquare()
    {
        super();
        setMinHeight(50);
        setMinWidth(50);
        setStyle("-fx-background-color: pink;");
    }
}
UtterlyConfused
  • 983
  • 1
  • 10
  • 18

0 Answers0