我正在尝试启用JavaFX Button
,具体取决于TableView
中所选行中属性值的聚合。以下是演示此问题的示例应用程序:
package test;
import java.util.Random;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(final String[] args) throws Exception {
launch(args);
}
private static class Row {
private final BooleanProperty myProp;
public Row(final boolean value) {
myProp = new SimpleBooleanProperty(value);
}
public BooleanProperty propProperty() { return myProp; }
}
@Override
public void start(final Stage window) throws Exception {
// Create a VBox to hold the table and button
final VBox root = new VBox();
root.setMinSize(200, 200);
// Create the table, and enable multi-select
final TableView<Row> table = new TableView<>();
final MultipleSelectionModel<Row> selectionModel = table.getSelectionModel();
selectionModel.setSelectionMode(SelectionMode.MULTIPLE);
root.getChildren().add(table);
// Create a column based on the value of Row.propProperty()
final TableColumn<Row, Boolean> column = new TableColumn<>("Value");
column.setCellValueFactory(p -> p.getValue().propProperty());
table.getColumns().add(column);
// Add a button below the table
final Button button = new Button("Button");
root.getChildren().add(button);
// Populate the table with true/false values
final ObservableList<Row> rows = table.getItems();
rows.addAll(new Row(false), new Row(false), new Row(false));
// Start a thread to randomly modify the row values
final Random rng = new Random();
final Thread thread = new Thread(() -> {
// Flip the value in a randomly selected row every 10 seconds
try {
do {
final int i = rng.nextInt(rows.size());
System.out.println("Flipping row " + i);
Thread.sleep(10000);
final BooleanProperty prop = rows.get(i).propProperty();
prop.set(!prop.get());
} while (true);
} catch (final InterruptedException e) {
System.out.println("Exiting Thread");
}
}, "Row Flipper Thread");
thread.setDaemon(true);
thread.start();
// Bind the button's disable property such that the button
// is only enabled if one of the selected rows is true
final ObservableList<Row> selectedRows = selectionModel.getSelectedItems();
button.disableProperty().bind(Bindings.createBooleanBinding(() -> {
for (int i = 0; i < selectedRows.size(); ++i) {
if (selectedRows.get(i).propProperty().get()) {
return false;
}
}
return true;
}, selectedRows));
// Show the JavaFX window
final Scene scene = new Scene(root);
window.setScene(scene);
window.show();
}
}
要测试,请启动上述应用程序,然后选择文本&#34;翻转行N&#34;所指示的行,其中N在[0,2]中。当所选行的值更改为true时...
观察到的行为 button
仍然被禁用
所需行为 button
已启用。
有谁知道如何创建展示所需行为的BooleanBinding
?
答案 0 :(得分:5)
如果所选行的var a = [];
a[700] = true;
console.log(a.length);
中的任何一个发生更改,则您的绑定需要失效。目前,绑定仅观察所选项目列表,该列表将在列表内容更改时触发事件(即,项目变为已选中或未选中),但在属于该列表中的项目的属性更改值时不会触发事件。
为此,请使用提取器创建列表:
propProperty
此列表将在添加或删除项目时,或者列表中任何项目的final ObservableList<Row> selectedRows =
FXCollections.observableArrayList(r -> new Observable[]{r.propProperty()});
发生更改时触发事件。 (如果需要观察多个值,可以将它们包含在propProperty()
的数组中。)
当然,您仍然需要此列表来包含表格中的所选项目。您可以通过将列表内容绑定到选择模型的Observable
来确保这一点:
selectedItems
以下是使用此版本的MCVE版本:
Bindings.bindContent(selectedRows, selectionModel.getSelectedItems());