2016-10-14 2 views
2

example과 같은 JavaFx의 텍스트 필드로 TableView에서 필터를 수행하려고합니다.JavaFx FilteredList .setPredicate가 작동하지 않습니다.

불행히도 나는 진실한 결과를 얻지 못했습니다. .setPredicateFilteredList이면 실행 중이 아니거나 디버그 모드에서 건너 뜁니다.

public class App extends Application { 

TableView tableScanSystemForSnapshots = new TableView<>(); 
private ObservableList<Snapshot> snapshots = FXCollections.observableArrayList(); 
private FilteredList<Snapshot> filteredListSnapshots = new FilteredList<>(snapshots, s -> true); 

public App() { 

} 

@Override 
public void start(Stage primaryStage) throws Exception { 
    //Table View Snapshots 
    tableScanSystemForSnapshots.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); //make multiple selection in TableView possible 

    //Initialize Columns 
    TableColumn<Snapshot, String> colVmName = new TableColumn<>("VM Name"); 
    TableColumn<Snapshot, String> colSnapshotName = new TableColumn<>("Snapshot Name"); 
    TableColumn<Snapshot, String> colSnapshotDescription = new TableColumn<>("Description"); 
    TableColumn<Snapshot, String> colCreatedTime = new TableColumn<>("created Time"); 
    TableColumn<Snapshot, String> colCreatedBy = new TableColumn<>("created by"); 

    //Initialize ValueFactory - name has to be the same as in Class: Snapshot 
    colVmName.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("vmName")); 
    colSnapshotName.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("snapshotName")); 
    colSnapshotDescription.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("description")); 
    colCreatedTime.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("createdTime")); 
    colCreatedBy.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("createdBy")); 

    //TextField Filter 
    TextField txtFilter = new TextField(); 
    txtFilter.textProperty().addListener(((observable, oldValue, newValue) -> { 
     filteredListSnapshots.setPredicate(s -> { 
      if(newValue == null||newValue.isEmpty()){ 
       return true; 
      } 
      //Compare 
      String lowerCaseFilter = newValue.toLowerCase(); 

      if (s.getVmName().toLowerCase().contains(lowerCaseFilter)){return true;} //Filter matches VM Name 
      if (s.getSnapshotName().toLowerCase().contains(lowerCaseFilter)){return true;} //Filter matches Snapshot Name 
      return false; // Does not match 
     }); 

     SortedList<Snapshot> sortedData = new SortedList<Snapshot>(filteredListSnapshots); 
     sortedData.comparatorProperty().bind(tableScanSystemForSnapshots.comparatorProperty()); 
     tableScanSystemForSnapshots.setItems(sortedData); 
    })); 
+1

코드가 나를 위해 작동합니다. (tableScanSystemForSnapshots의 유형을 'TableView '으로 변경하여 컴파일합니다). 어떤 Java 버전을 사용하고 있습니까? – VGR

+0

안녕하세요. VRG, 아래 라인을 편집 했는데도 여전히 동일한 효과가 있습니다. 1.8level8을 사용합니다. TableView tableScanSystemForSnapshots = new TableView (); – raphi

+1

문제를 재현 할 수없는 경우 도움을 받기가 어렵습니다. 코드를 편집하여 문제를 나타내는 짧고 완전한 독립 실행 형 프로그램이되도록하십시오. – VGR

답변

-1

ObservableList 스냅 샷이 올바르게 채워지지 않았으며 올바르게 수정되었습니다.

관련 문제