我正在尝试创建一个带有侦听器的搜索栏,以执行实时搜索和过滤javafx
. dataSearch
当我单击启动程序的执行按钮时,应该触发该方法。最初,我的搜索栏似乎工作正常,但问题是,当我从应用程序更新 Oracle 数据库时,TableView
搜索栏停止对输入关键字进行过滤。看起来该dataSearch
方法每次执行程序时只调用一次,并且TableView
修改后,ObservableList
被调用的方法generalList
不会自动更新。
代码示例:
public class App extends Application {
private TableView<General> generalTableView;
private ObservableList<General> generalList;
private TextField searchBox;
private TextField addTextField;
@Override
public void start(Stage primaryStage) {
generalTableView = new TableView<>();
TableColumn<General, String> generalColumn = new TableColumn<>("Col");
generalColumn.setCellValueFactory(new PropertyValueFactory<>("col"));
generalTableView.getColumns().add(generalColumn);
Button loadDataButton = new Button("Load Data");
loadDataButton.setOnAction(event -> {
createGeneralTable();
loadGeneralData();
dataSearch();
});
searchBox = new TextField();
searchBox.setPromptText("Search");
addTextField = new TextField();
addTextField.setPromptText("Add Data");
Button addButton = new Button("Add");
addButton.setOnAction(event -> addData(addTextField.getText()));
HBox searchBoxContainer = new HBox(searchBox);
HBox addBoxContainer = new HBox(addTextField, addButton);
VBox root = new VBox(generalTableView, loadDataButton, searchBoxContainer, addBoxContainer);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
loadDataButton.fire(); // Trigger the button action when the program starts
}
public static void main(String[] args) {
launch(args);
}
static class General {
private ObjectProperty<String> col;
public General(String col) {
this.col = new SimpleObjectProperty<>(col);
}
public String getCol() {
return col.get();
}
public void setCol(String col) {
this.col.set(col);
}
public ObjectProperty<String> colProperty() {
return col;
}
}
static class OracleConnect {
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "o4a75e");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
private void createGeneralTable() {
try {
Connection connection = OracleConnect.getConnection();
String createTableGeneral = "CREATE TABLE general (col VARCHAR2(50))";
PreparedStatement statement = connection.prepareStatement(createTableGeneral);
statement.execute();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void loadGeneralData() {
try {
Connection connection = OracleConnect.getConnection();
String query = "SELECT col FROM general";
PreparedStatement statement = connection.prepareStatement(query);
ResultSet resultSet = statement.executeQuery();
generalList = FXCollections.observableArrayList();
while (resultSet.next()) {
String col = resultSet.getString("col");
General general = new General(col);
generalList.add(general);
}
generalTableView.setItems(generalList);
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void dataSearch() {
ObservableList<General> generalList = FXCollections.observableArrayList(generalTableView.getItems());
FilteredList<General> filteredData = new FilteredList<>(generalList, b -> true);
searchBox.textProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(general -> {
if (newValue.isEmpty() || newValue.isBlank() || newValue == null) {
return true;
}
String searchKeyword = newValue.toLowerCase();
if (general.getCol().toLowerCase().contains(searchKeyword)) {
return true;
} else {
return false;
}
});
});
SortedList<General> sortedData = new SortedList<>(filteredData);
sortedData.comparatorProperty().bind(generalTableView.comparatorProperty());
generalTableView.setItems(sortedData);
}
private void addData(String data) {
try {
Connection connection = OracleConnect.getConnection();
String insertQuery ="INSERT INTO general (col) VALUES (?)";
PreparedStatement statement = connection.prepareStatement(insertQuery);
statement.setString(1, data);
statement.executeUpdate();
General general = new General(data);
generalList.add(general);
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}