Mr.White
Mr.White

Reputation: 31

Is there a simple way of checking if a column header was clicked in a TableView?

First time here. Thanks for your help. Using java 11 with javafx for java 11.

I am using a TableView but unable to determine if a column header was clicked instead of one of the rows.

Having something like the line below would solve the problem as I only want it to execute if there was a double click and the selected item is not a header.

(getClickCount() == 2) && notAHeader)

Currently the line below will return either the first item or the last item even if the header is clicked.

accountTable.getSelectionModel().getSelectedItem();  

If the following would return -1 when the header was clicked I could use it to solve my problem but it appears that it is only returned when nothing is selected.

accountTable.getSelectionModel().getFocusedIndex(); 

I tried clearing the selection at the end of the lambda. This appears to work but it just flashes the selection on the click and is not the behavior I want.

I also tried setting a event filter to listen for the clicks and determine what I clicked contains the text of any of the headers. This solution is very ugly but it has the behavior I would like.

accountTable.addEventFilter(MouseEvent.MOUSE_PRESSED, (EventHandler) event -> {
                if (event.getTarget().toString().contains("Display Name") ||
                        event.getTarget().toString().contains("Username") ||
                        event.getTarget().toString().contains("Email Address")) {
                    notAHeader= false;
                } else {
                    notAHeader= true;
                }
            });

Code being used to configure the table.

    private void configTable(TableView accountTable) {
        accountTable.setPlaceholder(new Label("Failed to load"));
        HashMap<String, String> columnHeaders = new HashMap<>();
        columnHeaders.put("Display Name", "displayName");
        columnHeaders.put("Username", "userLogonName");
        columnHeaders.put("Email Address", "emailAddress");
        columnHeaders.forEach((header, getter) -> {
                    TableColumn<String, ADAccount> column = new TableColumn<>(header);
                    column.setCellValueFactory(new PropertyValueFactory<>(getter));
                    accountTable.getColumns().add(column);
                }

        );
        accountTable.setOnMouseClicked(e -> {
            if (e.getClickCount() == 2) {
                //need this to be ignored if a column header ws clicked.
                openAccountDetails(getSelectedAccount(),getMode());
            }
        });
    }

I would like to be able to determine if a column header was clicked so I can ignore opening account details as the column header is not an account. Right now I am unable to distinguish between a click on the column header and a row as the selected item does not return any indicators of the column headers being selected.

Upvotes: 0

Views: 809

Answers (1)

Mr.White
Mr.White

Reputation: 31

When the column headers is clicked the onSort is called before the sorting. I used onSort to clear the selection as nothing is selected when the header is clicked.

The line below gives me the behavior I needed without any side effects from what I can tell.

//clears the selection when the column header is selected before the sorting 
accountTable.setOnSort( e -> accountTable.getSelectionModel().clearSelection());

Upvotes: 0

Related Questions