标签:java javafx javafx-2 javafx-8
我正在尝试使用文本字段来过滤表格视图,我想要一个文本字段(txtSearch)来搜索’nhs number’,’first name’,’last name’和’triage category’.我已经尝试过在线实施各种解决方案并且没有运气,我仍然对这一切都不熟悉,如果这被问得很糟糕,那么道歉.任何帮助将不胜感激,我的代码如下.
公共类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)进行静态引用
解决方法:
您的TableView定义为:
@FXML
private TableView<Patient> tableView;
所以….
尝试放:(tableView.comparatorProperty());
而不是:(TableView.comparatorProperty());
并执行相同的操作:TableView.setItems(sortedData);
我建议你改变tableView以获得与TableView不同的其他东西,比如patientTable!
如果有效,请告诉我!
标签:java,javafx,javafx-2,javafx-8 来源: https://codeday.me/bug/20190708/1400994.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。