【发布时间】:2015-07-10 16:09:08
【问题描述】:
我正在尝试使用文本字段来过滤表格视图,我想要一个文本字段 (txtSearch) 来搜索“nhs 编号”、“名字”、“姓氏”和“分类类别” .我已经尝试在网上实施各种解决方案,但没有运气,我对这一切还是陌生的,如果问得不好,我深表歉意。任何帮助将不胜感激,我的代码如下。
公共类 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);
说: 无法从 TableView 类型中对非静态方法 setItems(ObservableList) 进行静态引用
【问题讨论】:
-
“我尝试过在线实施各种解决方案,但没有成功。”请展示您尝试过的内容并解释出了什么问题。
-
@James_D 我主要尝试了这个教程:code.makery.ch/blog/javafx-8-tableview-sorting-filtering 但是当我在文本字段中输入时它从未触发过。
-
该教程中的代码对我来说运行良好。
-
@James_D 我似乎无法在我的代码中实现它,有什么帮助吗?
-
只需执行与您链接的优秀教程相同的操作即可。如果您无法使其工作,请发布您尝试过的代码。您的所有代码都与过滤表格无关。
标签: java javafx javafx-2 javafx-8