Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 851
UB- Avatar asked Jan 10 '23 01:01

UB-


1 Answers

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.

like image 58
eckig Avatar answered Jan 23 '23 06:01

eckig