Javafx tableview 复选框隐藏不需要的行?
Javafx tableview checkbox to hide unwanted rows?
我正在尝试通过使用复选框从表视图(我在 scenebuilder 中制作)中隐藏一些行来做一些报告。如果 tableview 中的对象在列中没有特定的字符串,它就会变得不可见。示例:通过单击复选框,隐藏 table_adress 中所有不具有确切值“Barcelona”的行。我试着这样做,但我一直让自己复杂化。有什么想法吗?
函数
@FXML
void CakeRequestsFromBarcelona(ActionEvent event) throws IOException {
for(Object cake:table.getItems()) {
for (TableColumn column : table.getColumns()) {
//not sure how to write it
}
}
}
初始化
@FXML
private TableColumn<CakeRequest, Integer> table_ID;
@FXML
private TableColumn<CakeRequest, String> table_adress;
@FXML
private TableColumn<CakeRequest, String> table_design;
@FXML
private TableColumn<CakeRequest, String> table_flavour;
@FXML
private TableColumn<CakeRequest, String> table_model;
@FXML
private TableColumn<CakeRequest, String> table_name;
@FXML
private TableColumn<CakeRequest, String> table_phonenumber;
@FXML
private TableView<CakeRequest> table;
public void initialize(){
//cakeRequest
table_ID.setCellValueFactory(
p-> new SimpleIntegerProperty(p.getValue().getID()).asObject()
);
table_name.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getOwnerName())
);
table_adress.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getOwnerAddress())
);
table_phonenumber.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getPhoneNumber())
);
table_flavour.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getFlavour())
);
table_design.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getDesign())
);
table_model.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getModel())
);
}
CakeRequest
public class CakeRequest implements Identifiable<Integer>, Serializable{
private int ID;
private String OwnerName;
private String OwnerAddress;
private String PhoneNumber;
private String Model;
private String Flavour;
private String Design;
使用 FilteredList
并在复选框为 checked/unchecked 时更改谓词:
private ObservableList<CakeRequest> data = FXCollections.observableArrayList();
private FilteredList<CakeRequest> filteredData = new FilteredList<>(data, request -> true);
public void initialize(){
// existing code...
table.setItems(filteredData);
}
@FXML
void cakeRequestsFromBarcelona(ActionEvent event) {
if (checkBox.isChecked()) {
filteredData.setPredicate(request -> request.getOwnerAddress().contains("Barcelona"));
} else {
filteredData.setPredicate(request -> true);
}
}
我正在尝试通过使用复选框从表视图(我在 scenebuilder 中制作)中隐藏一些行来做一些报告。如果 tableview 中的对象在列中没有特定的字符串,它就会变得不可见。示例:通过单击复选框,隐藏 table_adress 中所有不具有确切值“Barcelona”的行。我试着这样做,但我一直让自己复杂化。有什么想法吗?
函数
@FXML
void CakeRequestsFromBarcelona(ActionEvent event) throws IOException {
for(Object cake:table.getItems()) {
for (TableColumn column : table.getColumns()) {
//not sure how to write it
}
}
}
初始化
@FXML
private TableColumn<CakeRequest, Integer> table_ID;
@FXML
private TableColumn<CakeRequest, String> table_adress;
@FXML
private TableColumn<CakeRequest, String> table_design;
@FXML
private TableColumn<CakeRequest, String> table_flavour;
@FXML
private TableColumn<CakeRequest, String> table_model;
@FXML
private TableColumn<CakeRequest, String> table_name;
@FXML
private TableColumn<CakeRequest, String> table_phonenumber;
@FXML
private TableView<CakeRequest> table;
public void initialize(){
//cakeRequest
table_ID.setCellValueFactory(
p-> new SimpleIntegerProperty(p.getValue().getID()).asObject()
);
table_name.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getOwnerName())
);
table_adress.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getOwnerAddress())
);
table_phonenumber.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getPhoneNumber())
);
table_flavour.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getFlavour())
);
table_design.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getDesign())
);
table_model.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getModel())
);
}
CakeRequest
public class CakeRequest implements Identifiable<Integer>, Serializable{
private int ID;
private String OwnerName;
private String OwnerAddress;
private String PhoneNumber;
private String Model;
private String Flavour;
private String Design;
使用 FilteredList
并在复选框为 checked/unchecked 时更改谓词:
private ObservableList<CakeRequest> data = FXCollections.observableArrayList();
private FilteredList<CakeRequest> filteredData = new FilteredList<>(data, request -> true);
public void initialize(){
// existing code...
table.setItems(filteredData);
}
@FXML
void cakeRequestsFromBarcelona(ActionEvent event) {
if (checkBox.isChecked()) {
filteredData.setPredicate(request -> request.getOwnerAddress().contains("Barcelona"));
} else {
filteredData.setPredicate(request -> true);
}
}