如何计算流程窗格中的复选框 (Java)
How to count the checkbox in flow pane (Java)
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
System.out.println("confirmed");
this.refreshData( e -> true);
this.pieTriState.getData().forEach(this::clickOnPie);
Arrays.stream(gList).forEach(e -> {
var checkBox = new CheckBox(e.getState());
checkBox.setPrefSize(60,15);
this.fpStates.getChildren().add(checkBox);
System.out.println(checkBox);
checkBox.setOnAction((ActionEvent event) -> {
if(checkBox.isSelected()){
//I tried to count the checkbox by using this. but its not work for me
for(int i =0; i <1; i++){
System.out.println(i);
}
System.out.println(checkBox.getText());
//btnRefresh.setVisible(false);
//lblStatus.setText("more than 4 states selected is invalid.");
}
});
});
我正在尝试对复选框进行计数。如果选中 4 个复选框,那么我可以隐藏一个按钮。但问题是复选框在流程窗格中。所以我不知道怎么算。
在众多方法中,以下是您可以尝试的方法之一。
思路是:
- 创建一个计数器并创建一个布尔监听器来递增计数器并检查它是否满足条件
- 将此侦听器添加到所有选中的复选框 属性。
在循环外你需要包含下面的代码:
IntegerProperty count = new SimpleIntegerProperty();
ChangeListener<Boolean> selectedListener = (obs,old,selected)->{
count.setValue(selected?count.get()+1:count.get()-1);
if(count.get()>=4){
// hide the button, set the error text
}else{
// unhide the button, clear the error text
}
};
并且在循环中,创建复选框后,将侦听器添加到复选框:
checkBox.selectedProperty().addListener(selectedListener);
此答案使用与 this solution 相同的概念(过滤列表):
- Use a listener to get selected rows (mails) in tableView and add mails to my list of mails
但没有支持模型或 table。
解决方法:
- 创建一个复选框列表。
- 在复选框列表上定义一个提取器,以便在列表中任何复选框的选择 属性 更改时发生列表更改事件。
- 根据所有复选框的列表创建选定复选框的过滤列表。
- 将整数属性绑定到两个列表的大小。
- 将表示大小属性的标签显示为每个列表中元素数量的文本计数。
- 将复选框放在 FlowPane 中。
要更详细地了解该方法,请阅读链接解决方案随附的文本。
import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class CheckApp extends Application {
private static final int NUM_CHECKBOXES = 5;
@Override
public void start(Stage stage) {
// lists to track checkboxes and selected checkboxes.
ObservableList<CheckBox> checkBoxes = FXCollections.observableArrayList(
c -> new Observable[] { c.selectedProperty() }
);
FilteredList<CheckBox> selectedCheckBoxes = checkBoxes.filtered(
CheckBox::isSelected
);
// properties to track the size of the checkbox and selected checkbox lists.
IntegerProperty numCheckboxes = new SimpleIntegerProperty();
numCheckboxes.bind(Bindings.size(checkBoxes));
IntegerProperty numSelectedCheckboxes = new SimpleIntegerProperty();
numSelectedCheckboxes.bind(Bindings.size(selectedCheckBoxes));
// create the checkboxes.
for (int i = 0; i < NUM_CHECKBOXES; i++) {
checkBoxes.add(new CheckBox("" + (i+1)));
}
// create a flow pane to hold the checkboxes and put them inside.
FlowPane flowPane = new FlowPane(Orientation.VERTICAL, 10, 10);
flowPane.setPadding(new Insets(10));
flowPane.getChildren().addAll(checkBoxes);
// create text labels for the checkbox and selected checkbox counts.
Label numCheckboxesLabel = new Label();
numCheckboxesLabel.textProperty().bind(
numCheckboxes.asObject().asString()
);
Label numSelectedCheckboxesLabel = new Label();
numSelectedCheckboxesLabel.textProperty().bind(
numSelectedCheckboxes.asObject().asString()
);
// create some summary info
GridPane summaryInfo = new GridPane();
summaryInfo.setHgap(10);
summaryInfo.setVgap(5);
summaryInfo.addRow(0, new Label("# checkboxes:" ), numCheckboxesLabel);
summaryInfo.addRow(1, new Label("# selected checkboxes:" ), numSelectedCheckboxesLabel);
// layout the scene.
final VBox layout = new VBox(
10,
summaryInfo,
flowPane
);
layout.setPadding(new Insets(10));
layout.setPrefHeight(200);
stage.setScene(new Scene(layout));
stage.show();
}
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
System.out.println("confirmed");
this.refreshData( e -> true);
this.pieTriState.getData().forEach(this::clickOnPie);
Arrays.stream(gList).forEach(e -> {
var checkBox = new CheckBox(e.getState());
checkBox.setPrefSize(60,15);
this.fpStates.getChildren().add(checkBox);
System.out.println(checkBox);
checkBox.setOnAction((ActionEvent event) -> {
if(checkBox.isSelected()){
//I tried to count the checkbox by using this. but its not work for me
for(int i =0; i <1; i++){
System.out.println(i);
}
System.out.println(checkBox.getText());
//btnRefresh.setVisible(false);
//lblStatus.setText("more than 4 states selected is invalid.");
}
});
});
我正在尝试对复选框进行计数。如果选中 4 个复选框,那么我可以隐藏一个按钮。但问题是复选框在流程窗格中。所以我不知道怎么算。
在众多方法中,以下是您可以尝试的方法之一。 思路是:
- 创建一个计数器并创建一个布尔监听器来递增计数器并检查它是否满足条件
- 将此侦听器添加到所有选中的复选框 属性。
在循环外你需要包含下面的代码:
IntegerProperty count = new SimpleIntegerProperty();
ChangeListener<Boolean> selectedListener = (obs,old,selected)->{
count.setValue(selected?count.get()+1:count.get()-1);
if(count.get()>=4){
// hide the button, set the error text
}else{
// unhide the button, clear the error text
}
};
并且在循环中,创建复选框后,将侦听器添加到复选框:
checkBox.selectedProperty().addListener(selectedListener);
此答案使用与 this solution 相同的概念(过滤列表):
- Use a listener to get selected rows (mails) in tableView and add mails to my list of mails
但没有支持模型或 table。
解决方法:
- 创建一个复选框列表。
- 在复选框列表上定义一个提取器,以便在列表中任何复选框的选择 属性 更改时发生列表更改事件。
- 根据所有复选框的列表创建选定复选框的过滤列表。
- 将整数属性绑定到两个列表的大小。
- 将表示大小属性的标签显示为每个列表中元素数量的文本计数。
- 将复选框放在 FlowPane 中。
要更详细地了解该方法,请阅读链接解决方案随附的文本。
import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class CheckApp extends Application {
private static final int NUM_CHECKBOXES = 5;
@Override
public void start(Stage stage) {
// lists to track checkboxes and selected checkboxes.
ObservableList<CheckBox> checkBoxes = FXCollections.observableArrayList(
c -> new Observable[] { c.selectedProperty() }
);
FilteredList<CheckBox> selectedCheckBoxes = checkBoxes.filtered(
CheckBox::isSelected
);
// properties to track the size of the checkbox and selected checkbox lists.
IntegerProperty numCheckboxes = new SimpleIntegerProperty();
numCheckboxes.bind(Bindings.size(checkBoxes));
IntegerProperty numSelectedCheckboxes = new SimpleIntegerProperty();
numSelectedCheckboxes.bind(Bindings.size(selectedCheckBoxes));
// create the checkboxes.
for (int i = 0; i < NUM_CHECKBOXES; i++) {
checkBoxes.add(new CheckBox("" + (i+1)));
}
// create a flow pane to hold the checkboxes and put them inside.
FlowPane flowPane = new FlowPane(Orientation.VERTICAL, 10, 10);
flowPane.setPadding(new Insets(10));
flowPane.getChildren().addAll(checkBoxes);
// create text labels for the checkbox and selected checkbox counts.
Label numCheckboxesLabel = new Label();
numCheckboxesLabel.textProperty().bind(
numCheckboxes.asObject().asString()
);
Label numSelectedCheckboxesLabel = new Label();
numSelectedCheckboxesLabel.textProperty().bind(
numSelectedCheckboxes.asObject().asString()
);
// create some summary info
GridPane summaryInfo = new GridPane();
summaryInfo.setHgap(10);
summaryInfo.setVgap(5);
summaryInfo.addRow(0, new Label("# checkboxes:" ), numCheckboxesLabel);
summaryInfo.addRow(1, new Label("# selected checkboxes:" ), numSelectedCheckboxesLabel);
// layout the scene.
final VBox layout = new VBox(
10,
summaryInfo,
flowPane
);
layout.setPadding(new Insets(10));
layout.setPrefHeight(200);
stage.setScene(new Scene(layout));
stage.show();
}
}