1

I've tried to make a little math program with JavaFX. I have a Button action in Controller 1:

@FXML
public void showCalc(ActionEvent event2) {
    layout.parabel_nullstelle_showCalc.setVariables(a, b, c, x1, x2, ze1, ze2, ze3, ze4, ze5, ze6, ze7);
    Parent root3 = main.Main.getParent();
    Scene showCalc = new Scene(root3, 500, 1000);
    Stage paranullCalc = new Stage();
    paranullCalc.setTitle("Rechung");
    paranullCalc.setScene(showCalc);
    paranullCalc.show();
}

It opens a new Stage with Scene which contains a calculation. In the Controller for the showCalc I have the set variables method.

public static void setVariables(double a1, double b1, double c1,double x11, double x22, double ze11, double ze22, double ze33, double ze44, double ze55, double ze66, double ze77){
    a = (float) a1;
    b = (float) b1;
    c = (float) c1;
    x1 = (float) x11;
    x2 = (float) x22;
    ze1 = (float) ze11;
    ze2 = (float) ze22;
    ze3 = (float) ze33;
    ze4 = (float) ze44;
    ze5 = (float) ze55;
    ze6 = (float) ze66;
    ze7 = (float) ze77;
}

I needed to make it static because I can't do an object of a controller and with import I get the static/non static error. But now I want to change the text of a TextArea in the same Scene as the setVariables, so I can show the calculation. I can't make the TextArea static, because then it crashes. I also can't access it without static and creating an object of itself also isn't a solution. So how do I solve this?

Abra
  • 19,142
  • 7
  • 29
  • 41
dav20011
  • 71
  • 3
  • 14
  • Can you post the code for `main.Main.getParent();`? or the declaration for `main.Main`. – ItachiUchiha Jun 14 '15 at 09:59
  • It's not necessary, it just gets the Parent out of the Main Class because you can only import fxml in the start method, nothing interesting (it just returns the Parent) – dav20011 Jun 14 '15 at 11:56
  • What do you mean by "I can't do an object of a controller" and "you can only import FXML in the start method"? It is (almost?) always a mistake to make fields and methods in a controller static. – James_D Jun 14 '15 at 12:13
  • I can only do example = FXMLLoader.load(getClass().getResource("example")); in the start method of the Application, not in a Controller. And if I dont set setVariables to static, I get an error in showCalc, it says "Non static method setVariables(*) cannot be refernced from a static context", and creating an object of the controller also isn't working. – dav20011 Jun 14 '15 at 12:35
  • Again, I don't understand what you mean by that comment. See if [this](http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310#14190310) or [this](http://stackoverflow.com/questions/23105433/javafx-8-compatibility-issues-fxml-static-fields/23109125#23109125) help at all. – James_D Jun 14 '15 at 12:38
  • Sry, pressed Enter and it sent, i dont often write anything here – dav20011 Jun 14 '15 at 12:39
  • Why can you not load the second FXML from the controller? – James_D Jun 14 '15 at 12:45
  • I think i got the loading to work, but this wasn't the Problem, the posts in the two links are a bit complicatet (English is my second language), but is there a simple way to set the content of the TextArea from another class? – dav20011 Jun 14 '15 at 12:55

1 Answers1

2

Don't make variables or methods in your controller static solely for the purpose of being able to access them from elsewhere. It rarely, if ever, makes sense to have static members in a controller.

To access a method in a controller, just retrieve the controller instance from the FXMLLoader. You haven't really posted enough code to provide a complete answer, but you need to do something like:

FXMLLoader loader = new FXMLLoader(getClass().getResource("calc.fxml"));
Parent calcRoot = loader.load();
CalcController controller = loader.getController();
controller.setVariables(...);
Scene showCalc = new Scene(calcRoot, 500, 1000);
// ...

and remove static from the setVariables method declaration in the controller class.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • This looks like it could work, but when I click in the Programm on the Button which opens it, I get an Error: http://pastebin.com/M1fabch6 – dav20011 Jun 14 '15 at 13:12
  • You cannot get that error from the code I posted. It says you are trying to assign a grid pane to an `FXMLLoader`. What is on line 77 of parabel_nullstelle_layout.java? – James_D Jun 14 '15 at 13:15
  • Its the First line of the method (http://pastebin.com/Sr93buMR), so the line with "throws Expection", IntelIJ said I should use IOExpection which also didnt work, so I tried this. (I'm a bit new to Java, so I don't know what this is) – dav20011 Jun 14 '15 at 13:19
  • Why are you doing `FXMLLoader loader = FXMLLoader.load(...)`? Why not use the code I showed above? – James_D Jun 14 '15 at 13:20
  • My bad, already wrote the line a few minutes before and didn't change it, now it works, thanks! – dav20011 Jun 14 '15 at 13:29
  • You dont need to load the Parent to access the function of the controller. – MNCODE Jul 20 '18 at 08:22