Detect doubleclick on cell of TableView JavaFX

2019-07-06 04:37发布

问题:

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

}

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

回答1:

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).

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 ;
    }
});


回答2:

You need set the tableview's selection mode in cell, defined a custom type cell where you catch the keypress event or double clic, for an example you can review this page

https://gist.github.com/james-d/be5bbd6255a4640a5357