,我想使用基于 2 个组合框值(cmbClass、cmbaSection)的文本字段对 MySQL 中的数据进行排序
我尝试使用文本字段过滤表格视图,但是当我付款时,有人无法再次搜索。
有一些代码:
公共无效学生显示数据(){
studentListData = StudentGetData();
col_matricule_ep.setCellValueFactory(new PropertyValueFactory<>("矩阵"));
col_name_ep.setCellValueFactory(new PropertyValueFactory<>("名称"));
col_post_ep.setCellValueFactory(new PropertyValueFactory<>("post"));
col_date_ep.setCellValueFactory(new PropertyValueFactory<>("dateOfPay"));
col_paye_ep.setCellValueFactory(new PropertyValueFactory<>("金额"));
col litiges_ep.setCellValueFactory(new PropertyValueFactory<>("balance"));
tableViewPaymentEP.setItems(studentListData);
FilteredList
txt_searchStudent.textProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(管理员 -> {
if (newValue == null) {
返回真;
}
String LowerCaseFilter = newValue.toLowerCase();
if (Admins.getName().toLowerCase().contains(lowerCaseFilter)) {
返回真;
} else if (Admins.getMatricule().contains(lowerCaseFilter)) {
返回真;
} else if (Admins.getClasse().contains(LowerCaseFilter)) {
返回真;
}其他
返回错误;
});
});
SortedList
tableViewPaymentEP.setItems(sortedData);
}
public void initClassFP(MouseEvent mouseEvent) {
List = FXCollections.observable ArrayList();
尝试{
rs = con.createStatement().executeQuery(sql: "SELECT * FROM class_maternel ORDER BY id ASC");
while (rs.next()) {
list.add(rs.getString(columnLabel: "名称"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
cmb_class_ep.setItems(null);
cmb_class_ep.setItems(列表); }
此代码提供了以下改进:
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class PaymentController {
private TableView<Collect_Model> tableViewPaymentEP;
private TextField txt_searchStudent;
private ComboBox<String> cmb_class_ep;
private ObservableList<Collect_Model> studentListData;
private ObservableList<String> classList;
private Connection con;
private Statement stmt;
private ResultSet rs;
public PaymentController() {
// Initialize your database connection here
con = null; // Initialize your database connection
stmt = null; // Initialize statement
rs = null; // Initialize result set
// Initialize class list
classList = FXCollections.observableArrayList();
}
public void studentDisplayData() {
studentListData = studentGetData();
tableViewPaymentEP.setItems(studentListData);
FilteredList<Collect_Model> filteredData = new FilteredList<>(studentListData, p -> true);
txt_searchStudent.textProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(collectModel -> {
if (newValue == null || newValue.isEmpty()) {
return true;
}
String lowerCaseFilter = newValue.toLowerCase();
return collectModel.getName().toLowerCase().contains(lowerCaseFilter) ||
collectModel.getMatricule().toLowerCase().contains(lowerCaseFilter) ||
collectModel.getClasse().toLowerCase().contains(lowerCaseFilter);
});
});
SortedList<Collect_Model> sortedData = new SortedList<>(filteredData);
sortedData.comparatorProperty().bind(tableViewPaymentEP.comparatorProperty());
tableViewPaymentEP.setItems(sortedData);
}
public ObservableList<Collect_Model> studentGetData() {
ObservableList<Collect_Model> dataList = FXCollections.observableArrayList();
// Implement your database query to retrieve student payment data here
// Example:
try {
// Execute SQL query and populate dataList
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM your_table_name");
while (rs.next()) {
// Create Collect_Model object from result set and add to dataList
Collect_Model collectModel = new Collect_Model(
rs.getString("matricule"),
rs.getString("name"),
rs.getString("post"),
rs.getDate("dateOfPay"),
rs.getDouble("amount"),
rs.getDouble("balance")
);
dataList.add(collectModel);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// Close resources (statement and result set)
try {
if (stmt != null) {
stmt.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return dataList;
}
public void initClassFP() {
classList.clear(); // Clear existing class list
// Implement your database query to retrieve class names here
// Example:
try {
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT name FROM class_maternel ORDER BY id ASC");
while (rs.next()) {
classList.add(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// Close resources (statement and result set)
try {
if (stmt != null) {
stmt.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
cmb_class_ep.setItems(classList);
}
public void handleClassSelection() {
// Add listener to class ComboBox for filtering
cmb_class_ep.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
// Update filter predicate based on selected class
FilteredList<Collect_Model> filteredData = new FilteredList<>(studentListData, p -> true);
filteredData.setPredicate(collectModel -> {
if (newValue == null || newValue.isEmpty()) {
return true; // No class selected, return all data
}
return collectModel.getClasse().equals(newValue); // Filter by selected class
});
SortedList<Collect_Model> sortedData = new SortedList<>(filteredData);
sortedData.comparatorProperty().bind(tableViewPaymentEP.comparatorProperty());
tableViewPaymentEP.setItems(sortedData);
}
});
}
}