如何从screen1填充screen2中的TableView中的数据

时间:2014-03-05 13:24:52

标签: java controller javafx tableview

public class Screen1DocumentController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @FXML public void handleLogin() throws IOException{
        Parent root = FXMLLoader.load(getClass().getResource("Screen2Document.fxml"));
        Scene scene = new Scene(root);

        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();
    }

    @FXML Button btnLogin;


}

Screen2DocumentController.java

public class Screen2DocumentController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        ReportDetails r;
        r = new ReportDetails("pavan");
        rptDtls.add(r);
        rptDtls.add(new ReportDetails("Viajy"));
        rptDtls.add(new ReportDetails("sharat"));
        rptDtls.add(new ReportDetails("sandeep"));
        rptDtls.add(new ReportDetails("bhaskar"));
        rptDtls.add(new ReportDetails("nokal"));

    }    

    private ObservableList<ReportDetails> rptDtls =  FXCollections.observableArrayList();
    @FXML TableView tblData;
    @FXML TableColumn<ReportDetails,String> tblCData;
}

当我点击登录按钮时,我收到运行时错误。我做错了什么?

2 个答案:

答案 0 :(得分:0)

首先将您的FXMLLoader调用放入try / catch以获取详细信息 关于错误。我猜你的Screen2Document.fxml路径不正确或者ReportDetails的import语句丢失了吗?

答案 1 :(得分:0)

您可以通过

将controller1的引用传递给controller2
    Screen2DocumentController controller2 = (Screen2DocumentController)fxmlloader.getController();

将变量添加到Screen2DocumentController

private Screen1DocumentController controller1;

向Screen2DocumentController添加方法

public void setScreen1DocumentController(Screen1DocumentController controller){this.contoller1 = controller; }

现在您可以在handlelogin方法中调用方法

((Screen2DocumentController)fxmlloader.getController()).setScreen1DocumentController(this);

现在你在controller2中有了controller1的引用。

如果您现在向Screen1DocumentController添加一个方法sayHello

public void sayHello(String name){ System.out.println("Hello "+name);}

您可以使用

从Screen2DocumentController调用它
controller1.sayHello("pavankumar");

希望有所帮助