I have a scene similar to the one shown with the fxml code below (made with SceneBuilder):
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="bpMain" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="600.0" minWidth="800.0" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<HBox AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<BorderPane>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</BorderPane>
<BorderPane layoutX="10.0" layoutY="10.0" prefHeight="200.0" prefWidth="200.0">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<top>
<Label text="List 1:" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</BorderPane.margin>
</Label>
</top>
<center>
<ListView minHeight="500.0" minWidth="-Infinity" prefHeight="500.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</ListView>
</center>
</BorderPane>
</children>
</VBox>
<BorderPane prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<right>
<VBox minWidth="100.0" BorderPane.alignment="CENTER">
<children>
<BorderPane>
<center>
<ListView minWidth="150.0" prefHeight="500.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets top="10.0" />
</BorderPane.margin>
</ListView>
</center>
<VBox.margin>
<Insets bottom="10.0" top="10.0" />
</VBox.margin>
<top>
<VBox alignment="CENTER" BorderPane.alignment="CENTER">
<children>
<Label text="List2:" />
</children>
</VBox>
</top>
</BorderPane>
</children>
</VBox>
</right>
<center>
<BorderPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<top>
<Label text="Label" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="15.0" left="5.0" right="5.0" top="5.0" />
</BorderPane.margin>
</Label>
</top>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<center>
<BorderPane fx:id="matrixBorderPane" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
<bottom>
<VBox alignment="CENTER" spacing="10.0" BorderPane.alignment="CENTER">
<children>
<HBox alignment="CENTER" spacing="10.0">
<children>
<Button fx:id="button5" mnemonicParsing="false" onAction="#show5matrix" text="5" />
<Button fx:id="button15" layoutX="171.0" layoutY="10.0" mnemonicParsing="false" onAction="#show15matrix" text="15" />
</children>
</HBox>
</children>
</VBox>
</bottom>
</BorderPane>
</center>
</BorderPane>
</children>
</HBox>
</children>
</AnchorPane>
</center>
</BorderPane>
I want to add a matrix to the matrixBorderPane
which is represented by TextFields
in the GridPane
. The code added below works prefect for me when matrix is small (see it when Button
with 5
clicked), but with bigger dimensions the values are not all visible (Button
with 15
):
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Matrix");
primaryStage.setScene(scene);
primaryStage.setMinHeight(750);
primaryStage.setMinWidth(550);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java
package sample;
import javafx.beans.binding.Bindings;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML public BorderPane matrixBorderPane;
@FXML public Button button5;
@FXML public Button button15;
private ScrollPane scrollPane = new ScrollPane();
////////////////////////////////////////////////
@FXML
public void show5matrix(ActionEvent e){
showMatrix(5);
}
@FXML
public void show15matrix(ActionEvent e) {
showMatrix(15);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
private void showMatrix(int size) {
GridPane matrixGrid = new GridPane();
StackPane gridHolder = new StackPane(matrixGrid);
scrollPane.setContent(gridHolder);
matrixGrid.setMinHeight(30*size);
matrixGrid.setMinWidth(30*size);
matrixGrid.setMaxHeight(30*size);
matrixGrid.setMaxWidth(50*size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
TextField tf = new TextField();
Double value = 1.22;
tf.setText(value.toString());
tf.setAlignment(Pos.CENTER);
tf.setEditable(true);
tf.setPrefHeight(Region.USE_COMPUTED_SIZE);
tf.setPrefWidth(Region.USE_COMPUTED_SIZE);
GridPane.setRowIndex(tf, i);
GridPane.setColumnIndex(tf, j);
matrixGrid.getChildren().add(tf);
}
}
//gridHolder.minWidthProperty().bind(Bindings.createDoubleBinding(() ->
//scrollPane.getViewportBounds().getWidth(), scrollPane.viewportBoundsProperty()));
//gridHolder.minHeightProperty().bind(Bindings.createDoubleBinding(() ->
//scrollPane.getViewportBounds().getHeight(), scrollPane.viewportBoundsProperty()));
//matrixBorderPane.setCenter(scrollPane);
matrixBorderPane.setCenter(matrixGrid); //to comment
}
}
Thus, I added a scrollPane
to the matrixBorderPane
and then my GridPane
called matrixGrid
was placed in the scrollPane
. I read here: How to center the content of a javafx 8 scrollpane that StackPane
might be helpful while positioning it in the centre, but it didn't work for me like I needed it to.
You can see results of changes by uncommenting the commented lines and commenting the line marked as //to comment
in the Controller.java
class.
What I want exactly is to make scrollPane
activate only if matrix is too big to fit the dimensions of matrixBorderPane
, and if it does, I want it to be placed in the very centre of matrixBorderPane
.
So I just want my matrixGrid
to be in the scrollPane
and be positioned in the centre of matrixBorderPane
all the time, no matter how big is the matrix, but if it's too big, let the scrollPane
do its work. So I want it to look exactly like while running code with three commented lines when matrix is small and to look like after clicking 15
(the same code) when matrix is big, but within the scrollPane
.