1

I am new to JavaFX and I cannot set the eventhandler for the buttons in the class Controleur that has to manipulate with the Contructor of Controleur of vue and modele.

The question how can i create the object in class GUI to match with the Constructor of Controleur in class Controleur?

Any help will appreciate.

Here is my class GUI:

public class GUI extends Application implements IView{

     TextField textfield1, textfield2, textfield3, textfield4, textfield5;

     Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;

     Button button_add,button_sub,button_div,button_mult,button_virgule,button_neg,button_push,button_clear,button_backspace;



        // TODO Auto-generated method stub
        @Override
        public void start(Stage primaryStage) throws Exception {
            // TODO Auto-generated method stub

            AnchorPane anchorPane= new AnchorPane();
            //error place as i have no idea to create object controleur with parameter vue and modele
            Controleur controleur=new Controleur();
            Scene sc = new Scene(anchorPane, 400.0, 375.0);
            primaryStage.setTitle("Ma calculette");
            primaryStage.setScene(sc);
            primaryStage.show(); 


            textfield1= new TextField();
            textfield1.setPrefHeight(27.0);
            textfield1.setPrefWidth(400.0);
            textfield1.setLayoutY(109.0);
            textfield1.setEditable(false);
            textfield1.setAlignment(Pos.CENTER_RIGHT);
            textfield1.setId("textfield1");
            anchorPane.getChildren().add(textfield1);

            textfield2= new TextField();
            textfield2.setPrefHeight(27.0);
            textfield2.setPrefWidth(400.0);
            textfield2.setLayoutY(82.0);
            textfield2.setEditable(false);
            textfield2.setAlignment(Pos.CENTER_RIGHT);
            textfield2.setId("textfield2");
            anchorPane.getChildren().add(textfield2);

            button7 = new Button("7");
            button7.setPrefHeight(34.0);
            button7.setPrefWidth(82.0);
            button7.setLayoutY(207.0);
            button7.setLayoutX(21.0);
            button7.setMnemonicParsing(false);
            button7.setId("seven");
            //button7.setOnAction(handle);
            anchorPane.getChildren().add(button7);

            button4 = new Button("4");
            button4.setPrefHeight(34.0);
            button4.setPrefWidth(82.0);
            button4.setLayoutY(241.0);
            button4.setLayoutX(21.0);
            button4.setMnemonicParsing(false);
            //button4.setOnAction(handle);
            button4.setId("four");
}

and Here is my class Controleur:

public class Controleur implements ActionListener{

  GUI vue;
  Accumulateur modele;

  public Controleur(GUI vue, Accumulateur modele) {
      this.vue=vue;
      this.modele=modele;

  }


    @Override
    public void actionPerformed(ActionEvent event) {
        // TODO Auto-generated method stub
        if(vue.textfield1.getText().isEmpty() || modele.pile.empty()) {
            if (event.getSource() == vue.button1) {
                vue.textfield1.setText(vue.textfield1.getText()+ "1");
           }else if (event.getSource() == vue.button2) {
                vue.textfield1.setText(vue.textfield1.getText() + "2");
           }else if (event.getSource() == vue.button3) {
                vue.textfield1.setText(vue.textfield1.getText() + "3");
           }else if (event.getSource() == vue.button4) {
                vue.textfield1.setText(vue.textfield1.getText() + "4");
           }else if (event.getSource() == vue.button5) {
                vue.textfield1.setText(vue.textfield1.getText() + "5");
           }else if (event.getSource() == vue.button6) {
                vue.textfield1.setText(vue.textfield1.getText() + "6");
           }else if (event.getSource() == vue.button7) {
                vue.textfield1.setText(vue.textfield1.getText() + "7");
           }else if (event.getSource() == vue.button8) {
                vue.textfield1.setText(vue.textfield1.getText() + "8");
           }else if (event.getSource() == vue.button9) {
                vue.textfield1.setText(vue.textfield1.getText() + "9");
           }else if (event.getSource() == vue.button0) {
                vue.textfield1.setText(vue.textfield1.getText() + "0");   
           }else if (event.getSource() == vue.button_virgule) {
               if(!vue.textfield1.getText().contains(".")) {
                  vue.textfield1.setText(vue.textfield1.getText() + ".");   
              }else {
                   }
           }else if (event.getSource() == vue.button_push) {
               modele.pile.push(Double.parseDouble(vue.textfield1.getText()));
           }else if (event.getSource() == vue.button_backspace) {
               if(!vue.textfield1.getText().isEmpty()) {
                  vue.textfield1.setText(""+vue.textfield1.getText().substring(0, vue.textfield1.getText().length() - 1));
               }else {                     
               }
           }
    }
Schrodinger
  • 39
  • 1
  • 9
  • Any reason to use GUI class specifically for creating UI? It can be done easily with FXML. After creating FXML just add those objects into your controller and you are done. – Sushant Oct 27 '19 at 13:00
  • yes i know the way to create the fxml but since my professor required to do it purely in java not include the fxml file – Schrodinger Oct 27 '19 at 13:05
  • Can you give me some ideas of how to create the object of class Controleur to match with the constructor of Controleur class? – Schrodinger Oct 27 '19 at 13:07
  • I guess this link might help learn MVC without FXML. [https://stackoverflow.com/questions/36868391/using-javafx-controller-without-fxml] – Sushant Oct 27 '19 at 13:13
  • i have already seen this but it seems not what match my code – Schrodinger Oct 27 '19 at 13:18

1 Answers1

0

JavaFX includes EventHandler , so you don’t need to use awt libraries. Also, you declared instance of Controleur but didn’t mentioned anything as argument, so there will be compiling error. Do next In Gui class :

Controleur controleur = new Controleur(this);
button.setOnAction(controleur);

In Controleur class :

Gui parent;
Accumulator accumulator;
public Controleur(Gui parent){
 this.parent = parent;
 accumulator = new Accumulator ();
}
public void handle(Event event){
 Object ob = event.getSource();
  //here we communicate through reference parent
  if(ob == parent.button){
   String data = parent.textField.getText();
  // here we add value to list in Accumulator class through accumulator reference 
   accumulator.list.add(data);
 }
}

In Accumulator class (which is model) :

List list;
Accumulator (){
 list = new ArrayList();
}

When you press button ,through the reference of Controleur class parent it asks Controller what button has been pressed? Then , through the reference accumulator , depends on what button has been pressed, Controler pass value through accumulator to List in Accimulator class(which is model ) to save data in list. That’s how you can pass value from GUI to Controller to Model.

Gipsy King
  • 186
  • 2
  • 2
  • 14