I have a table view which has some rows. I am able to get the selected index which user has selected. I want to clear the selection when the user clicks on empty rows. How can I detect whether the user has clicked on empty rows or not?
I have tried below things.
cSVTableView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
//int selectedIndex = cSVTableView.getSelectionModel().selectedIndexProperty().get();
if (e.getButton() == MouseButton.SECONDARY && selectedIndex != -1 && selectedIndex <= modelList.size() - 1) {
contextMenu.show(cSVTableView, e.getScreenX(), e.getScreenY());
} else if(e.getButton() == MouseButton.PRIMARY && selectedIndex==-1){
cSVTableView.getSelectionModel().clearSelection();
contextMenu.hide();
} else {
contextMenu.hide();
}
}
});
I have tried this as well but it is not working
tableView.setRowFactory(new Callback<TableView<MainModel>, TableRow<MainModel>>() {
public TableRow<MainModel> call(TableView<MainModel> param) {
final TableRow<MainModel> row = new TableRow<MainModel>();
row.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<Event>() {
public void handle(Event event) {
if((! row.isEmpty()) && ( ((MouseEvent) event).getButton()==MouseButton.PRIMARY)){
MainModel clickedRow = row.getItem();
System.out.println("clickedRow::" +clickedRow);
}
}
});
return row;
}
});