JavaFX - ListView Item with an Image Button

前端 未结 3 2075
野性不改
野性不改 2020-12-02 17:41

I\'d like to know if there\'s possible to add an Image Button after every ListView Item. For example:\"sample\"

3条回答
  •  再見小時候
    2020-12-02 18:08

    I was testing this recently. My solution:

    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.ListCell;
    import javafx.scene.control.ListView;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.Priority;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import javafx.util.Callback;
    
    public class SO extends Application {
        static class XCell extends ListCell {
            HBox hbox = new HBox();
            Label label = new Label("(empty)");
            Pane pane = new Pane();
            Button button = new Button("(>)");
            String lastItem;
    
            public XCell() {
                super();
                hbox.getChildren().addAll(label, pane, button);
                HBox.setHgrow(pane, Priority.ALWAYS);
                button.setOnAction(new EventHandler() {
                    @Override
                    public void handle(ActionEvent event) {
                        System.out.println(lastItem + " : " + event);
                    }
                });
            }
    
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                setText(null);  // No text in label of super class
                if (empty) {
                    lastItem = null;
                    setGraphic(null);
                } else {
                    lastItem = item;
                    label.setText(item!=null ? item : "");
                    setGraphic(hbox);
                }
            }
        }
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            StackPane pane = new StackPane();
            Scene scene = new Scene(pane, 300, 150);
            primaryStage.setScene(scene);
            ObservableList list = FXCollections.observableArrayList(
                    "Item 1", "Item 2", "Item 3", "Item 4");
            ListView lv = new ListView<>(list);
            lv.setCellFactory(new Callback, ListCell>() {
                @Override
                public ListCell call(ListView param) {
                    return new XCell();
                }
            });
            pane.getChildren().add(lv);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    The cells will look like this:

    Custom List Cell with Button

    The relevant part is the XCell.updateItem method and the setGraphic call. With setGraphic() usually an icon shall be set for a label, but it works equally well to set a complex Node - in this case the HBox with label and button.

    You need to make sure, that the Button's event handler refers to the correct item in the list. In the first link below, it is mentioned, that the currently selected item might be sufficient for now. So fetch the currently selected index in the list when handling the button's event.

    You might like to look at these:

    • http://fxexperience.com/2012/05/listview-custom-cell-factories-and-context-menus/
    • http://docs.oracle.com/javafx/2/ui_controls/list-view.htm

提交回复
热议问题