samedi 16 avril 2016

JavaFX separating event handling from controller class

In my opinion handling events inside controller class in javafx comme difficult, specialy when the number of events to handle grows.So,I'm trying to separate event handling from Controller class.For example in complex applications how events are managed ?

So, let's make an example:

the view.fxml file:

    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://ift.tt/19NWjX5" xmlns:fx="http://ift.tt/1cgvoBb">
   <children>
      <Button layoutX="233.0" layoutY="175.0" mnemonicParsing="false" onAction="#button1Clicked" text="Button1" />
      <Button layoutX="233.0" layoutY="225.0" mnemonicParsing="false" onAction="#button2Clicked" text="Button2" />
      <Button layoutX="233.0" layoutY="279.0" mnemonicParsing="false" onAction="#button3Clicked" text="Button3" />
   </children>
</AnchorPane>

And the controller class:

public class AccountController implements Initializable {
       @FXML
    private Button button1;
    @FXML
    private Button button2;
    @FXML
    private Button button3;

    private boolean formSaved ;
    private boolean formUpdated ; 

    public void initialize(URL location, ResourceBundle resources) {

    }

    // Event handling 

    @FXML
    public button1Clicked(){
       // Handle button 1 clicked
    }

    @FXML
    public button2Clicked(){
    // Handle button 2 clicked
    }

    @FXML
    public button3Clicked(){
    // Handle button 3 clicked
    }


}

I need to move logic in the controller class button1Clicked()... and put it in another class for example:

public class eventManager {
    // form state
    // Event handling ..etc
}

Any idea, javafx frameworks, design patteren ...etc. Or do you recommand put every thing inside the controller class ? What best practices for event handling in complex applications?

Aucun commentaire:

Enregistrer un commentaire