JavaFX 列表视图单元格增长
JavaFX listview cell growing
我在使用 JavaFX 库显示数据时遇到问题。
我的问题描述:
我需要显示 String
的列表,每个列表都有不同的元素数。
我发明的只是为每个列表创建列表视图。
但我在展示时遇到了麻烦:
例如:我有带有 String
的列表视图 - 并遵循简单的代码:
ListView<String> lst = new ListView<>();
ObservableList<String> observableList = FXCollections.observableArrayList("Hello", "Hello -2");
lst.setItems(observableList);
lst.setPrefHeight(100);
lst.setOrientation(Orientation.HORIZONTAL);
Scene scene = new Scene(new VBox(lst));
primaryStage.setScene(scene);
primaryStage.show();
当我 运行 它时 - 我看到类似的东西:
但我希望列表视图中的元素在视图中填充 space,我不希望右侧有条纹 space。
在我的任务中,我有不同的元素数量不同的列表。
如果你能帮助我或让我知道应该怎么做 - 那就太好了!
谢谢!
我认为这就是您正在寻找的解决方案:
public class TestController {
@FXML
private ListView<String> listView;
@FXML
public void initialize() {
ObservableList<String> observableList = FXCollections.observableArrayList("Hello", "Hello -2", "More", "Item");
listView.setItems(observableList);
listView.setPrefHeight(100);
Platform.runLater(() -> System.out.println(listView.getWidth()));
listView.setOrientation(Orientation.HORIZONTAL);
listView.setCellFactory(param -> new CustomListCell());
}
private class CustomListCell extends ListCell<String> {
CustomListCell() {
// If you want to use as a separate class you can use the getListView() instead of listView.
prefWidthProperty().bind(listView.widthProperty()
.divide(listView.getItems().size()) // set the width equally for each cell
.subtract(1)); // subtracted 1 to prevent displaying of a scrollBar, but you can play with
// this if you have many values in the listView
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
} else {
setText(item);
}
}
}
}
我在使用 JavaFX 库显示数据时遇到问题。
我的问题描述:
我需要显示 String
的列表,每个列表都有不同的元素数。
我发明的只是为每个列表创建列表视图。
但我在展示时遇到了麻烦:
例如:我有带有 String
的列表视图 - 并遵循简单的代码:
ListView<String> lst = new ListView<>();
ObservableList<String> observableList = FXCollections.observableArrayList("Hello", "Hello -2");
lst.setItems(observableList);
lst.setPrefHeight(100);
lst.setOrientation(Orientation.HORIZONTAL);
Scene scene = new Scene(new VBox(lst));
primaryStage.setScene(scene);
primaryStage.show();
当我 运行 它时 - 我看到类似的东西:
但我希望列表视图中的元素在视图中填充 space,我不希望右侧有条纹 space。 在我的任务中,我有不同的元素数量不同的列表。
如果你能帮助我或让我知道应该怎么做 - 那就太好了!
谢谢!
我认为这就是您正在寻找的解决方案:
public class TestController {
@FXML
private ListView<String> listView;
@FXML
public void initialize() {
ObservableList<String> observableList = FXCollections.observableArrayList("Hello", "Hello -2", "More", "Item");
listView.setItems(observableList);
listView.setPrefHeight(100);
Platform.runLater(() -> System.out.println(listView.getWidth()));
listView.setOrientation(Orientation.HORIZONTAL);
listView.setCellFactory(param -> new CustomListCell());
}
private class CustomListCell extends ListCell<String> {
CustomListCell() {
// If you want to use as a separate class you can use the getListView() instead of listView.
prefWidthProperty().bind(listView.widthProperty()
.divide(listView.getItems().size()) // set the width equally for each cell
.subtract(1)); // subtracted 1 to prevent displaying of a scrollBar, but you can play with
// this if you have many values in the listView
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
} else {
setText(item);
}
}
}
}