javafx,TableView:检测对单元格的双击 [英] javafx, TableView: detect a doubleclick on a cell

查看:140
本文介绍了javafx,TableView:检测对单元格的双击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个TableView,我需要检测单元格上的双击.

Given a TableView, i need to detect the doubleclick on a cell.

tableView.setOnMouseClicked(new EventHandler<MouseEvent>()
{
    @Override
    public void handle(MouseEvent event)
    {
        if(event.getClickCount()>1)
        {
            System.out.println("double clicked!");
        }
    }
});

如何确定单击鼠标所在的单元格?

How to determine the cell on which the mouse has been clicked?

推荐答案

代码示例.
运行示例12-11:单元格编辑的替代解决方案" ="noreferrer">官方Tableview教程.
替换以下内容:

Code example.
Run the "Example 12-11: Alternative Solution Of Cell Editing" of official tableview tutorial.
Replace the followings:

table.setEditable(false);
Callback<TableColumn, TableCell> cellFactory =
        new Callback<TableColumn, TableCell>() {
            public TableCell call(TableColumn p) {
                TableCell cell = new TableCell<Person, String>() {
                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        setText(empty ? null : getString());
                        setGraphic(null);
                    }

                    private String getString() {
                        return getItem() == null ? "" : getItem().toString();
                    }
                };

                cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        if (event.getClickCount() > 1) {
                            System.out.println("double clicked!");
                            TableCell c = (TableCell) event.getSource();
                            System.out.println("Cell text: " + c.getText());
                        }
                    }
                });
                return cell;
            }
        };

不需要EditingCell,因为您的单元格不可编辑.单元格工厂用于单元格渲染.因此,可以使用单元格的setGraphics()方法放置除默认Labeled以外的任何节点/控件. IMO,您无法直接访问默认单元格,因此您应该定义自己的单元格工厂,以便能够在单元格上放置事件过滤器.

No need to EditingCell since your cells are uneditable. Cell factory is used for cell rendering. So one can put any node/control other than default Labeled using cell's setGraphics() method. IMO you cannot access the default cell directly so you should define your own cell factory to be able to put event filter on cell.

这篇关于javafx,TableView:检测对单元格的双击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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