JavaFX的 - 与图像按钮列表视图项 [英] JavaFX - ListView Item with an Image Button

查看:748
本文介绍了JavaFX的 - 与图像按钮列表视图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能每个ListView项后添加图像按钮。例如:

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

在这里的红色方块应该有一个按钮来代替。如果可能的话,我怎么能处理项目的按钮的单击事件?

where those red squares should have a button instead. If it's possible, how can I handle item's button's click event?

编辑:如果有,可以做其他控件,请让我知道。我测试的TableView。

if there's another control that can do it, please let me know. I'm testing TableView.

在此先感谢!

推荐答案

我最近测试此。我的解决方案:

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<String> {
        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<ActionEvent>() {
                @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 : "<null>");
                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<String> list = FXCollections.observableArrayList(
                "Item 1", "Item 2", "Item 3", "Item 4");
        ListView<String> lv = new ListView<>(list);
        lv.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
            @Override
            public ListCell<String> call(ListView<String> param) {
                return new XCell();
            }
        });
        pane.getChildren().add(lv);
        primaryStage.show();
    }

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

细胞将是这样的:

The cells will look like this:

相关部分是 XCell.updateItem 方法和 setGraphic 电话。与 setGraphic()通常的图标,应为一个标签设置,但它同样适用于设置的复节点 - 。在这种情况下的HBox与标签和按钮

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.

您可能想看看这些:

这篇关于JavaFX的 - 与图像按钮列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆