1

How to add a double from one textfield popup to another textfield on the main screen. I have a cinema system where there is a main page which shows the total price and I have a popup for drinks as well. how do I get the value from drinks popup window to add to the textfield in the main window. I have assigned both classes with the variable completetotal which should add together and close the window when I click the button 'finish'. However it doesn't.

Controller for drinkspopup

import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import static sample.FilmsController.completetotal;

public class DrinksController {

    public TextField watertf;
    public TextField coketf;
    public Button totalbut;
    public TextField totaltf;
    public Button finishdrinks2;

    private FilmsController fc = new FilmsController();

    /*changed button to private */

    /*public TextField gettotaltf() {
        return totaltf;
    }

    private FilmsController parent;*/


    private Drinks drink = new Drinks();

   /* public void setParent(FilmsController fc) {
        // Remember the reference to the parent GUI, then we can call its methods
        parent = fc;
    }*/


    /*added action event*/
    public void addrinks(ActionEvent actionEvent) {

        try {

            int water = new Integer(watertf.getText());

            int coke = new Integer(coketf.getText());

            double result = water * (1.75) + coke * (1.25);

            totaltf.setText("" + result);

            completetotal += result;

        } catch (NumberFormatException e) {

            totaltf.setText("Not a Number");


        }
    }
    /*This is supposed to add the drinks then add it to the variable complete total*/
    public void finishdrinksclose2(ActionEvent actionEvent) {
        int water = new Integer(watertf.getText());

        int coke = new Integer(coketf.getText());

        double result = water * (1.75) + coke * (1.25);

        completetotal += result;

        totaltf.setText("" + result);

        Stage stage = (Stage) finishdrinks2.getScene().getWindow();
        stage.close();
        }
    }

Mainscreen Filmscontroller

 public void comptotal(ActionEvent actionEvent) {

    try {
        int adult = new Integer(adulttf.getText());
        int child = new Integer(childtf.getText());
        double filmprice= new Double(filmpricetf.getText());

        double result = adult * (filmprice) + child*(filmprice);

        completetotal+= result;

        totalpricetf.setText("" + result);

    }
    catch (NumberFormatException e) {

        totalpricetf.setText("Not a Number");

    }}
}

I have only added part of the code which does the procedure on filmscontroller.

  • 1
    It should modify the field, but modifying a field does trigger an update of any part of the GUI by itself. I do not recommend using `static` to communicate things. Design a proper model that you can add observers to and then simply modify the model. The model can be passed around as described here: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml You could also use `showAndWait` and retrieve the results of the popup after the popup window is closed. – fabian Mar 19 '19 at 12:32

1 Answers1

0

Lets imagine this your MainScreen when you try to call popUp window

click.setOnAction(event -> {
    FXMLLoader loader = new FXMLLoader();
    try {
        Parent parent =  loader.load(getClass().getClassLoader().getResource("second.fxml").openStream());
        Stage stage = new Stage();
        stage.setScene(new Scene(parent));
        stage.initModality(Modality.APPLICATION_MODAL);
        Children children = loader.getController();
        children.setController(this);
        stage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
});

Let imagin this your popUp window

@FXML
private JFXTextField field;
private Controller controller;
@FXML private Button cls;
@Override
public void initialize(URL location, ResourceBundle resources)
{
    cls.setOnAction(event -> {
            this.controller.setTextIntoTextField(field.getText());
        Stage stage = (Stage)((Button)(event).getSource()).getScene().getWindow();
        stage.close();
    });

}

public void setController(Controller controller) {
    this.controller = controller;
}

this is a type of callback method can help you to load all the data that should appear in mainscreen, more precisely from children to parent controller class

Jahongir Sabirov
  • 460
  • 1
  • 8
  • 24