Dal*_*pic 2 java bind javafx button tableview
诺布需要再次帮助.:)
我有一个名为tblTabela的TableView和一个名为btnIzracunaj的Button.我需要的是将Button禁用属性与TableView绑定,以便在TableView没有内容时禁用Button.
当TextFields为空时,我做了与另一个Button类似的绑定,如下所示:如何在TextField为空时禁用Button?
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);
Run Code Online (Sandbox Code Playgroud)
但我的问题是使用TableView,我不知道如何设置绑定属性.应该使用TableView的哪些属性?我试过这个,它没有返回错误,但也没有按预期工作.我相信getItems()应该有别的东西,但无法弄清楚是什么.:(
BooleanBinding ee = new BooleanBinding() {
{
super.bind(tblTabela.getItems());
}
@Override
protected boolean computeValue() {
return (tblTabela.getItems().isEmpty());
}
};
btnIzracunaj.disableProperty().bind(ee);
Run Code Online (Sandbox Code Playgroud)
提前致谢.
将按钮的disabled属性绑定到可观察列表,如下所示:
button.disableProperty().bind(Bindings.size(list).isEqualTo(0));
Run Code Online (Sandbox Code Playgroud)
示例代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)