gpt4 book ai didi

java - 如何在 JavaFx 中检测 tableview 项目计数调整大小?

转载 作者:行者123 更新时间:2023-11-29 08:40:24 25 4
gpt4 key购买 nike

我正在尝试使用 FX Tableview 过滤和显示项目计数。

有一些按钮和txt字段用于列出特定数据 here is

文本输入:过滤

清除按钮:在此处输入代码默认值

本月按钮:获取本月值

数据选择器:用于获取任何一天的值

有一个显示表格项目大小的标签

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));
}
}

}

最佳答案

Bindings class提供了一个 size 方法,允许您为 ObservableList 的大小创建绑定(bind)。假设您在每次过滤时修改现有列表并且不将其替换为新列表(例如使用 FilteredList),您可以使用它来绑定(bind) Label 文本:

// after assigning the items
lblRecordSize.textProperty().bind(Bindings.size(tableview.getItems()).asString("Record count: %s"));

编辑

在您的代码中,您替换了项目列表。 items 未被替换的先决条件未给出...

您可以改为向 item 属性添加一个监听器,并且每次都重新绑定(bind) 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()));
}

关于java - 如何在 JavaFx 中检测 tableview 项目计数调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40671421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com