2015-07-04 119 views
0

我正在创建一个javafx项目,我应该从xml过滤数据并将其显示在表中。有一个textfiled,我试图用作一个搜索字段,无论输入什么都通过表内的条目进行搜索,并匹配incase匹配只有那些具有正匹配的条目。 这是我的模型类:无法过滤javafx中的表

我正在使用我的模型类的包装从XML文件中提取数据。

模型类:

private final StringProperty fileSubject; 
private final StringProperty fileDate; 
private final StringProperty fileRemarks; 

public void setFileSubject(String fileSubject){ 
    this.fileSubject.set(fileSubject); 
} 
public String getFileSubject() { 
    return fileSubject.get(); 
} 
public StringProperty fileSubjectProperty() { 
    return fileSubject; 
} 

public void setFileDate(String fileDate){ 
    this.fileDate.set(fileDate); 
} 
public String getFileDate() { 
    return fileDate.get(); 
} 
public StringProperty fileDateProperty() { 
    return fileDate; 
} 

public void setFileRemarks(String fileRemarks){ 
    this.fileRemarks.set(fileRemarks); 
} 
public String getFileRemarks() { 
    return fileRemarks.get(); 
} 
public StringProperty fileRemarksProperty(){ 
    return fileRemarks; 
} 

/** 
* Default constructor. 
*/ 
public FileModel(){ 
    this(null,null, null); 
} 

/** 
*Constructor with some initial data. 
* 
*@param 
* 
*/ 

public FileModel(String fileSubject, String fileDate, String fileRemarks){ 
    this.fileSubject = new SimpleStringProperty(fileSubject); 
    this.fileDate = new SimpleStringProperty(fileDate); 
    this.fileRemarks = new SimpleStringProperty(fileRemarks); 
} 

} 

包装类:

@XmlRootElement(name = "files") 
public class FileListWrapper { 

private List<FileModel> files; 

@XmlElement(name = "file") 
public List<FileModel> getFiles() { 
    return files; 
} 

public void setFiles(List<FileModel> files) { 
    this.files = files; 
} 

} 

这就是我正在解编的:

JAXBContext context = JAXBContext 
       .newInstance(FileListWrapper.class); 
Unmarshaller um = context.createUnmarshaller(); 
File file = new File("xxxxxxxxxx/xxx/x/xxxx/x.xml"); 
     // Reading XML from the file and unmarshalling. 
     FileListWrapper wrapper = (FileListWrapper) um.unmarshal(file); 
     fileData.clear(); 
     fileData.addAll(wrapper.getFiles()); 

现在我将提取出的数据进入的tableview和稍后在其上执行搜索如下:

private FileListWrapper wrapper; 
private ObservableList<FileModel> masterData = FXCollections.observableArrayList(); 
@FXML 
private void initialize(){ 
    //Initialize the file table with the two columns. 
    fileSubjectColumn.setCellValueFactory(cellData ->   cellData.getValue().fileSubjectProperty()); 
    fileDateColumn.setCellValueFactory(cellData -> cellData.getValue().fileDateProperty()); 
// ******* BUT AT THIS POINT IT GIVES THE NULL POINTER EXCEPTION.****** 
masterData.addAll(wrapper.getFiles()); 

FilteredList<FileModel> filteredData = new FilteredList<>(masterData, p -> true); 

//This is my textfield which is to be used for the search. 
     filterField.textProperty().addListener((observable, oldValue, newValue) -> { 
      filteredData.setPredicate(file -> { 
       // If filter text is empty, display all data. 
       if (newValue == null || newValue.isEmpty()) { 
        return true; 
       } 


       String lowerCaseFilter = newValue.toLowerCase(); 

       if (file.getFileSubject().toLowerCase().indexOf(lowerCaseFilter) != -1) { 
        return true; // Filter matches the File Subject. 
       } else if (file.getFileDate().toLowerCase().indexOf(lowerCaseFilter) != -1) { 
        return true; // Filter matches the File Date. 
       } 
       return false; // Does not match. 
      }); 
     }); 

     // 3. Wrap the FilteredList in a SortedList. 
     SortedList<FileModel> sortedData = new SortedList<>(filteredData); 

     // 4. Bind the SortedList comparator to the TableView comparator. 
     // Otherwise, sorting the TableView would have no effect. 
     sortedData.comparatorProperty().bind(fileTable.comparatorProperty()); 

     // 5. Add sorted (and filtered) data to the table. 
     fileTable.setItems(sortedData); 
} 

它给了我一个空指针异常,当我做

masterData.addAll(wrapper.getFiles()); 

我怎样才能使它正确吗?

+1

“masterData”为“null”,或“wrapper”为“null”(或可能包含两者)。由于您不会在初始化这些代码的位置显示代码,因此很难提供帮助。 –

+0

@James_D,我已经更新了我的代码。 – user3626720

+0

看起来你还没有初始化'wrapper' –

回答

0

调试只是帮助我修复它。我遇到的问题是我正在使用init()方法进行过滤,但那只是定义表的结构,在init()方法之后调用的方法中,我将数据提供给表。所以,我在上面的代码中筛选了一个null ObservableList。我的错误是,由于某些限制,我没有在问题中提到我的那种方法。