Search code examples
javabuttonjavafxtableviewbind

Disable Button when TableView is empty


noob need help again. :)

I have a TableView named tblTabela and a Button named btnIzracunaj. What I need is to bind Button disable property with TableView so when TableView has no content Button is disabled.

I did similar binding with another Button when TextFields are empty like this: How to disable Button when TextField is empty?

    BooleanBinding bb = new BooleanBinding() {
{
    super.bind(txtPovrsina.textProperty(),
            txtPrvi.textProperty(),
            txtDrugi.textProperty());
}

@Override
protected boolean computeValue() {
    return (txtPovrsina.getText().isEmpty()
            || txtPrvi.getText().isEmpty()
            || txtDrugi.getText().isEmpty());
}

};
btnDodaj.disableProperty().bind(bb);

But my problem is with TableView, I don't know how to set property for binding. What property of TableView should be used? I tried this, it doesn't return error, but not working as intended either. I am sure instead of getItems() there should be something else but can't figure out what. :(

    BooleanBinding ee = new BooleanBinding() {
{
    super.bind(tblTabela.getItems());
}

@Override
protected boolean computeValue() {
    return (tblTabela.getItems().isEmpty());
}

};
btnIzracunaj.disableProperty().bind(ee);

Thanks in advance.


Solution

  • Bind the button's disabled property to the observable list like this:

    button.disableProperty().bind(Bindings.size(list).isEqualTo(0));
    

    Example code:

    import javafx.application.Application;
    import javafx.beans.binding.Bindings;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
        @Override
        public void start(Stage primaryStage) {
    
            ObservableList<String> list = FXCollections.observableArrayList();
    
            HBox root = new HBox();
    
            // add button
            Button addButton = new Button("Add");
            addButton.setOnAction(e -> {
    
                list.add("Text");
    
                System.out.println("Size: " + list.size());
    
            });
    
            // remove button
            Button removeButton = new Button("Remove");
            removeButton.setOnAction(e -> {
    
                if (list.size() > 0) {
                    list.remove(0);
                }
    
                System.out.println("Size: " + list.size());
    
            });
    
            root.getChildren().addAll(addButton, removeButton);
    
            // bind to remove button
            removeButton.disableProperty().bind(Bindings.size(list).isEqualTo(0));
    
            Scene scene = new Scene(root, 800, 600);
    
            primaryStage.setScene(scene);
            primaryStage.show();
    
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }