0

i made a mainEntredController which contains a TabPane with two tabs (HomeController) and (PatientController), The MainEntred Controle the Parameter Passage Between this two tabs but i have liitls issu that the tabs doesnet reconize the Parent (MainEntredController) or each other. it retuns an error inn this line *** labelpatient.setText(mainec.LoadtxtFromlabelhometabhome()); ****** the idea is to get a text from the first tab to show it in a label in the second tab

hear's my code the first is the MainEntred Class the Container

public class MainEntredController  {
@FXML MenuItem closemi;
@FXML  HomeController homecontroller;
@FXML  PatientController patientcontroller;


 public void initialize(URL location, ResourceBundle resources) {
    System.out.println("Application Started");
homecontroller.intit(this);
patientcontroller.init(this);

}



public void ClickMenuItemClose(){
    System.exit(0);
}


public String LoadtxtFromlabelhometabhome() {
//  System.out.println(homecontroller.labelhome.getText());
    return  homecontroller.labelhome.getText();
}

public void setTabpatientlabel(String text) {
    //patientcontroller.labelpatient.setText(text);

}

}

the Second is the First Tab HomeController

public class HomeController {

public MainEntredController mainec;

@FXML public Label labelhome;
@FXML private TextField tfhome;
@FXML private Button savehome;
@FXML private Button sendhome;
//public HomeController(){}

 public void btnSaveHomeClicked(ActionEvent event){
System.out.println("le boutton save home est cliqué");
labelhome.setText(tfhome.getText());
 }
public void btnSendHomeClicked(ActionEvent event){
System.out.println("le boutton send home est cliqué");
// send data to tab patient
 mainec.setTabpatientlabel(labelhome.getText());
  }

public void intit(MainEntredController mainEntredController) {
mainec = mainEntredController;
}

}

the Third one is the Second tab PatientController

 public class PatientController {

 public MainEntredController mainec;

@FXML public Label labelpatient;
@FXML private TextField tfpatient;
@FXML private Button savepatient;
@FXML private Button loadpatient;


  public void btnSavePatientClicked(ActionEvent event){
System.out.println("le boutton save Patient est cliqué");
labelpatient.setText(tfpatient.getText());
   }

   public void btnLoadPatientClicked(ActionEvent event){
System.out.println("le boutton Load patient est cliqué");
labelpatient.setText(mainec.LoadtxtFromlabelhometabhome());
   }

 public void init(MainEntredController mainEntredController) {
  mainec = mainEntredController;
  }
 }

And the FXML File of the MainEntred

<fx:include id="homecontroller" source="home.fxml" />
<fx:include id="patientcontroller" source="patient.fxml" />

i guess those two are madding the problem any advise how to solve it Please i dont know why i am having java.lang.NullPointerException at the line declarred erlier he cant get the source (unknown source)

salah tabet
  • 119
  • 1
  • 1
  • 10
  • I'm pretty sure the code would fail before getting to that line, since the controller field names are wrong, see [JavaFx Nested Controllers (FXML )](http://stackoverflow.com/questions/12543487/javafx-nested-controllers-fxml-include) Also the fxml doesn't use the `id` attribute from the `fx` namespace... – fabian Jan 11 '16 at 00:18
  • what i nedd to change ? in that exemple they suggested to make the same source from the two includs tabs, which gives an error, what should i do please – salah tabet Jan 11 '16 at 13:09
  • Nested controllers are used as described here: https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#nested_controllers . What they don't tell you is, how to choose the name of the field where the controller is injected (see answers to the questions I linked in the previous comment). Right now it's impossible to give advice on other parts of your question, since you describe an error, that wouldn't happen, if the code you posted was used (line `homecontroller.intit(this);` in `MainEntredController.initialize` should yield a NPE, not the line mentioned.). – fabian Jan 11 '16 at 13:26
  • Thanks a lot i wil do some research on nested controller to solve it – salah tabet Jan 11 '16 at 22:43

2 Answers2

0

If i understand correctly then you want to communicate between controller. well, this can be done pretty easy.

in you main type something likes this:

FXMLLoader loader = new FXMLLoader(Main.class.getResource("your.fxml"));
Parent node = loader.load(); // this loads view 
YourController instance = loader.getController();
some_class.initialize(instance);
Bobul Mentol
  • 134
  • 1
  • 7
0

Finelly i found what is wrong. the thing is that in the declaration in the MainController of the two Tabs we put the fx:id declared on the fxml file and adding the word "Controller"

like this

    @FXML private HomeController homeController;
    @FXML  private PatientController patientController;

the fx:id="home" the second includes fx:id="patient"

salah tabet
  • 119
  • 1
  • 1
  • 10