I want the height of my gridView to get automatically adjusted while changing its width (resizing window), the gridView scrollBar doesn't show up and I get one scrollBar (from scrollPane) for the gridView and some additional nodes together. But for some reason the setPrefHeight()
within the changeListener has no effect.
Using gridView.setPrefHeight(1000);
for example in the initialize()
method of the the controller class has the desired effect.
MainApp.java
loads the FXML file and make the stage show up:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.IOException;
public class MainApp extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("View.fxml"));
try {
AnchorPane rootPane = loader.load();
Scene scene = new Scene(rootPane, 500, 300);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Controller.java
The controller class in that the gridView is initialized and where the changeListener is located:
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.paint.Color;
import javafx.util.Callback;
import org.controlsfx.control.GridCell;
import org.controlsfx.control.GridView;
import org.controlsfx.control.cell.ColorGridCell;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private GridView<Color> myGrid;
@Override
public void initialize(URL location, ResourceBundle resources) {
ObservableList<Color> list = FXCollections.observableArrayList();
myGrid.setCellFactory(new Callback<GridView<Color>, GridCell<Color>>() {
public GridCell<Color> call(GridView<Color> gridView) {
return new ColorGridCell();
}
});
Random r = new Random(System.currentTimeMillis());
for(int i = 0; i < 50; i++) {
list.add(new Color(r.nextDouble(), r.nextDouble(), r.nextDouble(), 1.0));
}
myGrid.setItems(list);
myGrid.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
//Nothing happens
myGrid.setPrefHeight(10000);
}
});
}
}
View.fxml
The gui:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import org.controlsfx.control.GridView?>
<AnchorPane prefHeight="118.0" prefWidth="473.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
<children>
<ScrollPane fitToWidth="true" prefHeight="174.0" prefWidth="473.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<VBox>
<children>
<AnchorPane>
<children>
<Label layoutX="63.0" layoutY="57.0" text="Some Nodes" AnchorPane.leftAnchor="63.0" AnchorPane.topAnchor="57.0" />
</children>
</AnchorPane>
<GridView fx:id="myGrid" />
</children>
</VBox>
</content>
</ScrollPane>
</children>
</AnchorPane>