如何使用JavaFX通过在窗口2中的按钮来更新窗口1中的TableView

如何使用JavaFX通过在窗口2中的按钮来更新窗口1中的TableView,java,javafx,Java,Javafx,从昨天起我就一直在思考这个问题。现在大约10个小时了。我已经浏览了所有与我的问题有任何相似之处的现有线索,没有一条对我有帮助。我尝试过使用所谓的“回调”方法,但我无法让它继续工作 情况: 我在一个窗口的选项卡中有一个TableView 在持有TableView的场景中有一个按钮 按钮打开一个新窗口(显示一个新阶段) 在这个新阶段中,用户可以在第一个窗口中为TableView添加一个新条目 如何使它在创建TableView的新条目后,立即在TableView中显示该条目。如何在从第二个窗口(阶

从昨天起我就一直在思考这个问题。现在大约10个小时了。我已经浏览了所有与我的问题有任何相似之处的现有线索,没有一条对我有帮助。我尝试过使用所谓的“回调”方法,但我无法让它继续工作

情况:

  • 我在一个窗口的选项卡中有一个TableView
  • 在持有TableView的场景中有一个按钮
  • 按钮打开一个新窗口(显示一个新阶段)
  • 在这个新阶段中,用户可以在第一个窗口中为TableView添加一个新条目
如何使它在创建TableView的新条目后,立即在TableView中显示该条目。如何在从第二个窗口(阶段)为TableView创建新条目时使其刷新? 基本上,我需要实现的是让一个控制器由不在同一窗口中的另一个控制器触发的方法。

这是第一个阶段的控制器,在该阶段中,TableView是(“TeachehAllStableView”)。
package main.JavaFX;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.fxml.fxml;
导入javafx.fxml.fxmloader;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.stage.model;
导入javafx.stage.stage;
导入main.classes.teachehall;
导入main.sqlite.DatabaseCommunicaton;
导入java.io.IOException;
导入java.sql.SQLException;
导入java.util.ArrayList;
公共课堂讲师HollStabController{
@FXML
private TableView讲师HollStableView;
@FXML
私有按钮AddNew讲师所有按钮;
@FXML
私有按钮删除所有按钮;
私人表格第1栏;
私人表格第2栏;
ArrayList lhlist=新的ArrayList();
DatabaseCommunicaton dbcomm=新的DatabaseCommunicaton();
public void initialize()引发SQLException{
第1列=新表列(“大厅代码”);
第2列=新表列(“大厅容量”);
第1列:setCellValueFactory(新属性ValueFactory(“hallCode”);
第2列:setCellValueFactory(新属性ValueFactory(“容量”);
讲师HollStableView.getColumns().add(0,column1);
讲师HollStableView.getColumns().add(1,column2);
lhlist=dbcomm.queryLectureHalls();
讲师HollStableView.getItems().addAll(lhlist);
/**
*删除按钮功能
*/
DeleteExcellateShareButton.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
讲师霍尔selectedItem=讲师霍尔stableview.getSelectionModel().getSelectedItem();
试一试{
lhlist=dbcomm.deleteConthehall(selectedItem.getId());
讲师HollStableView.getItems().clear();
讲师HollStableView.getItems().addAll(lhlist);
}捕获(SQLE异常){
e、 printStackTrace();
}
}
});
}
/**
*使用所有现有数据库条目填充tableView
*/
public void refreshTeacheHolls()引发SQLException{
lhlist=dbcomm.queryLectureHalls();
}
public void open讲师AllInputWindow(ActionEvent ActionEvent){
阶段=新阶段();
父根=空;
试一试{
root=fxmloader.load(getClass().getResource(“讲师AllInput.fxml”);
}捕获(IOE异常){
e、 printStackTrace();
}
stage.setTitle(“讲座输入”);
舞台场景(新场景(根));
阶段。可设置大小(假);
//用于将新窗口强加于其他窗口的目的
阶段.初始模态(模态.应用\模态);
stage.show();
}
}
这是第二个窗口的控制器,通过该窗口可以创建新条目:
package main.JavaFX;
导入javafx.beans.value.ChangeListener;
导入javafx.beans.value.observeValue;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.fxml.fxml;
导入javafx.scene.control.Button;
导入javafx.scene.control.TextField;
导入javafx.stage.stage;
导入main.sqlite.DatabaseCommunicaton;
导入java.sql.SQLException;
公共类讲师AllInputController{
@FXML
私人按钮确认按钮;
@FXML
私人按钮取消按钮;
@FXML
私有文本字段hallCodeField;
@FXML
私人TextField-hallyfield;
公共无效初始化(){
cancelButton.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
Stage Stage=(Stage)cancelButton.getScene().getWindow();
stage.close();
}
});
confirButton.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
字符串hallCode=hallCodeField.getText();
int hallCapacity=Integer.parseInt(hallCapacityField.getText());
DatabaseCommunicaton dbcomm=新的DatabaseCommunicaton();
试一试{
dbcomm.ADDECTHEHALL(霍尔编码,霍尔容量);
}捕获(SQLE异常){
e、 printStackTrace();
}
Stage Stage=(Stage)confirButton.getScene().getWindow();
stage.close();
}
});
}
}
我已经实现了详细的答案。 我所做的主要工作是创建了一个新类,用于“托管”Observableis
package main.JavaFX;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Modality;
import javafx.stage.Stage;
import main.classes.LectureHall;
import main.sqlite.DatabaseCommunicaton;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

public class LectureHallsTabController {
    @FXML
    private TableView<LectureHall> lectureHallsTableView;
    @FXML
    private Button addNewLectureHallButton;
    @FXML
    private Button deleteLectureHallButton;

    private TableColumn<LectureHall, String> column1;
    private TableColumn<LectureHall, Integer> column2;

    ArrayList<LectureHall> lhlist = new ArrayList<>();
    DatabaseCommunicaton dbcomm = new DatabaseCommunicaton();

    public void initialize() throws SQLException {
        column1 = new TableColumn<>("Hall code");
        column2 = new TableColumn<>("Hall capacity");
        column1.setCellValueFactory(new PropertyValueFactory<>("hallCode"));
        column2.setCellValueFactory(new PropertyValueFactory<>("capacity"));
        lectureHallsTableView.getColumns().add(0, column1);
        lectureHallsTableView.getColumns().add(1, column2);
        lhlist = dbcomm.queryLectureHalls();
        lectureHallsTableView.getItems().addAll(lhlist);

        /**
         * Delete button functionality
         */
        deleteLectureHallButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                LectureHall selectedItem = lectureHallsTableView.getSelectionModel().getSelectedItem();
                try {
                    lhlist = dbcomm.deleteLectureHall(selectedItem.getId());
                    lectureHallsTableView.getItems().clear();
                    lectureHallsTableView.getItems().addAll(lhlist);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });

    }
    /**
     * populates the tableView with all the existing database entries
     */
    public void refreshLectureHalls() throws SQLException {
        lhlist = dbcomm.queryLectureHalls();
    }

    public void openLectureHallInputWindow(ActionEvent actionEvent) {
        Stage stage = new Stage();
        Parent root = null;
        try {
            root = FXMLLoader.load(getClass().getResource("lectureHallInput.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        stage.setTitle("Lecture Input");
        stage.setScene(new Scene(root));
        stage.setResizable(false);

        // Serves the purpose of the new window being imposed over the other window
        stage.initModality(Modality.APPLICATION_MODAL);

        stage.show();

    }

}
package main.JavaFX;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import main.sqlite.DatabaseCommunicaton;

import java.sql.SQLException;

public class LectureHallInputController {

    @FXML
    private Button confirmButton;
    @FXML
    private Button cancelButton;
    @FXML
    private TextField hallCodeField;
    @FXML
    private TextField hallCapacityField;

    public void initialize() {

        cancelButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                Stage stage = (Stage) cancelButton.getScene().getWindow();
                stage.close();
            }
        });
   
        confirmButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                String hallCode = hallCodeField.getText();
                int hallCapacity = Integer.parseInt(hallCapacityField.getText());
                DatabaseCommunicaton dbcomm = new DatabaseCommunicaton();
                try {
                    dbcomm.addLectureHall(hallCode, hallCapacity);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                Stage stage = (Stage) confirmButton.getScene().getWindow();
                stage.close();
            }
        });
    }
}
public class LectureHallData{
    DatabaseCommunicaton dbcomm = new DatabaseCommunicaton();

    public ObservableList<LectureHall> getLectureHallList() {
        return lectureHallList;
    }

    public ObservableList<LectureHall> lectureHallList = FXCollections.observableArrayList(lectureHall -> new Observable[]{lectureHall.codeProperty(), lectureHall.capacityProperty()});
    private ObjectProperty<LectureHall> currentLectureHall = new SimpleObjectProperty<>();

    public ObservableList<LectureHall> loadLectureHalls() throws SQLException {
        ObservableList<LectureHall> lectureHallObservableList = FXCollections.observableArrayList(dbcomm.queryLectureHalls());
        this.lectureHallList = lectureHallObservableList;
        return lectureHallObservableList;
    }

    public void refreshLectureHalls() throws SQLException {
        this.lectureHallList.clear();
        this.lectureHallList.addAll(FXCollections.observableArrayList(dbcomm.queryLectureHalls()));
    }

    public void addLectureHall(String hallCode, int hallCapacity) throws SQLException {
        dbcomm.addLectureHall(hallCode, hallCapacity);
        refreshLectureHalls();
    }

}
public class LectureHallsTabController {
    @FXML
    private TableView<LectureHall> lectureHallsTableView;
    @FXML
    private Button addNewLectureHallButton;
    @FXML
    private Button deleteLectureHallButton;

    private TableColumn<LectureHall, String> column1;
    private TableColumn<LectureHall, Integer> column2;

    ArrayList<LectureHall> lhlist = new ArrayList<>();
    DatabaseCommunicaton dbcomm = new DatabaseCommunicaton();
    private LectureHallData data;

    public void initialize() throws SQLException {

        initData();


    }

  public void openLectureHallInputWindow(ActionEvent actionEvent) {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("lectureHallInput.fxml"));
        Stage stage = new Stage();
        Parent root = null;
        try {
            root = loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }

        LectureHallInputController lectureHallInputController = loader.getController();
        lectureHallInputController.initData(data);
        stage.setTitle("Lecture Input");
        stage.setScene(new Scene(root));
        stage.setResizable(false);

        // Serves the purpose of the new window being imposed over the other window
        stage.initModality(Modality.APPLICATION_MODAL);

        stage.show();

    }

    public void initData() throws SQLException {
        // ensure data is only set once:
        if (this.data != null) {
            throw new IllegalStateException("Data can only be initialized once");
        }
        this.data=new LectureHallData();
        column1 = new TableColumn<>("Hall code");
        column2 = new TableColumn<>("Hall capacity");
        column1.setCellValueFactory(new PropertyValueFactory<>("hallCode"));
        column2.setCellValueFactory(new PropertyValueFactory<>("capacity"));
        lectureHallsTableView.getColumns().add(0, column1);
        lectureHallsTableView.getColumns().add(1, column2);

        lectureHallsTableView.getItems().clear();
        data.loadLectureHalls();
        lectureHallsTableView.setItems(data.getLectureHallList());
    }

}
public class LectureHallInputController {

    @FXML
    private Button confirmButton;
  
   ...

    private LectureHallData data;

    public void initialize() {

    ...

    confirmButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                String hallCode = hallCodeField.getText();
                int hallCapacity = Integer.parseInt(hallCapacityField.getText());
                try {
                    data.addLectureHall(hallCode, hallCapacity);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                Stage stage = (Stage) confirmButton.getScene().getWindow();
                stage.close();
            }
        });
    }

    public void initData(LectureHallData data) {
        this.data = data;
    }

}