Detect doubleclick on cell of TableView JavaFX


Question

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
3
6/3/2014 3:01:58 PM

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 ;
    }
});
4
11/24/2014 3:29:34 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon