在另一个类中更改场景时,如何调用类的构造函数(控制器)? (Java,JavaFX)

时间:2019-03-13 16:48:29

标签: java sql sqlite javafx constructor

我正在开发一个小型DVD管理程序,目前正在一个地方闲逛。 简短的介绍: ActorController类是FXML文件Actor.fxml的控制器。如果我双击某个条目,则应调用函数openEditEntryWindow(),然后更改场景。到这里为止,一切正常。然后,控制器EditEntryActor应该负责类EditEntryActor.fxml。但是,现在如何将数据从一个类转移到另一个类呢?我曾想过在调用对象时将其移交给控制器,但是我不知道它应该如何工作(如果可以的话)。如果有人可以帮助我,那将真的很棒。在下面的代码中:

SchauspielerController:(ActorController)

public class SchauspielerController implements Initializable {
    private SchauspielerDAO doaControl;
    @FXML
    protected Button buttonMenu;
    @FXML
    protected TableView<Schauspieler> tableViewActor = new TableView<>();
    @FXML
    protected TableColumn<Schauspieler, Long> tableColID = new TableColumn<>();
    @FXML
    protected TableColumn<Schauspieler, String> tableColName = new TableColumn<>();


    public SchauspielerController(){
        doaControl = new SchauspielerDAO();
    }

    public void openNewEntryWindow() throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("NewEntry.fxml"));
        Parent root = loader.load();
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle("New Actor entry!");
        stage.setScene(new Scene(root, Main.width - 200, Main.height - 200));
        stage.showAndWait();
        updateTable();
    }

    public void openEditEntryWindow(Schauspieler actor) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("EditEntryActor.fxml"));
        Parent root = loader.load();
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle("Editing");
        stage.setScene(new Scene(root, Main.width - 200, Main.height - 200));
        stage.showAndWait();
        updateTable();
    }

    public void updateTable(){
        ObservableList<Schauspieler> temp = doaControl.readAll();
        tableViewActor.setItems(temp);
    }

    public void deleteEntry(){
        Schauspieler genre = tableViewActor.getSelectionModel().getSelectedItem();
        doaControl.deleteEntry(genre);
        updateTable();
    }

    public void loadMenu(ActionEvent event){
        Parent root2;
        try {
            root2 = FXMLLoader.load(getClass().getResource("Menu.fxml"));
            Scene scene2 = new Scene(root2, Main.width, Main.height);
            Stage windows = (Stage) ((Node) event.getSource()).getScene().getWindow();
            windows.setScene(scene2);
            windows.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    public void clickItem(MouseEvent event) throws IOException {
        if (event.getClickCount() == 2)
        {
            System.out.println(tableViewActor.getSelectionModel().getSelectedItem());
            openEditEntryWindow(tableViewActor.getSelectionModel().getSelectedItem());
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ObservableList<Schauspieler> result = doaControl.readAll();
        tableColID.setCellValueFactory(new PropertyValueFactory<>("id"));
        tableColName.setCellValueFactory(new PropertyValueFactory<>("name"));
        tableViewActor.setItems(result);
    }
}

EditEntryActor.java:

public class EditEntryActor {
    @FXML
    Button buttonCancel, buttonSubmit;
    @FXML
    TextField textFieldName, textFieldID;
    Schauspieler actor;
    SchauspielerDAO actorDAO;

    public EditEntryActor(Schauspieler actor){
        this.actor = actor;
        this.actorDAO = new SchauspielerDAO();
    }

    public void closeWindow(){
        buttonCancel.getScene().getWindow().hide();
    }

    public void submitEntry(){
        actor.setName(textFieldName.getText());
        actorDAO.change(this.actor);
        closeWindow();
    }
}

0 个答案:

没有答案