Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

GridPane layout problems in Java 8

Martin ValentaMar 24 2014 — edited Mar 25 2014

Hi, can anyone explain why the following example has different outputs in Java 7 and Java 8? This example simple shows what kind of problem I am facing in my JavaFX application when I run it using Java 8. Is the problem really that 33.3 + 33.3 + 33.3 != 100 but Java 7 didn't mind? The following example results in small window using Java 7 and very wide window using Java 8.

{code}

import java.util.ArrayList;

import java.util.List;

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.control.Label;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.ColumnConstraints;

import javafx.scene.layout.GridPane;

import javafx.stage.Stage;

public class BetpalFXApplication extends Application {

    @Override

    public void start(Stage stage) throws Exception {

        GridPane root = createGrid();

        stage.setScene(new Scene(root));

        stage.show();

    }

    private GridPane createGrid() {

        BorderPane left = new BorderPane();

        left.setStyle("-fx-background-color: red");

        left.setCenter(new Label("left"));

        BorderPane middle = new BorderPane();

        middle.setStyle("-fx-background-color: yellow");

        middle.setCenter(new Label("middle"));

        BorderPane right = new BorderPane();

        right.setStyle("-fx-background-color: blue");

        right.setCenter(new Label("right"));

        GridPane root = new GridPane();

        double[] widthPercentColumnConstraintsThree = { 33.3, 33.3, 33.3 };

        setColumnConstraints(root, widthPercentColumnConstraintsThree);

        root.add(left, 0, 0);

        root.add(middle, 1, 0);

        root.add(right, 2, 0);

        return root;

    }

    private void setColumnConstraints(GridPane gridPane, double[] widthPercentColumnConstraints) {

        List<ColumnConstraints> listOfColumnConstraints = new ArrayList<ColumnConstraints>();

        for (double widthPercentColumnConstraint : widthPercentColumnConstraints) {

            ColumnConstraints columnConstraints = new ColumnConstraints();

            columnConstraints.setPercentWidth(widthPercentColumnConstraint);

            listOfColumnConstraints.add(columnConstraints);

        }

        gridPane.getColumnConstraints().setAll(listOfColumnConstraints);

    }

    public static void main(String[] args) {

        Application.launch(args);

    }

}

{code}

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 22 2014
Added on Mar 24 2014
3 comments
473 views