JavaFX and Scene Builder clip scene edges despite specifying USE_COMPUTED_SIZE
Asked Answered
A

1

6

I'm using Scene Builder (v11.0.0) to create FXML files for scenes in JavaFX (v12) but, despite instructing all containers to USE_COMPUTED_SIZE for the preferred widths and heights, the rendered scenes (as seen in Scene Builder and also when run as a JavaFX application which loads those FXML files) are being clipped at the right and bottom edges so that bits of nodes are chopped off.

And in Scene Builder it seems that the renderer must know that the scene won't fit the allowed bounds because the editor shows blue boundary markers which are clearly some way beyond the rendered rectangle.

View in Scene Builder

Screenshot of the view in Scene Builder, showing the form design has been clipped at the right and bottom edges, followed by a dark boundary showing where the content ought to be, and then blue boundary markers showing the extent of that boundary.

The view in Scene Builder shows that more space is needed at the bottom in order to give the buttons sufficient space (their bottom edge, and the lower edge of the TitledPane is missing). And more space is needed at the right in order to fit the right edges of the DatePicker and TitledPane. The blue boundary markers show clearly where the actual content ends, so it's not clear why the display area is being calculated to be several pixels shorter than this.

View of running Java application

Screenshot of the scene rendered within a Java application, the form design clipped at the right and bottom edges, very similar to the degree of clipping seen in Scene Builder.

Once the FXML files are used to populate a window in a JavaFX application, the same thing is seen: the calculated size for the window is a number of pixels too few to fit the whole scene correctly.

If the blue boundary markers have correctly been calculated to show that extra display area width and height are needed, how do I tell the FXML to require this additional space when rendering?

Is this a known bug/limitation in Scene Builder, FXML, or JavaFX. Or is there something more I need to do beyond just selecting USE_COMPUTED_SIZE for the preferred dimensions?

In order to make this explicit, see the example FXML below which displays the problem illustrated.

scene.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <TitledPane animated="false" collapsible="false" text="untitled">
        <content>
            <HBox>
               <children>
                  <fx:include source="subscene.fxml" />
               </children>
            </HBox>
        </content>
      </TitledPane>
      <TitledPane animated="false" collapsible="false" text="untitled">
        <content>
            <HBox>
               <children>
                  <fx:include source="subscene.fxml" />
               </children>
            </HBox>
        </content>
      </TitledPane>
      <TitledPane animated="false" collapsible="false" text="untitled">
        <content>
            <HBox alignment="BASELINE_RIGHT">
               <children>
                  <Button mnemonicParsing="false" text="Button" />
                  <Button mnemonicParsing="false" text="Button" />
               </children>
            </HBox>
        </content>
      </TitledPane>
   </children>
</VBox>

subscene.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>


<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label text="Label" />
      <DatePicker />
   </children>
</VBox>
Adduction answered 28/3, 2019 at 17:18 Comment(10)
Might be related to this issue? If that's not the case, can you post your FXML?Garlinda
If you run an application using the FXML file do you still get the same problem? If not, then the issue is likely on Scene Builder's end.Mennonite
Yes, the same thing is seen when the application is run in Java and the scenes are loaded from the FXML files generated by Scene Builder. I have a suspicion that the problem might be that the scenes fx:include other FXML files; maybe JavaFX has a bug which means it miscalculates some of the bounds when the scene includes nested sub-scenes. I'll see if I can reproduce this in a simple FXML example which I can share here.Adduction
Okay, @JoséPereda I found a compact MVCE which displays exactly the same problem I'm seeing in my full application, so I've updated my question to include the FXML and new screenshots.Adduction
It looks like the DatePicker control doesn't get its size properly. If you remove it or if you set any width different than -1, or you add another control, everything will work as it should. This seems like a JavaFX bug to me.Garlinda
hmm ... -infinity is use_pref_size (not use_computed_size), so the most inner vbox is constrained to its pref in every direction - replace with a real computed_size (that is -1) doesn't have the problem horizontally, but still vertically. Seems to be a bug in initial sizing of the window ..Plenty
When you look at the size in scene builder it says W:216 H:259 when you print it during execution post .show() it says W:200.0 H:263.0 and after running primaryStage.sizeToScene() it says W:216.0 H:281.0 very odd how they're all differentDoodle
@JoséPereda, do you know whether this bug has already been filed in the appropriate place? If not, where am I able to file a bug report (or is that only open to project developers as is the case with JDK bugs)?Adduction
The bug hasn't been filed yet. You can do it at the OpenJFX issue tracker. It helps posting a short code snippet that reproduces the issue, like in @Mennonite answer.Garlinda
Bug reported in new openjdk-jfx issue #435. Please let me know if that issue needs any further information or tweaks.Adduction
M
3

This does appear to be a bug in JavaFX, specifically DatePicker, as this simple example can reproduce the problem:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {   
        VBox root = new VBox(new DatePicker());

        // Problem shows up when using USE_COMPUTED_SIZE (the default) as well
        root.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
        root.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);

        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

}

Resulting in:

image showing improperly sized date picker parent

Note: It does not seem to matter what parent the DatePicker is put in. Nor does the problem appear with other controls.

A workaround to this issue appears to be calling Window.sizeToScene() after calling show(). I don't understand why that would make a difference, but it does. Unfortunately, this will only help in the real application, not in Scene Builder.

Mennonite answered 30/3, 2019 at 5:51 Comment(1)
Thank you. Seeing as your MVCE is more compact than mine, I've used this to open an openjdk-jfx issue on GitHub to report this bug.Adduction

© 2022 - 2024 — McMap. All rights reserved.