小编use*_*782的帖子

javafx listview with button in each cell

我有一个 javafx 应用程序,其中包含一个带有自定义单元工厂的列表视图。每个单元格都包含一个按钮,用于对该单元格执行特定操作。在更新到 java 8 之前,一切正常,但是在使用 java 8 运行我的应用程序后,当我单击按钮而不是按按钮处理事件时,整个单元格被选中。这是我的代码:

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;

public class CustomCell extends ListCell<String> {
    private Button actionBtn;   

    public CustomCell() {
        super();    

        setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {              
                //do something                  
            }
        });     
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        setEditable(false);
        if (item != null) {
            Label name = new Label(item);                           
            actionBtn = new Button("my action");            
            actionBtn.setOnAction(new EventHandler<ActionEvent>() {

                @Override …
Run Code Online (Sandbox Code Playgroud)

listview javafx

3
推荐指数
1
解决办法
7699
查看次数

标签 统计

javafx ×1

listview ×1