Sagir
Sagir

Reputation: 19

BorderPane overlaping content

I have child GridPane, I want if text will increase then text should not goes to out of gridPane, instead ScrollPane, I don't want see go Apple text out side of box.

  @Override
public void start(Stage primaryStage) throws Exception {

    String border = "-fx-border-color:red;";

    Text title=new Text("Machin");

    GridPane topLeft=new GridPane();
    topLeft.setMinSize(80, 5);
    topLeft.setMaxSize(80, 80);

    topLeft.setStyle(border);

    topLeft.add(title, 0, 0);

    for(int i=1;i<=15;i++) {
        topLeft.add(new Text("Apple"), 0,i);
    }


    GridPane root = new GridPane();
    root.setAlignment(Pos.CENTER);

    root.setHgap(20);
    root.setVgap(20);

    root.add(topLeft, 0, 0);



    BorderPane borderPane = new BorderPane();
    borderPane.setTop(new Label("Header"));
    borderPane.setCenter(root);
    borderPane.setBottom(new Label("Buttom"));


    primaryStage.setScene(new Scene(borderPane, 600, 400));
    primaryStage.show();
}

Image:

enter image description here

Upvotes: 1

Views: 70

Answers (1)

jewelsea
jewelsea

Reputation: 159586

Rather than a layout pane like a GridPane, you can use some of the more sophisticated controls like ListView or TableView to enclose your content. Those controls have in-built scrollbars which will show when the content of the controls is larger than the viewable area. Review the linked tutorials to assess whether those other controls are applicable for your application.

If you wish to keep with a GridPane, then you can wrap the GridPane in a ScrollPane to allow the content of the GridPane to be scrolled if it reaches a certain size, such as in the sample below:

scrollable grid

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class ScrollingGrid extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        String border = "-fx-border-color:red;";
        Text title = new Text("Machin");

        GridPane appleGrid = new GridPane();
        appleGrid.setMinSize(80, 5);
        appleGrid.setStyle(border);
        appleGrid.add(title, 0, 0);

        for (int i = 1; i <= 15; i++) {
            appleGrid.add(new Text("Apple"), 0, i);
        }

        ScrollPane scrollPane = new ScrollPane(appleGrid);
        scrollPane.setPrefViewportWidth(80);
        scrollPane.setPrefViewportHeight(80);
        scrollPane.setMaxSize(ScrollPane.USE_PREF_SIZE, ScrollPane.USE_PREF_SIZE);

        BorderPane borderPane = new BorderPane();
        borderPane.setTop(new Label("Header"));
        borderPane.setCenter(scrollPane);
        borderPane.setBottom(new Label("Bottom"));

        primaryStage.setScene(new Scene(borderPane, 600, 400));
        primaryStage.show();
    }

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

Upvotes: 6

Related Questions