检测TableView JavaFX的单元格上的双击 [英] Detect doubleclick on cell of TableView JavaFX

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

问题描述

我正在尝试检测tableview的随机单元格上的双击。
检测双击不是问题,而是双击哪个单元格。

I am trying to detect a doubleclick on a random cell of a tableview. The detection of the doubleclick is not a problem but rather which cell has been doubleclicked.

table.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());
            }
        }
    });

这就是我构建表格的方式:

This is how I'm building my table:

private void BuildTable() throws Exception
{
    /*Some initialisations etc*/

        for(int i=0; i<result.getMetaData().getColumnCount();i++)
        {
            final int j = i;
            TableColumn col = new TableColumn(result.getMetaData().getColumnName(i+1));
            col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
                public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param)
                {
                    return new SimpleStringProperty(param.getValue().get(j).toString());
                }
            });
            table.getColumns().addAll(col);


        }
        while(result.next()){
            ObservableList<String> row = FXCollections.observableArrayList();
            for(int i = 1; i<=result.getMetaData().getColumnCount();i++){
                row.add(result.getString(i));
            }
            data.add(row);
        }
        table.setItems(data);
    }catch(Exception e){
        e.printStackTrace();
    }

}

这里真正的问题是我不能只是转换为TableCell。
有人可以帮帮我吗?我将非常感激。

The real problem here is that I can't just typecast into a TableCell. Can someone help me out? I would be very grateful.

推荐答案

您需要使用表格单元注册处理程序,而不是使用表格注册处理程序他们自己。为此,请在相应的 TableColumn (s)上使用单元工厂。

Instead of registering a handler with the table, you need to register the handler with the table cells themselves. To do this, use a cell factory on the appropriate TableColumn(s).

例如,添加以下代码到标准表示例(清单13.6)。

As an example, add the following code to the standard table example (listing 13.6).

firstNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
    @Override
    public TableCell<Person, String> call(TableColumn<Person, String> col) {
        final TableCell<Person, String> cell = new TableCell<Person, String>() {
            @Override
            public void updateItem(String firstName, boolean empty) {
                super.updateItem(firstName, empty);
                if (empty) {
                    setText(null);
                } else {
                    setText(firstName);
                }
            }
         };
         cell.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
             @Override
             public void handle(MouseEvent event) {
                 if (event.getClickCount() > 1) {
                     System.out.println("double click on "+cell.getItem());
                 }
             }
         });
         return cell ;
    }
});

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

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