enes enes - 19 days ago 8
Java Question

How to detect a tableview items count resize in JavaFx?

I am trying to filter and show items count with FX Tableview.

There are some buttons and txt field for listing a spesific data
here is

text input: filtering

clear button: gettingenter code here default values

this month button: getting this month values

data picker: for getting any day values

There is a label for showing table items size

public Label lblRecordSize;


My problem;
i don't want to
lblRecordSize.settext()
under the every action for filtering.
Is there a listener like
tableview.onItemSizeChangeListener()
etc.

UPDATED:

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

}

Answer

The Bindings class provides a size method that allows you to create a binding for the size of a ObservableList. Assuming you modify the existing list and do not replace it with a new one every time you filter (e.g. using FilteredList), you can use this to bind the Label text:

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

Edit

In your code you replace the items list. The prequesite of the items not being replaced is not given...

You could add a listener to the item property instead and rebind the Label text every time

// before setting items the first time
tblvRecords.itemsProperty().addListener((observable, oldItems, newItems) -> 
                                           lblRecordSize.textProperty().bind(
                                                   Bindings.size(newItems).asString()));

However you could also modify a single list to contain the source data instead of recreating the lists every time:

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