1

I am new to JavaFX and I tried to import the textfields and buttons from class GUI into class Controleur but I cannot do it. How to do it? Any answer will appreciates.

I have class GUI:

public class GUI implements IView{

     TextField textfield1;
     TextField textfield2;
     TextField textfield3;
     TextField textfield4;
     TextField textfield5;


    public void View() throws Exception {
        // TODO Auto-generated method stub


            AnchorPane anchorPane= new AnchorPane();

            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);

            //TextField 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);

            textfield3.setPrefHeight(27.0);
            textfield3.setPrefWidth(400.0);
            textfield3.setLayoutY(55.0);
            textfield3.setEditable(false);
            textfield3.setAlignment(Pos.CENTER_RIGHT);
            textfield3.setId("textfield3");
            anchorPane.getChildren().add(textfield3);

            textfield4.setPrefHeight(27.0);
            textfield4.setPrefWidth(400.0);
            textfield4.setLayoutY(28.0);
            textfield4.setEditable(false);
            textfield4.setAlignment(Pos.CENTER_RIGHT);
            textfield4.setId("textfield4");
            anchorPane.getChildren().add(textfield4);

            //TextField textfield5= new TextField();
            textfield5.setPrefHeight(20.0);
            textfield5.setPrefWidth(400.0);
            textfield5.setLayoutY(1.0);
            textfield5.setEditable(false);
            textfield5.setAlignment(Pos.CENTER_RIGHT);
            textfield5.setId("textfield5");
            anchorPane.getChildren().add(textfield5);

            Button 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");
            anchorPane.getChildren().add(button7);

            Button button4 = new Button("4");
            button4.setPrefHeight(34.0);
            button4.setPrefWidth(82.0);
            button4.setLayoutY(241.0);
            button4.setLayoutX(21.0);
            button4.setMnemonicParsing(false);
            button4.setId("four");
            anchorPane.getChildren().add(button4);

            Button button1 = new Button("1");
            button1.setPrefHeight(34.0);
            button1.setPrefWidth(82.0);
            button1.setLayoutY(275.0);
            button1.setLayoutX(21.0);
            button1.setMnemonicParsing(false);
            button1.setId("one");
            anchorPane.getChildren().add(button1);


            Scene sc = new Scene(anchorPane, 400.0, 375.0);
}

and class Controleur:

public class Controleur {

//code to connect with those textfields and buttons from class GUI
}
fabian
  • 80,457
  • 12
  • 86
  • 114
Schrodinger
  • 39
  • 1
  • 9

1 Answers1

0

Create instance of Controller in your GUI constructor

public class View extends Application {
 public View() {
 Controller controller = new Controller(this);
  //here all declaration and stuff...
  Button button = new Button();
  button.setOnAction(controller);
 }
}

And then in Controller instance of your GUI class:

public class Controller implements EventListener {
 View parent;
 public Controller(View parent) {
  this.parent = parent;
 }
 public void handle(Event event) {
   Object ob = event.getSource();
   if( ob == parent.button ) {
    String data = parent.textField.getText();
  }
 }
} 
Gipsy King
  • 186
  • 2
  • 2
  • 14
  • if my controller has to manipulate with view and model how can i write it? – Schrodinger Oct 26 '19 at 20:50
  • @Schrodinger pls show what you have done so far – Gipsy King Oct 27 '19 at 10:28
  • I want to add View and Model for the public controller(View Parent, Accumulateur ac){} so what should i change with the Controller controleur=new Controller(this)? – Schrodinger Oct 27 '19 at 10:35
  • As I said , you should pass reference of your View class where you declared all your objects to another classes (controller and model) to communicate between these classes through this reference and pass values to any objects, see example above. – Gipsy King Oct 27 '19 at 11:47
  • here I have done so far. https://stackoverflow.com/questions/58578482/how-to-define-an-object-to-match-with-the-class-controleur-from-class-gui – Schrodinger Oct 27 '19 at 11:58