B2 · Episode 8 · 5:15

Building Graphical Interfaces with JavaFX

This episode explains how to build graphical user interfaces using JavaFX, focusing on the Model-View-Controller (MVC) architecture: how the view handles UI elements, the controller manages interactions and updates, and the model encapsulates core logic (e.g., a counter), all working together seamlessly.

Source:Nicolas Hurtubise / classe.iro.umontreal.ca · License:CC BY-SA 4.0

Transcript

  1. qui vous permet de jouer à toutes sortes de jeux de cartes

    that lets you play all kinds of card games

  2. dans le fond la logique de la disposition des cartes ici va changer selon le jeu la

    Essentially, the logic behind how cards are arranged changes depending on the game

  3. façon les cartes sont disposées puis la façon dont on

    — specifically, how the cards are laid out

  4. gagne une partie de cartes

    and how a round is won or lost

  5. Mais essentiellement, peu importe le jeu que vous choisissez,

    But no matter which game you choose

  6. il y a une logique commune.

    there’s a shared underlying logic

  7. Peu importe le jeu que vous choisissez,

    No matter which game you choose

  8. vous devriez toujours être capable d'afficher ce petit menu- là qui vous permet de

    you should always be able to open this small menu

  9. créer une nouvelle partie, recommencer la partie, choisir un autre jeu,

    to start a new game, restart the current one, or switch to another game

  10. annuler, recommencer.

    or undo and redo actions

  11. Puis tout ce qui change dans le fond, c'est la disposition des cartes,

    What actually changes is only how the cards are arranged

  12. puis la façon de détecter si on a gagné ou si on a perdu.

    and how victory or defeat is detected

  13. Dans le fond, il y a toute la vue ici,

    In fact, most of this interface here

  14. la grosse majorité de la vue ici va être réutilisée d'un jeu à l'autre,

    is reused across different games

  15. ce qui veut dire que vraiment si on écrit chacun des jeux cartes comme étant un

    That means if we build each card game as a separate module

  16. modèle séparé, tout ce qui nous reste à faire c'est de brancher ça dans

    we just need to plug it into this main program

  17. ce programme- là, puis dans

    and into

  18. le fond on a toute la partie de la vue qui est déjà faite.

    the underlying view layer, which is already built

  19. quand on veut rajouter un nouveau jeu de cartes.

    when we want to add a new card game

  20. Ok, on va regarder un exemple un petit peu plus concret en JavaFX.

    OK, let’s look at a more concrete example in JavaFX

  21. Donc il y a le fichier MVC.

    Here is the MVC project file

  22. zip qui devrait être disponible sur Studium.

    which should be available for download on Studium

  23. On va aller regarder un petit programme qui permet d'entrer des nombres.

    We’ll examine a small program that lets users enter numbers

  24. Donc c'est ici le programme qu'on va avoir.

    This is the program we’ll run

  25. Puis quand on clique sur ajouter dans le fond, ça va ajouter ça à un compteur,

    When you click ‘Add’, the number gets added to a counter

  26. puis le résultat va s'afficher ici.

    and the result appears here

  27. Fait que allons regarder ça,

    So, let's take a look at this,

  28. allons regarder ça ici.

    let's examine it here.

  29. Comment est- ce qu'on peut faire ça?

    How can we implement this?

  30. Bien, on va vouloir une classe qui extende application

    Well, we’ll need a class that extends Application,

  31. et qui nous permet de simplement définir les éléments graphiques qu'on veut

    one that lets us simply define the graphical elements we want to use.

  32. utiliser. Dans la méthode start ici, bien on va lui définir nos

    In the start method here, we’ll define our components for the title and the area where we’ll enter numbers,

  33. composantes pour le titre, pour la zone où on va rentrer nos nombres.

    the 'Add' button, and the output — i.e., what we want to display — and make it look nice.

  34. le bouton pour ajouter,

    the 'Add' button,

  35. la sortie donc qu'est ce qu'on veut afficher, on va aller rendre ça joli.

    the output — i.e., what we want to display — and make it look nice.

  36. On va aller définir un événement qui peut se produire quand on clique sur le bouton,

    We’ll define an event that triggers when the button is clicked,

  37. puis dans le fond, ça va être le gros de notre classe Vue.

    and essentially, that’s the core of our Vue class.

  38. Notre classe Vue va avoir une fonction update_output qu'on peut appeler et qui va

    Our Vue class will have an update_output function that we can call,

  39. permettre de modifier le texte qui est affiché dedans.

    to update the text displayed inside.

  40. Donc si je lance ce programme- là.

    Now, if I run this program,

  41. On va regarder ce qui se passe.

    let’s see what happens.

  42. Si je lance ce programme- là, je vais avoir mon super compteur,

    running it gives me my super counter,

  43. je peux rentrer des nombres, puis quand je clique sur ajouter ici,

    I can enter numbers, then click 'Add' here,

  44. l'événement qui se produit c'est celui- là ici.

    and the event that fires is this one here.

  45. C'est simplement d'aller avertir le contrôleur qu'on veut ajouter un nombre à

    It simply notifies the controller that

  46. partir de la valeur qui est dans

    we want to add a number based on the value in

  47. cet input- là.

    this input field.

  48. Donc cet input- là, on peut aller chercher la valeur qui est ici avec cette

    For this input field, we can retrieve its value using this function,

  49. fonction- là. Puis dans le fond, voilà,

    and essentially, that’s it:

  50. le contrôleur écrit dans la vue directement, le contrôleur écrit ici.

    the controller writes directly into the view — right here.

  51. Au moment d'instancier le contrôleur,

    When instantiating the controller,

  52. on lui passe en paramètre une référence à l'instance de vue qu'on a en ce moment.

    we pass it, as a parameter, a reference to the current view instance.

  53. Puis à ce moment- là,

    Then, at that moment,

  54. cette instance- là va pouvoir être référencée dans l'autre sens.

    this instance can be referenced in the opposite direction.

  55. Si on va regarder au niveau du contrôleur, qu'est- ce qui se passe?

    If we look at the controller level, what happens?

  56. Le contrôleur, au moment de le créer,

    When the controller is created,

  57. va simplement garder une référence à la vue pour pouvoir aller demander à la vue de

    it simply keeps a reference to the view, so it can ask the view to

  58. modifier ce qui est affiché.

    update what is displayed.

  59. Puis le contrôleur va aller créer le modèle.

    Then, the controller will create the model.

  60. Donc le contrôleur va aller créer le modèle,

    So the controller creates the model—

  61. le modèle qui est simplement un compteur, donc un compteur qui

    a model that is simply a counter,

  62. possède une fonction ajouter, donc on peut rajouter quelque chose,

    which has an 'add' function, allowing us to add values,

  63. puis on peut aller chercher la valeur totale avec tout ce qui a été ajouté.

    and then retrieve the total sum of all added values.

  64. Donc ici c'est vraiment le modèle de l'application,

    So here, this model is truly the application's core model,

  65. c'est le problème abstrait qui est essentiellement de compter,

    the abstract problem at its heart: counting,

  66. de rajouter des nombres, compter.

    adding numbers and accumulating them.

  67. C'est quelque chose qui peut clairement exister sans interface graphique,

    It can clearly exist without any graphical interface,

  68. c'est quelque chose qu'on pourrait intégrer dans un autre projet,

    it could be integrated into another project,

  69. c'est quelque chose où on pourrait faire une interface textuelle à ça.

    or we could build a text-based interface for it.

  70. Fait que le contrôleur ici va aller garder une référence à la vue,

    So the controller keeps a reference to the view,

  71. va aller créer le modèle, puis va commencer par aller mettre à jour la vue en lui

    creates the model, and then starts by updating the view,

  72. demandant d'utiliser la valeur du modèle comme output.

    asking it to use the model’s current value as its output.

  73. quand on clique sur le bouton

    When the user clicks the button,

  74. pour rajouter un nombre dans notre

    to add a number in our view,

  75. vue ça appelle la fonction du contrôleur avec le texte qui correspond au nombre

    the corresponding controller function is called, passing in the text representing that number,

  76. qu'on veut rajouter.

    i.e., the number we want to add.

  77. Cette fonction- là dans le fond

    This function essentially will,

  78. va aller lire ce qui a été rentré,

    read what was entered,

  79. va demander au modèle de rajouter ce nombre- là,

    ask the model to add that number,

  80. puis va demander à la vue de se mettre

    and then ask the view to update itself accordingly,

  81. à jour en conséquence.

    completing the entire process.

  82. On fait un try catch ici parce que quand on essaie de valider

    We use a try-catch here because when we try to validate the input,

  83. l'entrée, dans le fond quand on essaie de lire le texte comme étant un double,

    we’re essentially trying to read the text as a double,

  84. peut- être que les utilisateurs ont écrit n'importe quoi, puis à ce moment- là,

    and users might have entered anything at all—so at that point,

  85. le modèle peut pas fonctionner avec ça.

    the model can’t work with that.

  86. Ce qui va se passer, c'est qu'on va, au moment du catch, dans le fond,

    What will happen is that, when the catch block runs,

  87. quand on se rend compte qu'il y a eu une erreur, on

    i.e., the moment we detect an error,

  88. va aller réinitialiser le modèle à zéro,

    we’ll reset the model to its initial state,

  89. puis on va aller afficher dans la vue erreur.

    and then display an error message in the view.