2016-11-18 56 views
0

我想篩選和顯示FX Tableview的項目數。如何檢測JavaFx中調整tableview項目數量?

有一些按鈕和列出一個spesific數據 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

Bindings class提供了size方法,使您可以創建一個ObservableList大小的結合。假設您修改現有列表,並用新的每次過濾器(例如,使用FilteredList)時並沒有取代它,你可以用它來了Label文本綁定:

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

編輯

在您的代碼中,您替換項目列表。的prequesite的items沒有被替換不給...

你可以一個監聽器添加到項目屬性,而不是每次都

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

重新綁定Label文本但是你也可以修改單個列表以包含源數據,而不是每次重新創建列表:

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

謝謝,但不工作時,我過濾值。 –

+0

@enes你會替換你的列表,這就是爲什麼它不起作用。如果你沒有聽到'TableView'的'items'屬性的變化,並且每次替換時都要重新綁定'Label'的'text'屬性,它就應該工作;編輯答案。 – fabian

+0

謝謝你,這對我來說很有用! :) –

相關問題