JavaFX - 在另一个Controller中编辑后更新TableView内容

时间:2015-09-22 11:30:14

标签: java javafx tableview javafx-8

我有两个控制器用于两个不同的FXML文件,AutoControllerInsertAutoController。 在AutoController中,我使用了一个显示从数据库中获取的值的tableview,在InsertAutoController中,我有插入或编辑数据的函数,因此更改了数据库内容。 AutoController中的表格视图:

@FXML
private TableView<ModelAuto> autotable;
@FXML 
private TableColumn chilometraggio; 
@FXML 
private TableColumn modello; 
@FXML 
private TableColumn requisiti;
@FXML 
private TableColumn stato; 
@FXML 
private TableColumn targa; 
@FXML 
private TableColumn costokm; 
@FXML 
private TableColumn fascia; 

我用来设置autotable中数据库取值的函数是loadTableAuto

    @FXML
void loadTableAuto()
{
    //Autovettura
    try {
        ArrayList<ArrayList<String>>autof = (ArrayList<ArrayList<String>>) fc.processRequest("ReadModelAuto");
        ArrayList<ModelAuto> C = new ArrayList<ModelAuto>();
        for(int i=0; i<autof.size(); i++)
        {
            ModelAuto c1 = new ModelAuto(autof.get(i).get(0), autof.get(i).get(1), autof.get(i).get(2), autof.get(i).get(3), autof.get(i).get(4), autof.get(i).get(5), Double.parseDouble(autof.get(i).get(6))); 

            C.add(c1);
        }

        ObservableList<ModelAuto> clienteData = FXCollections.observableList(C);

        targa.setCellValueFactory(new PropertyValueFactory<ModelAuto, String>("targa"));
        fascia.setCellValueFactory(new PropertyValueFactory<ModelAuto, String>("fascia"));
        modello.setCellValueFactory(new PropertyValueFactory<ModelAuto, String>("modello"));
        chilometraggio.setCellValueFactory(new PropertyValueFactory<ModelAuto, String>("ultimokm"));
        requisiti.setCellValueFactory(new PropertyValueFactory<ModelAuto, String>("requisiti"));
        costokm.setCellValueFactory(new PropertyValueFactory<ModelAuto, Double>("costokm"));
        stato.setCellValueFactory(new PropertyValueFactory<ModelAuto, String>("stato"));


        autotable.setItems(clienteData);

        } catch (SecurityException  | NoSuchMethodException
            | ClassNotFoundException | InstantiationException
            | IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
}

我在initialize()方法中调用此函数。

我打开InsertAutoController的FXML作为AutoController窗口上方的窗口,它始终保持打开状态。当我关闭loadTableAuto相关窗口时,我不知道如何调用函数InsertAutoController,以便表格可以显示更新的值。

我尝试在AutoController中导入InsertAutoController类并在那里使用loadTableAuto()函数,但它给了我一个NullPointerException错误。 在InsertAutoController中插入新值的功能:

    @FXML
private void handleOk(ActionEvent event) {
    if ((fasciacombo.getSelectionModel().getSelectedItem() == null) || (modellocombo.getSelectionModel().getSelectedItem() == null)  || (statocombo.getSelectionModel().getSelectedItem() == null))
    {
        //alert message: incorrect values
    }

    else
    { 
       //values are correct
        try {
        boolean ok = (boolean)fc.processRequest("IAuto", reg); //function that inserts values in the database and returns boolean
        if (ok==true)
        {
            //I wanted to call the loadTableAuto() function in here, after I added data in the database, before the stage is closed
            Stage stage = (Stage) okbtn.getScene().getWindow();
            stage.close();
        }
    } catch (SecurityException | NoSuchMethodException | ClassNotFoundException
            | InstantiationException | IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }  
    }

    }

在编辑数据库并关闭由InsertAutoController控制的窗口后,有没有办法以某种方式调用此方法? 或者在编辑完成后自动更新Tableview的方法?

0 个答案:

没有答案