0

I am new to JavaFx, it seems to me that this is a common problem but according to how each person started coding the answer is different that is why I can't find mine in here so I'll simply ask.

Program: Software to manage a Clinic
Description:
1 - Application open, display menu buttons (Doctor,Patients,etc)
2 - (Suppose) Click on Doctor, display a manager window with a table to see all doctors register to the system
3 - In here, there a button to add a new one
4 - (Suppose) You click the addNewDoctor button and fill the form
5 - (This is where I am stuck) How to send this data to the table

This is what i have tried:

What happens is this:
I figure it out how to use a method in one controller from another controller in javaFx and i know how to add values into a table view but here is what happens:

From the addNewDoctor class (where is the dialogBox with the form) i call a method from the Doctor class (where i created the table) and in this method i add values to the table, i also use a sysout to print the values on the console (just to see if the values are been passed), and they are because they are been printed, but it does not add them to the table.

But if i create a method in the Doctor class (where the table is located) to add a new doctor and link it to a button that in this window it adds a new doctor (but i want to add from a new dialogBox, which the controller is the addNewDoctor class).

This is my code:
1-The Main class
Here i create all the windows, not just the Main window. package clinica;

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 - The Doctor(Medico) class

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- The AddNewDoctor class

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- The Doctor(Medico) Model class

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";

} }

  • I have seen the others answers but it is not working. Please just let other people see my code. Did you even saw my code or just the title. – Walter Sono Jun 13 '16 at 18:23
  • First: this bears no resemblance to the solutions in the linked question. Why don't you actually try to implement one of those, instead of all these `static` fields and methods? Second: you haven't actually stated the problem. What does "It is not working" mean? What actually happens? What have you tried to do to solve the problem? – James_D Jun 14 '16 at 01:16
  • You are right. Just edited and add what i have tried and what is the problem. Thank you. – Walter Sono Jun 14 '16 at 01:51

0 Answers0