我正在尝试过滤并使用FX Tableview显示项目数。
有一些按钮和txt字段用于列出特定数据 here is
文字输入:过滤
清除按钮:此处的gettingenter代码默认值为
本月按钮:获取本月价值
数据选择器:获取任何一天的值
有一个标签用于显示表项目大小
public Label lblRecordSize;
我的问题;
我不想在每个动作下lblRecordSize.settext()
进行过滤。
是否有像tableview.onItemSizeChangeListener()
等听众。
更新:
public class CenterDbController implements Initializable {
public AnchorPane centerDbPanelPane;
/** */
public TextField txtSerialNo;
public ComboBox<EModemModel> cbxModemModel;
public Label lblRecordSize;
public DatePicker dpStartDate;
public DatePicker dpEndDate;
public Button btnShow;
public Button btnClear;
/** */
public TableView<RecordedTest> tblvRecords;
public TableColumn colRecordId;
public TableColumn colOfficeId;
public TableColumn colCompany;
public TableColumn colModemSerialNumber;
public TableColumn colModemBrand;
public TableColumn colModemModel;
public TableColumn colSoftwareVersion;
public TableColumn colAccessTest;
public TableColumn colSoftwareTest;
public TableColumn colDhcpTest;
public TableColumn colWifiTest;
public TableColumn colInternetTest;
public TableColumn colResetTest;
public TableColumn colTestResult;
public TableColumn colSendStatus;
public TableColumn colRecordDate;
public TableColumn colTestDetails;
/** */
private IRecordedTestService recordedTestService;
private FilteredList<RecordedTest> filteredList;
@Override
public void initialize(URL location, ResourceBundle resources) {
initCellFactories();
recordedTestService = UtilsForSpring.getSingleBeanOfType(IRecordedTestService.class);
filteredList = new FilteredList<>(FXCollections.observableList(recordedTestService.getThisMonthRecords()), s -> true);
tblvRecords.setItems(filteredList);
lblRecordSize.textProperty().bind(Bindings.size(tblvRecords.getItems()).asString("%s"));
// lblRecordSize.setText(filteredList.size() + "");
filteredList.addListener(new ListChangeListener<RecordedTest>() {
@Override
public void onChanged(Change<? extends RecordedTest> c) {
// lblRecordSize.setText(filteredList.size() + "");
}
});
cbxModemModel.getItems().addAll(EModemModel.values());
}
private void initCellFactories() {
colRecordId.setCellValueFactory(new PropertyValueFactory<RecordedTest, String>("recordId"));
colOfficeId.setCellValueFactory(new PropertyValueFactory<>("officeId"));
colCompany.setCellValueFactory(new PropertyValueFactory<>("company"));
colModemSerialNumber.setCellValueFactory(new PropertyValueFactory<>("modemSerialNumber"));
colModemBrand.setCellValueFactory(new PropertyValueFactory<>("modemBrand"));
colModemModel.setCellValueFactory(new PropertyValueFactory<>("modemModel"));
colSoftwareVersion.setCellValueFactory(new PropertyValueFactory<>("softwareVersion"));
colAccessTest.setCellValueFactory(new PropertyValueFactory<>("accessTest"));
colSoftwareTest.setCellValueFactory(new PropertyValueFactory<>("softwareTest"));
colDhcpTest.setCellValueFactory(new PropertyValueFactory<>("dhcpTest"));
colWifiTest.setCellValueFactory(new PropertyValueFactory<>("wifiTest"));
colInternetTest.setCellValueFactory(new PropertyValueFactory<>("internetTest"));
colResetTest.setCellValueFactory(new PropertyValueFactory<>("resetTest"));
colTestResult.setCellValueFactory(new PropertyValueFactory<>("testResult"));
colSendStatus.setCellValueFactory(new PropertyValueFactory<>("sendStatus"));
colRecordDate.setCellValueFactory(new PropertyValueFactory<>("recordDate"));
colTestDetails.setCellValueFactory(new PropertyValueFactory<>("testDetails"));
}
public void btnClearOnClickAction(ActionEvent e) {
txtSerialNo.clear();
cbxModemModel.getSelectionModel().clearSelection();
dpEndDate.setValue(null);
dpStartDate.setValue(null);
filteredList = new FilteredList<>(FXCollections.observableList(recordedTestService.getThisMonthRecords()), s -> true);
tblvRecords.setItems(filteredList);
// lblRecordSize.setText(filteredList.size() + "");
}
public void btnShowOnClickAction(ActionEvent e) {
if (dpStartDate.getValue() != null && dpEndDate != null) {
filteredList = new FilteredList<>(FXCollections.observableList(recordedTestService.getBetweenRecords(dpStartDate.getValue(), dpEndDate.getValue())));
tblvRecords.setItems(filteredList);
}
}
public void tableOnSortListener() {
// lblRecordSize.setText(tblvRecords.getItems().size() + "");
}
public void txtSerialNoOnKeyPress() {
txtSerialNo.textProperty().addListener(observable -> {
String filter = txtSerialNo.getText();
if (filter == null || filter.length() == 0) {
filteredList.setPredicate(s -> true);
} else {
filteredList.setPredicate(s -> s.getModemSerialNumber().contains(filter));
}
});
}
public void cbxModemModelOnValueChange() {
String filter = cbxModemModel.getSelectionModel().getSelectedItem().toString();
if (filter == null || filter.length() == 0) {
filteredList.setPredicate(s -> true);
} else {
filteredList.setPredicate(s -> s.getModemModel().equalsIgnoreCase(filter));
}
}
}
答案 0 :(得分:3)
Bindings
class提供size
方法,允许您为ObservableList
的大小创建绑定。假设您修改现有列表,并且每次过滤时都不要用新列表替换它(例如使用FilteredList
),您可以使用它来绑定Label
文本:
// after assigning the items
lblRecordSize.textProperty().bind(Bindings.size(tableview.getItems()).asString("Record count: %s"));
修改强>
在您的代码中,您可以替换项目列表。 items
未被替换的优先权没有给出......
您可以向item属性添加侦听器,并每次重新绑定Label
文本
// before setting items the first time
tblvRecords.itemsProperty().addListener((observable, oldItems, newItems) ->
lblRecordSize.textProperty().bind(
Bindings.size(newItems).asString()));
但是,您也可以修改单个列表以包含源数据,而不是每次都重新创建列表:
private final ObservableList<RecordedTest> data = FXCollections.observableArrayList();
private final FilteredList<RecordedTest> filteredList = new FilteredList<>(data);
@Override
public void initialize(URL location, ResourceBundle resources) {
initCellFactories();
recordedTestService = UtilsForSpring.getSingleBeanOfType(IRecordedTestService.class);
filteredList.setPredicate(null);
data.setAll(recordedTestService.getThisMonthRecords());
tblvRecords.setItems(filteredList);
lblRecordSize.textProperty().bind(Bindings.size(filteredList).asString());
...
}
...
public void btnClearOnClickAction(ActionEvent e) {
...
filteredList.setPredicate(null);
data.setAll(recordedTestService.getThisMonthRecords()));
}