Gorgeo
Gorgeo

Reputation: 7

Custom item in ListView JavaFX

I have question about ListView in JavaFX.

How I can do custom item in ListView like this :

ScreenShot SS

Secondly, when I click Button1 i want to show image2 and textfield1

Upvotes: 0

Views: 1817

Answers (1)

Miss Chanandler Bong
Miss Chanandler Bong

Reputation: 4258

Implementing your own CellFactory gives you all the options you want to apply on a ListView's cell. Unfortunately the image you uploaded is not opening so I couldn't understand your exact requirements. anyway this is how you set your CellFactory for your ListView where T is your data type.

ListView#setCellFactory(Callback<ListView<T>, ListCell<T>> value)

Example:

ListView<Employee> listView = new ListView<>();
listView.setCellFactory(new Callback<ListView<Employee>, ListCell<Employee>>() {
    @Override
    public ListCell<Employee> call(ListView<Employee> param) {
        return new ListCell<Employee>() {
            
            private ImageView imageView = new ImageView("ImageURL");
            private TextField textField = new TextField("Text");
            private Button button = new Button("Button");
            private BorderPane bp = new BorderPane(imageView, null, button, null, textField);
            
            @Override
            protected void updateItem(Employee item, boolean empty) {
                super.updateItem(item, empty);
                if (item == null || empty) {
                    setText(null);
                    setGraphic(null);
                } else {
                    setText(item.getName());
                    setGraphic(bp);
                }
            }
        };
    }
});

Upvotes: 1

Related Questions