JavaFX中具有自定义内容的ListView

JavaFX中具有自定义内容的ListView,java,user-interface,javafx,javafx-8,Java,User Interface,Javafx,Javafx 8,如何使用JavaFx为我的应用程序定制ListView。我需要一个带有图像的HBox,每行列表视图有两个标签。您可以通过查看提供一个自定义CellFactory 工作示例 import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.

如何使用JavaFx为我的应用程序定制ListView。我需要一个带有图像的HBox,每行列表视图有两个标签。

您可以通过查看提供一个自定义CellFactory

工作示例

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Callback;

public class CustomListView extends Application {
    private static class CustomThing {
        private String name;
        private int price;
        public String getName() {
            return name;
        }
        public int getPrice() {
            return price;
        }
        public CustomThing(String name, int price) {
            super();
            this.name = name;
            this.price = price;
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        ObservableList<CustomThing> data = FXCollections.observableArrayList();
        data.addAll(new CustomThing("Cheese", 123), new CustomThing("Horse", 456), new CustomThing("Jam", 789));

        final ListView<CustomThing> listView = new ListView<CustomThing>(data);
        listView.setCellFactory(new Callback<ListView<CustomThing>, ListCell<CustomThing>>() {
            @Override
            public ListCell<CustomThing> call(ListView<CustomThing> listView) {
                return new CustomListCell();
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(listView);
        primaryStage.setScene(new Scene(root, 200, 250));
        primaryStage.show();
    }

    private class CustomListCell extends ListCell<CustomThing> {
        private HBox content;
        private Text name;
        private Text price;

        public CustomListCell() {
            super();
            name = new Text();
            price = new Text();
            VBox vBox = new VBox(name, price);
            content = new HBox(new Label("[Graphic]"), vBox);
            content.setSpacing(10);
        }

        @Override
        protected void updateItem(CustomThing item, boolean empty) {
            super.updateItem(item, empty);
            if (item != null && !empty) { // <== test for null item and empty parameter
                name.setText(item.getName());
                price.setText(String.format("%d $", item.getPrice()));
                setGraphic(content);
            } else {
                setGraphic(null);
            }
        }
    }

}
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.control.ListCell;
导入javafx.scene.control.ListView;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.StackPane;
导入javafx.scene.layout.VBox;
导入javafx.scene.text.text;
导入javafx.stage.stage;
导入javafx.util.Callback;
公共类CustomListView扩展了应用程序{
私有静态类定制{
私有字符串名称;
私人int价格;
公共字符串getName(){
返回名称;
}
public int getPrice(){
退货价格;
}
公共自定义对象(字符串名称、整数价格){
超级();
this.name=名称;
这个价格=价格;
}
}
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primaryStage){
ObservableList data=FXCollections.observableArrayList();
addAll(newcustomthing(“Cheese”,123),newcustomthing(“Horse”,456),newcustomthing(“Jam”,789));
最终ListView ListView=新ListView(数据);
setCellFactory(新回调(){
@凌驾
公共ListCell调用(ListView ListView){
返回新的CustomListCell();
}
});
StackPane root=新的StackPane();
root.getChildren().add(listView);
原始阶段。设置场景(新场景(根,200250));
primaryStage.show();
}
私有类CustomListCell扩展了ListCell{
私人HBox内容;
私有文本名称;
私有文本价格;
公共CustomListCell(){
超级();
名称=新文本();
价格=新文本();
VBox VBox=新的VBox(名称、价格);
内容=新的HBox(新标签(“[图形]”),vBox);
内容。设置间隔(10);
}
@凌驾
受保护的void updateItem(自定义项,布尔值为空){
super.updateItem(项,空);

如果(项!=null&&!空){//您需要一个自定义单元格工厂,并使用listView进行设置。setCellFactory,我将很快写出一个答案。谢谢。我只看到了listView的示例,在可观察列表中包含自定义对象,而不是在单元格中包含自定义小部件。我添加了添加任意内容的示例,可以是任何节点、文本字段等。是的!就是这样!谢谢!我可以使用fxml
(项)?您是如何将“数据”可观察列表与表视图绑定的?此处似乎缺少某些内容?@csotiriou它将其传递到构造函数中…请参阅“新建列表视图(数据)”