2

I've been looking around and have noticed the response given on this post: Detect doubleclick on row of TableView JavaFX

However, this post uses lambda expressions and I'm unsure how to convert these to be used in JavaFX 2 code. My main goal is to be able to click a row, and then pop up a new window that has the information that was stored in that row. As long as I can get the Callback working, I will be able to take it from there! Any advice would be appreciated.

table.setRowFactory( tv -> {
    TableRow<MyType> row = new TableRow<>();
    row.setOnMouseClicked(event -> {
        if (event.getClickCount() == 2 && (! row.isEmpty()) ) {
            MyType rowData = row.getItem();
            System.out.println(rowData);
        }
    });
    return row ;
 });
Community
  • 1
  • 1
Sarah
  • 107
  • 1
  • 8

3 Answers3

1

Here you go:

table.setRowFactory(tv -> {
    TableRow<MyType> row = new TableRow<>();
    row.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if (event.getClickCount() == 2 && (!row.isEmpty())) {
                MyType rowData = row.getItem();
                System.out.println(rowData);
            }
        }
    });
    return row;
});

Depending on your IDE you can usually hit ctrl + space when the cursor is in the parentheses of setOnMouseClicked() and it will tell you what it takes. Then you can type new EventHandler (or whatever it needs), hit enter and most IDEs will autocomplete the full method for you. Then you can just paste in the guts.

Brad
  • 722
  • 2
  • 8
  • 24
1

I advise you to use this way to solve the problem

@FXML
     public void handle(MouseEvent event) {
         
             if (event.getClickCount() == 2) {
                  TableView.TableViewSelectionModel<Pessoa> p = tableFamilia.getSelectionModel();
                   System.out.println(p.getSelectedItem().getNome());
             }
           
     
    }
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Angleu
  • 11
  • 1
0

My Full solution

 @FXML
private TableView<StudentTableModel> studentTable;

....
studentTable.setRowFactory(tv -> {
        TableRow<StudentTableModel> row = new TableRow<>();
        row.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.getClickCount() == 2 && (!row.isEmpty())) {
                    TableView.TableViewSelectionModel<StudentTableModel> rowData = studentTable.getSelectionModel();
                    editOnDoubleClick(rowData.getSelectedItem());
                }
            }
        });
        return row;
    });

It took me a while searching for this and i this really helped me give a more intuitive UX on my JavaFX application.

Hope it helps someone.

blongho
  • 1,151
  • 9
  • 12