Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX and Scene Builder clip scene edges despite specifying USE_COMPUTED_SIZE

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>
like image 863
Bobulous Avatar asked Mar 28 '19 17:03

Bobulous


People also ask

How do I connect to JavaFX with Scene Builder?

In the Settings/Preferences dialog ( Ctrl+Alt+S ), select Languages & Frameworks | JavaFX. in the Path to SceneBuilder field. In the dialog that opens, select the Scene Builder application (executable file) on your computer and click OK. Apply the changes and close the dialog.

Is JavaFX a SceneBuilder?

Scene Builder is written as a JavaFX application, supported on Windows, Mac OS X and Linux. It is the perfect example of a full-fledge JavaFX desktop application. Scene Builder is packaged as a self contained application, which means it comes bundled with its own private copy of the JRE.

Does JavaFX support drag and drop?

Moreover, drag-and-drop can be implemented between a JavaFX application and a third-party (native) application such as Windows Explorer or a desktop. A drag-and-drop gesture happens as follows: The user click a mouse button on a gesture source, drags the mouse, and releases the mouse button on a gesture target.

What is ImageView in JavaFX?

The ImageView is a Node used for painting images loaded with Image class. This class allows resizing the displayed image (with or without preserving the original aspect ratio) and specifying a viewport into the source image for restricting the pixels displayed by this ImageView .


1 Answers

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.

like image 184
Slaw Avatar answered Nov 15 '22 05:11

Slaw