George
George

Reputation: 1283

is there a grid layout in javafx that wraps?

I was trying to search for the answer to my question but I am not really even sure what to google for to find the answer - so here I am on stackoverflow.

In my app I am using java 1.8 with javafx for the GUI. I want to add N number of checkboxes onto a stage and each checkbox has a label on it. The labels can be any number of characters in length. The checkboxes will be added by the user as my application runs. I want to allow more than 1 checkbox on each row. So basically I want to have a table or grid layout so all the checkboxes are aligned in a grid. It should fill in the first row and once all horizontal space is taken up it should start a new row.

Now lets say the first row has 8 checkboxes and then the second row starts to get filled in. If something on row 2 has a really long label maybe only 6 checkboxes will fit on the second row. I cant have 8 columns in the first row and 6 columns in the second row and still have everything aligned. So now I have to change the number of columns in the grid to 6 (moving 2 checkboxes from the first row into the second row) and re-layout everything.

Is there any layout or control that handles this? FYI - I don't want to allow horizontal scrollbars.

Upvotes: 0

Views: 729

Answers (2)

Spotted
Spotted

Reputation: 4091

Using a TilePane should fit your needs. The implentation of a TilePane guarantees the same number of tiles on each row (excepting the last row).

Here's a quick example that shows the behaviour:

package application;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;

public class Main extends Application {

private String alphabet = "abcdefghijklmnopqrstuvwxyz";
private Label lastLabel = new Label("");

@Override
public void start(Stage primaryStage) {
    try {
        VBox root = new VBox();
        Button btnAdd = new Button("Add label");
        final TilePane tilePane = new TilePane();

        btnAdd.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                Label lbl = new Label(lastLabel.getText()
                        + alphabet.charAt(lastLabel.getText().length()));
                tilePane.getChildren().add(lbl);
                lastLabel = lbl;
            }
        });

        root.getChildren().add(btnAdd);
        root.getChildren().add(tilePane);
        Scene scene = new Scene(root, 400, 400);
        scene.getStylesheets().add(
                getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
    }
}

You must know that if a checkbox contains a very long text (relatively to your windows width), it could reduce dramatically the number of columns of your TilePane !

Also, it is a good practice to wrap your TilePane into a ScrollPane (allowing only vertical scrollbar) to avoid unreachable elements when the TilePane's width comes smaller.

Upvotes: 1

James_D
James_D

Reputation: 209418

I think both TilePane and FlowPane come close to what you describe; the resize behavior might be slightly different to what you are looking for.

Upvotes: 0

Related Questions