这有效:
getTableView().itemsProperty().addListener((observableValue, oldVal, newVal) -> button.setDisable(newVal == null || newVal.isEmpty()));
Run Code Online (Sandbox Code Playgroud)
当表视图项为空或为空时,该按钮被禁用。
我想知道是否有一个“绑定”解决方案以及如何实施。
button.disableProperty().bind(Bindings.or(Bindings.isNull(getTableView().itemsProperty()), Bindings.isEmpty(getTableView().getItems())));
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,因为itemsPropertyis nevernull并且getItems()可能为 null 导致Bindings.isEmpty()失败。
是ItemsProperty一个TableView简单的ObservableProperty,它不提供任何专门的功能来跟踪项目中的项目ObservableList.
您将需要创建一个ListProperty,它又提供了emptyProperty您可以用于您的目的的:
ListProperty<String> listProperty = new SimpleListProperty<>();;
Run Code Online (Sandbox Code Playgroud)
现在绑定该属性以ItemsProperty观察TableView:
listProperty.bind(tableView.itemsProperty());
Run Code Online (Sandbox Code Playgroud)
最后一步只是绑定disableProperty您的Button:
button.disableProperty().bind(listProperty.emptyProperty());
Run Code Online (Sandbox Code Playgroud)
ListView下面是一个使用 a代替 a 的完整示例TableView。功能是相同的,但就本示例而言,a 的ListView实现速度更快。
import javafx.application.Application;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ButtonBindingToList extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// **********************************************************************************************
// Create a basic layout
// **********************************************************************************************
VBox root = new VBox(5);
root.setAlignment(Pos.TOP_CENTER);
root.setPadding(new Insets(10));
// **********************************************************************************************
// Creat the ListView
// **********************************************************************************************
ListView<String> listView = new ListView<>();
// **********************************************************************************************
// Create a Button to add items to the list (will always be enabled).
// **********************************************************************************************
Button btnAdd = new Button("Add item");
btnAdd.setOnAction(event -> listView.getItems().add("Added item #" + listView.getItems().size()));
// **********************************************************************************************
// Create button to remove the last item in the ListView
// **********************************************************************************************
Button btnRemove = new Button("Remove last item");
btnRemove.setOnAction(event -> listView.getItems().remove(listView.getItems().size() - 1));
// **********************************************************************************************
// In order to bind to the emptiness of the ListView, we need to create a ListProperty that
// is bound to the ListView's itemsProperty
// **********************************************************************************************
ListProperty<String> listProperty = new SimpleListProperty<>();
listProperty.bind(listView.itemsProperty());
// **********************************************************************************************
// Now bind the "Remove" button's disable property to the listProperty. This will disable
// the button as long as the ListView is empty
// **********************************************************************************************
btnRemove.disableProperty().bind(listProperty.emptyProperty());
root.getChildren().addAll(listView, btnAdd, btnRemove);
// **********************************************************************************************
// Set the Scene for the stage
// **********************************************************************************************
primaryStage.setScene(new Scene(root));
// **********************************************************************************************
// Configure the Stage
// **********************************************************************************************
primaryStage.setTitle("Test Application");
primaryStage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
784 次 |
| 最近记录: |