ListView with custom content in JavaFX

后端 未结 1 557
夕颜
夕颜 2021-02-06 06:26

How i can make custom ListView with JavaFx for my app. I need HBox with image and 2 Labels for each line listView.\"enter

相关标签:
1条回答
  • 2021-02-06 06:38

    You can provide a custom CellFactory via see ListView.setCellFactory(...)

    Example

    Working example

    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);
                }
            }
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题