我正在try 创建一个带有监听程序的搜索栏,以便对javafx
中的数据进行实时搜索和过滤.当我单击启动程序的Execute按钮时,应该会触发dataSearch
方法.起初,我的搜索栏似乎工作得很好,但问题是,当我从应用程序更新我的Oracle数据库时,TableView
不再根据搜索栏中的输入关键字进行过滤.似乎在每次执行程序时,dataSearch
方法只被调用一次,并且在TableView
被修改之后,被调用的ObservableList
不会自动更新.
代码示例:
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();
}
}
}