gpt4 book ai didi

java - 在Javafx中更新或删除后如何刷新数据库 TableView ?

转载 作者:行者123 更新时间:2023-12-01 17:04:04 24 4
gpt4 key购买 nike

我有两个 Controller 类( Controller 和Dailog Controller )。 CONtroller 类有 tableView。双击 Tableview 行会弹出对话框。 dialogController 类有两个按钮,即更新和删除。更新按钮更新和删除数据库中的数据。更新或删除后我想刷新表格 View 。问题是 tablview 刷新方法位于 Controller 类中。那么如何刷新呢?

    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) {


}



}

DialogController类:

[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 类中:

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);

}}

在 Controller 中:

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

关于java - 在Javafx中更新或删除后如何刷新数据库 TableView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61475471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com