1

I have an app with 3 fxml files. The 1st is main window of app and most of work im doing here, but the 2 others fxml files allows me to change some settings and add more records to app. I made it that 2 small fxml files extends the main file. Basically:

public class FXMLDocumentController implements Initializable {
@FXML
private TextField txt;
@FXML
private Stage stage;
Symulacja symulacja = new Symulacja();
Promocja promocja = new Promocja();
@Override
public void initialize(URL url, ResourceBundle rb) {
    txt.textProperty().bindBidirectional(promocja.getProgKwotyZetonow(), new NumberStringConverter());
    labelProgKwotyZetonow.textProperty().bind(promocja.getProgKwotyZetonow().asString());
}
@FXML
public void zmienUstawienia() {
    symulacja.zmienOkno("FXMLUstawienia.fxml", "Ustawienia");
}
@FXML
public void dodajKlientow() {
    symulacja.zmienOkno("FXMLDodajKlienta.fxml", "Dodawanie");
}
}

It's main fxml Controller

public class FXMLUstawieniaController extends FXMLDocumentController{

@FXML
private TextField textProgKwota;

@FXML
private Button btnZatwierdzUstawienia;

@FXML
private TextField textProgIlosc;

@FXML
public void zatwierdzUstawienia() {
    promocja.setProgIlosciZetonow(Integer.parseInt(textProgIlosc.getText()));
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    textProgKwota.textProperty().bindBidirectional(promocja.getProgKwotyZetonow(), new NumberStringConverter());
}
}

As you see I'm using properties to dynamically change what is showing in labelProgKwotyZetonow and textProgKwota.

When im not using the 2nd window txt and labelProgKwotyZetonow works fine, but when I'm initializing 2nd window I always get 0 in TextField and even if I change it this is not changing labelProgKwotyZetonow.

It's strange to me because when I'm souting (System.out.println) the value of promocja.getProgKwotyZetonow() in some places I get:

In function initialize in the 2nd controller - 0.

In function zmienUstawienia in 1st controller - the value from txt/labelProgKwotyZetonow.

In function zatwierdzUstawienia in 2nd controller - the value from textProgKwota.

Piotter
  • 71
  • 1
  • 1
  • 4
  • Looks like you expect the `Promocja` of the controller instance of a new controller instance contains the same data as the `Promocja` of a different controller which is probably not the case... – fabian Jun 19 '17 at 16:33
  • But doesn't extends provide me with the same instance? I don't declare another `Promocja` in the 2nd controller. – Piotter Jun 19 '17 at 16:37
  • `String extends Object`, but does that mean the following 2 variables use point to the same instance? `Object o = new Object(); String s = "abcde";` Even using the same type does not make sure the instances are the same, e.g. here it's different `Object o1 = new Object(); Object o2 = new Object();`. If you're using the `fx:controller` attribute to specify the controllers, `FXMLLoader` will always create a new instance of the controller when loading the fxml. You should probably take a look at this question: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian Jun 19 '17 at 16:46
  • thx I will try it – Piotter Jun 19 '17 at 17:34

0 Answers0