2015-04-30 66 views
4

我想使用文本字段來過濾表格視圖,我想要一個文本字段(txtSearch)來搜索'nhs數字','名字','姓氏'和「分類類別」。我已經嘗試過在網上實施各種解決方案,但沒有運氣,我仍然對這一切都很陌生,所以很抱歉,如果這個問題不好。任何幫助將不勝感激,我的代碼如下。Java FX過濾器表格視圖

公共類QueueTabPageController實現Initializable {

@FXML 
private TableView<Patient> tableView; 

@FXML 
private TableColumn<Patient, String> NHSNumberColumn; 

@FXML 
private TableColumn<Patient, String> firstNameColumn; 

@FXML 
private TableColumn<Patient, String> lastNameColumn; 

@FXML 
private TableColumn<Patient, String> timeEnteredColumn; 

@FXML 
private TableColumn<Patient, String> triageAssessmentColumn; 

@FXML 
private TextField filterField; 

@FXML 
private QueueTabPageController queueTabPageController; 

private ObservableList<Patient> tableData; 

// public static LinkedList<Patient> displayQueue; 


/** 
    * Initializes the controller class. This method is automatically called 
    * after the fxml file has been loaded. 
    * 
    * Initializes the table columns and sets up sorting and filtering. 
    */ 
    @Override 
    public void initialize(URL arg0, ResourceBundle arg1) { 

     assert tableView != null : "fx:id=\"tableView\" was not injected: check your FXML file 'FXMLQueueTabPage.fxml'"; 

     NHSNumberColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("nhsNumber")); 
     firstNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("firstName")); 
     lastNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("lastName")); 
     timeEnteredColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("timeEnteredString")); 
     triageAssessmentColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("triage")); 

     // display the current queue to screen when opening page each time 
     displayQueue(Queue.queue); 

     // 0. Initialize the columns. 
     //firstNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("firstName")); 
     //lastNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("lastName")); 

     // 1. Wrap the ObservableList in a FilteredList (initially display all 
     // data). 
     FilteredList<Patient> filteredData = new FilteredList<>(tableData, 
       p -> true); 

     // 2. Set the filter Predicate whenever the filter changes. 
     filterField.textProperty().addListener(
       (observable, oldValue, newValue) -> { 
        filteredData.setPredicate(Patient -> { 
         // If filter text is empty, display all persons. 
          if (newValue == null || newValue.isEmpty()) { 
           return true; 
          } 

          // Compare first name and last name of every person 
          // with filter text. 
          String lowerCaseFilter = newValue.toLowerCase(); 

          if (Patient.getFirstName().toLowerCase() 
            .contains(lowerCaseFilter)) { 
           return true; // Filter matches first name. 
          } else if (Patient.getLastName().toLowerCase() 
            .contains(lowerCaseFilter)) { 
           return true; // Filter matches last name. 
          } 
          return false; // Does not match. 
         }); 
       }); 

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

     // 4. Bind the SortedList comparator to the TableView comparator. 
     sortedData.comparatorProperty().bind(tableView.comparatorProperty()); 

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

    } 

我在這裏得到一個錯誤(步驟4):

(TableView.comparatorProperty()); 

,並說(第5步)

TableView.setItems(sortedData); 

: 無法對t中的非靜態方法setItems(ObservableList)進行靜態引用他鍵入的TableView

+0

「我嘗試過在線實施各種解決方案,但沒有運氣。」請顯示你的嘗試,並解釋出了什麼問題。 –

+0

@James_D我主要嘗試這個教程:http://code.makery.ch/blog/javafx-8-tableview-sorting-filtering但是當我在文本字段中輸入時,它從未被解僱。 – Davidaj

+0

該教程中的代碼適合我。 –

回答

1

TableView被定義爲:

@FXML 
private TableView<Patient> tableView; 

所以....

嘗試把:(tableView.comparatorProperty());

代替:(TableView.comparatorProperty());

,做同樣的附:TableView.setItems(sortedData);

我建議您將tableView更改爲與TableView更類似的內容,例如patientTable

讓我知道它是否有效!

+0

修正了沒有錯誤但仍然沒有發射的情況,是否可以將表格初始化爲空,直到添加患者爲止?所以它沒有預先填充?對不起,這是新的。 – Davidaj