我希望在更改宽度(调整窗口大小)时自动调整gridView的高度,gridView scrollBar不显示,我从gridView获得一个scrollBar(来自scrollPane),还有一些额外的节点在一起。但由于某种原因,changeListener中的setPrefHeight()
无效。
在控制器类的gridView.setPrefHeight(1000);
方法中使用initialize()
可以达到预期的效果。
MainApp.java
加载FXML文件并让舞台显示:
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
控制器类,初始化gridView,以及changeListener所在的位置:
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
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>