JAVA FX - 如何将表单数据从一个窗口传递到另一个窗口?

时间:2016-06-13 16:27:18

标签: java javafx controller dialog tableview

我是JavaFx的新手,在我看来这是一个常见的问题,但根据每个人开始编码答案的方式不同,这就是为什么我在这里找不到我的所以我只会问。 / p>

计划:管理诊所的软件
说明
1 - 应用程序打开,显示菜单按钮(医生,患者等)
2 - (假设)点击医生,显示一个带表格的经理窗口,查看所有医生注册到系统
3 - 在这里,有一个添加新的按钮
4 - (假设)单击addNewDoctor按钮并填写表格
5 - (这是我被困的地方)如何将这些数据发送到表格

这就是我的尝试:

这是怎么回事:
  我想弄清楚如何在javaFx中的另一个控制器的一个控制器中使用一个方法,我知道如何将值添加到表视图中,但这是发生的事情:

从addNewDoctor类(其中是带有表单的dialogBox)我从Doctor类(我在哪里创建表)中调用一个方法,在这个方法中我向表添加值,我还使用sysout在控制台上打印值(只是为了查看值是否已通过),它们是因为它们已被打印,但它没有将它们添加到表中。

但是如果我在Doctor类(表所在的位置)创建一个方法来添加一个新医生并将其链接到一个按钮,在这个窗口中它会添加一个新的医生(但我想添加来自一个新的dialogBox,控制器是addNewDoctor类)

这是我的代码:
1 - 主要班级
在这里,我创建所有窗口,而不仅仅是主窗口。 包临床;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class Main extends Application {

    private static Stage addDialogStageM;
    private static Stage primaryStage;
    private static BorderPane mainLayout;

    @Override
    public void start(Stage primaryStage) throws IOException {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Aplicacao de Gestao de Clinica");
        showMainView();
        showMainItems();
    }

    public static void showMainView() throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/MainView.fxml"));
        mainLayout = loader.load();
        Scene scene = new Scene(mainLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void showMainItems()throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/MainItems.fxml"));
        BorderPane mainItems = loader.load();
        mainLayout.setLeft(mainItems);
    }

    public static void showMedicoScene()throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("medico/MedicoGUI.fxml"));
        BorderPane medicoGUI = loader.load();
        mainLayout.setCenter(medicoGUI);
    }

    public static void showPacienteScene()throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("paciente/PacienteGui.fxml"));
        BorderPane pacienteGUI = loader.load();
        mainLayout.setCenter(pacienteGUI);
    }

    public static void showConsultasScene()throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("consulta/ConsultaGUI.fxml"));
        BorderPane consultaGUI = loader.load();
        mainLayout.setCenter(consultaGUI);
    }

    public static void showHospedagemScene()throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("hospedagem/HospedagemGUI.fxml"));
        BorderPane hospedagemGUI = loader.load();
        mainLayout.setCenter(hospedagemGUI);;
    }

    public static void showQuartoScene()throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("quarto/QuartoGUI.fxml"));
        BorderPane quartoGUI = loader.load();
        mainLayout.setCenter(quartoGUI);
    }

    public static void showAddMedicoStage()throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("medico/AddMedicoGUI.fxml"));
        BorderPane addMedico = loader.load();

        addDialogStageM = new Stage();
        addDialogStageM.setTitle("Adicionar Novo Medico");
        addDialogStageM.initModality(Modality.WINDOW_MODAL);
        addDialogStageM.initOwner(primaryStage);

        Scene scene = new Scene(addMedico);
        addDialogStageM.setScene(scene);
        addDialogStageM.showAndWait();
    }

    public static void closeAddMedicoStage(){
        addDialogStageM.close();
    }

    public static void showAddPacienteStage()throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("paciente/AddPacienteGUI.fxml"));
        BorderPane addPaciente = loader.load();

        Stage addDialogStage = new Stage();
        addDialogStage.setTitle("Adicionar Novo Paciente");
        addDialogStage.initModality(Modality.WINDOW_MODAL);
        addDialogStage.initOwner(primaryStage);

        Scene scene = new Scene(addPaciente);
        addDialogStage.setScene(scene);
        addDialogStage.showAndWait();
    }

    public static void showAddQuartoStage()throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("quarto/AddQuartoGUI.fxml"));
        BorderPane addQuarto = loader.load();

        Stage addDialogStage = new Stage();
        addDialogStage.setTitle("Criar novo Quarto");
        addDialogStage.initModality(Modality.WINDOW_MODAL);
        addDialogStage.initOwner(primaryStage);

        Scene scene = new Scene(addQuarto);
        addDialogStage.setScene(scene);
        addDialogStage.showAndWait();
    }

    public static void showAddHospedagem()throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("hospedagem/AddHospedagemGUI.fxml"));
        BorderPane addHospedagemGUI = loader.load();

        Stage addDialogStage = new Stage();
        addDialogStage.setTitle("Fazer Hospedagem");
        addDialogStage.initModality(Modality.WINDOW_MODAL);
        addDialogStage.initOwner(primaryStage);

        Scene scene = new Scene(addHospedagemGUI);
        addDialogStage.setScene(scene);
        addDialogStage.showAndWait();
    }

    public static void showAddConsultaStage()throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("consulta/AddConsultaGUI.fxml"));
        BorderPane addConsulta = loader.load();

        Stage addDialogStage = new Stage();
        addDialogStage.setTitle("Marcar Consulta");
        addDialogStage.initModality(Modality.WINDOW_MODAL);
        addDialogStage.initOwner(primaryStage);

        Scene scene = new Scene(addConsulta);
        addDialogStage.setScene(scene);
        addDialogStage.showAndWait();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

2 - 医生(Medico)课程

package clinica.medico;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import clinica.Main;


public class MedicoGUIController implements Initializable{

    private Main main;
    private Medico medico;
    @FXML public TableView<Medico> tbMedico;
    @FXML public TableColumn<Medico,Integer> crm;
    @FXML public TableColumn<Medico,String> nome;
    @FXML public TableColumn<Medico,String> dtAdm;
    @FXML public TableColumn<Medico,String> especialidade;
    @FXML public TableColumn<Medico,Double> salario;
    @FXML public TextField txtFiltrarCRM;


    public ObservableList<Medico> list = FXCollections.observableArrayList(
            new Medico(1,"Carlos ","1994-01-12","Cirugiao",100000),
            new Medico(2,"Some Name","1992-02-15","Genecologista",100000)
    );


    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        crm.setCellValueFactory(new PropertyValueFactory<Medico,Integer>("crm"));
        nome.setCellValueFactory(new PropertyValueFactory<Medico,String>("nome"));
        dtAdm.setCellValueFactory(new PropertyValueFactory<Medico,String>("dtAdm"));
        especialidade.setCellValueFactory(new PropertyValueFactory<Medico,String>("especialidade"));
        salario.setCellValueFactory(new PropertyValueFactory<Medico,Double>("salario"));
        tbMedico.setItems(list);
    }

    @FXML
    private void goAddMedico()throws IOException{
        main.showAddMedicoStage();
    }

    public void setMedicoInfo(Medico medico) {
        this.medico = medico;
        tbMedico.getItems().addAll(new Medico(medico.getCrm(),medico.getNome(),medico.getDtAdm(),medico.getEspecialidade(),medico.getSalario()));
        System.out.println(medico.toString());
    }
}

3- AddNewDoctor类

package clinica.medico;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import clinica.Main;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class AddMedicoGUIController implements Initializable{


    @FXML private TextField txtCRM;
    @FXML private TextField txtNome;
    @FXML private DatePicker dpDtAdm;
    @FXML private TextField txtEspecialidade;
    @FXML private TextField txtSalario;

    public int getCRM(){
        return Integer.parseInt(txtCRM.getText()); 
    }

    public String getNome(){
        return txtNome.getText();
    }

    public String getDtAdm(){
        return ""+dpDtAdm.getValue();
    }

    public String getEspec(){
        return txtEspecialidade.getText();
    }

    public double getSalario(){
        return Double.parseDouble(txtSalario.getText());
    }

    @FXML
    public void addMedico(ActionEvent event) throws IOException{
        Medico medico = new Medico(getCRM(),txtNome.getText(),getDtAdm(),getEspec(),getSalario());
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("medico/MedicoGUI.fxml"));
        loader.load();
        MedicoGUIController medicoGUI = loader.getController();
        medicoGUI.setMedicoInfo(medico);
    }

    public String toString(){
        return "CRM :"+getCRM();
    }

    @FXML
    public void closeAddDialogMedico(){
        Main.closeAddMedicoStage();
    }

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) { 
    }
}  

4-医生(Medico)模特课

package clinica.medico;

public class Medico {

    private int crm;
    private String nome;
    private String dtAdm;
    private String especialidade;
    private double salario;

    public Medico(){

    }

    public Medico(int crm, String nome, String dtAdm,String especialidade,double salario){
        super();
        this.crm = crm;
        this.nome = nome;
        this.dtAdm = dtAdm;
        this.especialidade = especialidade;
        this.salario = salario;
    }

    public int getCrm(){
        return crm;
    }

    public void setCrm(int crm){
        this.crm = crm;
    }

    public String getNome(){
        return nome;
    }

    public String getDtAdm(){
        return dtAdm;
    }

    public String getEspecialidade(){
        return especialidade;
    }

    public double getSalario(){
        return salario;
    }

    public void setCRM(int crm){}

    public String toString(){
        return "CRM           : "+crm+"\n"+
               "NOME          : "+nome+"\n"+
               "DATA ADIM     : "+dtAdm+"\n"+
               "ESPECIALIDADE : "+especialidade+"\n"+
               "SALARIO       : "+salario+"\n";

} }

0 个答案:

没有答案