0

I have a Java FX project and I just got all of the different scenes connected with button actions, but the last thing I have left is not quite working. Once logged into the main scree, I have a homescreen with a table. If you select "Add patient" A new pop up scene opens with a form. I have the button on the form pop up redirecting back to the table homescreen, but the info doesn't save as it's supposed to. Any help is much appreciated. Here's some of the code:

public class homescreenController implements Initializable {

    @FXML
    private TableView <Table> patientTable;
    @FXML
    private TableColumn<Table, Integer> PatientNo;
    @FXML
    private TableColumn<Table, String> LastName;
    @FXML
    private TableColumn<Table, String> FirstName;
    @FXML
    private TableColumn<Table, String> DateOfBirth; 
    @FXML
    private Button openPop;
    @FXML
    private Button Btn2;

    @FXML
    int iNumber = 1001;

    @FXML
    final ObservableList <Table> data = FXCollections.observableArrayList(
           new Table(iNumber++, "Smith", "Patient1", "1/22/1978"),
           new Table(iNumber++, "Roberts", "Patient2", "1/22/1978"),
           new Table(iNumber++, "Doe", "Patient3", "1/22/1978")
    );

    @Override
    public void initialize(URL url, ResourceBundle rb) {

       PatientNo.setCellValueFactory(new PropertyValueFactory<Table, Integer>   ("rPatientNo"));
        LastName.setCellValueFactory(new PropertyValueFactory<Table, String>("rLastName"));
        FirstName.setCellValueFactory(new PropertyValueFactory<Table, String>("rFirstName"));
        DateOfBirth.setCellValueFactory(new PropertyValueFactory<Table, String>("rDateOfBirth"));

        patientTable.setItems(data);


    }




    @FXML
    public void handleButtonAction2(ActionEvent event) throws IOException, SQLException {

       Parent root = FXMLLoader.load(getClass().getResource("popUp.fxml"));
       Scene home_page_scene = new Scene(root);
       Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
       app_stage.setScene(home_page_scene);
       app_stage.show();

    }  
}

//And the controller for the pop up

public class popUpController extends homescreenController implements Initializable {


    @FXML
    TextField LastNameField;
    @FXML
    TextField FirstNameField;
    @FXML
    TextField DateOfBirthField ;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    @FXML
    private void handleButtonAction3 (ActionEvent event) throws IOException, SQLException {


        Table entry = new Table(iNumber, LastNameField.getText(), FirstNameField.getText(), DateOfBirthField.getText());
        iNumber++;

        data.add(entry);

        clearForm();

        Parent home_page_parent = FXMLLoader.load(getClass().getResource("homescreen.fxml"));
        Scene home_page_scene = new Scene(home_page_parent);
        Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        app_stage.setScene(home_page_scene);
        app_stage.show();


    }

    @FXML
    private void clearForm() {

        LastNameField.clear();
        FirstNameField.clear();
        DateOfBirthField.clear();
    }

}
James_D
  • 201,275
  • 16
  • 291
  • 322
Hunter Noman
  • 1
  • 1
  • 7
  • Why is one controller a subclass of the other? – James_D Apr 25 '16 at 00:47
  • I just did that a few moments before I posted as a different option. Originally I had everthing in my homescreenController and my homescreen and pop up were both controlled by it. Everything was working for switching screens but when I added the method to my button for putting the info into the table it quit loading the popup and threw and FXML loader exception error. – Hunter Noman Apr 25 '16 at 00:51
  • I still don't understand why one controller would be a subclass of the other. What are you actually trying to achieve by doing that? Take out the inheritance. Pass the data to the popup controller from the home screen controller when you load and display it, using the techniques in the linked question. – James_D Apr 25 '16 at 00:52
  • Because the homescreen controller seemed to be having an issue with multiple event handlers so I created one specifically for the pop up with that event handler. However, when I needed to add to the table, it didn't recognize the fields. I realize that wasn't really a good idea now, but when I add the event handler back to the other controller I keep getting the error. The methods are the same, I just originally had everything under homescreen controller. – Hunter Noman Apr 25 '16 at 00:57
  • Hmm. Not really sure where you are confused, but you have fundamentally misunderstood something. Each time you load an FXML file, the `FXMLLoader` creates a new instance of the specified controller class (what else could it do?). So each FXML has a different object as its controller. So in your version with one controller class being a subclass of the other, you have two copies of the data (only one is the one being shown in the table). Just try it the way described in my previous comment. (I think you have to pass the data back from the popup controller to the home screen controller too). – James_D Apr 25 '16 at 01:00
  • Ok, I think I understand. I'm going to try now......So keep the pop up controller, just remove inheritance and pass the info between the two? – Hunter Noman Apr 25 '16 at 01:04
  • Yes, something like that. I would probably not try to reload the entire FXML every time (i.e. load the "home screen" fxml once and the "popup" fxml once, and switch between the two, instead of reloading from scratch each time), but that might be quite a big refactor at this point. – James_D Apr 25 '16 at 01:06

0 Answers0