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();
}
Run Code Online (Sandbox Code Playgroud)
Image:

您可以使用一些更复杂的控件(如ListView或TableView)来包围内容,而不是像GridPane这样的布局窗格。这些控件具有内置的滚动条,这些滚动条将在控件的内容大于可见区域时显示。查看链接的教程,以评估其他控件是否适用于您的应用程序。
如果您希望保留GridPane,则可以将其包装GridPane在ScrollPane中,以便在其内容GridPane达到一定大小时滚动其内容,例如下面的示例:
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);
}
}
Run Code Online (Sandbox Code Playgroud)