UB-
UB-

Reputation: 123

Enabling a disabled button when TableView row selected

I have a button on my interface that is disabled by default. I want it to become enabled when the user selects a row in my TableView and becomes disabled again when the user clicks elsewhere. What is the simplest way to do this?

Upvotes: 4

Views: 4916

Answers (2)

eckig
eckig

Reputation: 11134

Seems like a perfect place to use JavaFX Bindings:

TableView<String> tableView = new TableView<>(tableData);

TableColumn<String, String> column1 = new TableColumn<>();

Button button = new Button("Button");
button.disableProperty().bind(Bindings.isEmpty(tableView.getSelectionModel().getSelectedItems()));

This example disables the Button when the user has selected nothing or cleared his selection and becomes enabled as soon as at least one row is being selected.

Upvotes: 11

bakriawad
bakriawad

Reputation: 345

add a focus listener to your tableView object, and on focus set button.enable = true; on lost focus then button.enable = false;

the code will look a bit like

class myClass implements FocusListener
{
    TableView.addFocusListener(this);

    public void focusGained(FocusEvent e) {
        button.enable=true;
    }

    public void focusLost(FocusEvent e) {
        button.enable=false;
    }


}

Focus listener toturial

Upvotes: 0

Related Questions