在 Javafx 中更新或删除后如何刷新数据库表视图?

How to refresh database tableview after updating or Deleting In Javafx?

我有两个控制器class(控制器和 Dailog 控制器)。控制器 class 有 tableView。双击 Tableview 行,弹出对话框。 dialogController class 有两个按钮,即更新和删除。 更新按钮更新和删除数据库中的数据。更新或删除后我想刷新表视图。问题是 tablview 刷新方法在 Controller class 中。那么我该如何刷新呢?

    public class Controller implements Initializable{



    @FXML
    private TabPane tabPane;

     @FXML
     private Tab createTaskTab;

     @FXML
     private TextArea textArea;

     @FXML
     private Button saveBtn;

     @FXML
     private Tab viewTasksTab;

    @FXML
    private TableView<Task> tableView;

    @FXML
    private TableColumn<Task, Integer> idColumn;

    @FXML
    private TableColumn<Task, String> dateColumn;

    @FXML
    private TableColumn<Task, String> timeColumn;

    @FXML
    private TableColumn<Task, String> taskColumn;

    @FXML
    private TableColumn<Task, String> statusColumn;


    @FXML
    void saveTask(ActionEvent event) {

        String getTask = textArea.getText();

        if(getTask.length() > 0)
        {
            MysqlConnection mysqlConnection = new MysqlConnection();
            int count = mysqlConnection.insertTask(getTask);
            if(count > 0)
            {
                Alert alert = new Alert(AlertType.INFORMATION);
                alert.setTitle("Saved");
                alert.setContentText("Task Saved");
                alert.show();
                textArea.clear();
            }
        }
        else
        {
            Alert alert = new Alert(AlertType.WARNING);
            alert.setTitle("Empty TextArea");
            alert.setContentText("Please write the task");
            alert.show();
        }
    }

    @FXML
    public void viewTasks(Event e)
    {
        try{

            tabPane.getSelectionModel().selectedItemProperty().addListener(
                    new ChangeListener<Tab>() {

                        @Override
                        public void changed(ObservableValue<? extends Tab> observable, Tab oldValue, Tab newValue) {

                            if(newValue == viewTasksTab)
                            {

                                refreshTable();
                            }

                        }
                    });

        }catch(Exception exception)
        {
            System.out.println("Exception in viewTasks");
        }

    }

    protected void refreshTable() {

        MysqlConnection myconn = new MysqlConnection();
        idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
        dateColumn.setCellValueFactory(new PropertyValueFactory<>("date"));
        timeColumn.setCellValueFactory(new PropertyValueFactory<>("time"));
        taskColumn.setCellValueFactory(new PropertyValueFactory<>("task"));
        statusColumn.setCellValueFactory(new PropertyValueFactory<>("status"));
        tableView.setItems(myconn.fetchTasks());
    }

    @FXML
    public void onEdit(MouseEvent event)
    {
        if(event.getClickCount() == 2){

            Task selectedTask = tableView.getSelectionModel().getSelectedItem();
            Scene scene;
            Stage stage;
            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("DialogBox.fxml"));
                Parent root = (Parent) loader.load();
                DialogBoxController dialog = loader.getController();
                dialog.editTask(selectedTask);
                scene = new Scene(root);
                stage = new Stage();
                stage.initModality(Modality.APPLICATION_MODAL);
                stage.setScene(scene);
                stage.showAndWait();

            } catch (IOException e) {

                System.out.println("Exception in onEdit"+e.getMessage());
            }
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {


    }



}

对话框控制器class:

[public class DialogBoxController implements Initializable{


@FXML
private Label idLabel;

@FXML
private Label dateLabel;

@FXML
private Label timeLabel;

@FXML
private ComboBox<String> statusComboBox;

@FXML
private TextArea textAreaDialog;

@FXML
private Button updateBtn;

@FXML
private Button deleteBtn;



private void closeStage(ActionEvent event) {

    Node source = (Node) event.getSource();
    Stage stage = (Stage) source.getScene().getWindow();
    stage.close();
}

public void editTask(Task task)
{

    idLabel.setText(""+task.getId());
    dateLabel.setText(task.getDate());
    timeLabel.setText(task.getTime());
    textAreaDialog.setText(task.getTask());
    statusComboBox.setValue(task.getStatus());
    textAreaDialog.setEditable(false);
}

@FXML
public void update(ActionEvent event){

    int taskID = Integer.parseInt(idLabel.getText());
    String status = statusComboBox.getSelectionModel().getSelectedItem().toString();
    MysqlConnection myconn = new MysqlConnection();
    myconn.updateTask(taskID, status);
    closeStage(event);
}

@FXML
public void delete(ActionEvent event){

    int taskID = Integer.parseInt(idLabel.getText());
    MysqlConnection myconn = new MysqlConnection();
    myconn.deleteTask(taskID);
    closeStage(event);

}

@Override
public void initialize(URL location, ResourceBundle resources) {

    statusComboBox.getItems().addAll("Pending","Done","Aborted");


}

Snapcshot of Application

您可以将其添加到 DialogBoxController class:

public class DialogBoxController {
private Controller controller;
public void setController(Controller controller){
    this.controller = controller;
}
@FXML
public void delete(ActionEvent event){

    // Your code
    controller.refreshTable();
    closeStage(event);

}}

在控制器中:

DialogBoxController dialog = loader.getController();
    dialog.editTask(selectedTask);
    dialog.setController(this);