IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaFX Discussion :

Afficher contenu ComboBox dans TextField


Sujet :

JavaFX

  1. #1
    Débutant   Avatar de FCL31
    Profil pro
    Inscrit en
    Août 2007
    Messages
    885
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 885
    Points : 267
    Points
    267
    Par défaut Afficher contenu ComboBox dans TextField
    Bonjour

    (j'utilise JavaFX)
    J'ai une ComboBox qui est alimentée par une base de données SQL.

    La table de la base contient 4 champs :
    - id = INT (clé primaire) AI
    - question = VARCHAR
    - response = VARCHAR
    - difficulté = INT (1 ou 2)

    J' "alimente" la ComboBox sans problème.

    Ce que je voudrais mais que je n'arrive pas a trouvé (ou peut être pas su l'adapter) alors que je pense que la question a été posées 10 000 fois, c'est qu'en sélection le choix dans la combobox, les information correspondantes, s'affichent dans des TextField (si possible sans interagir sur un bouton seulement le clic sur le choix dans le ComboBox).

    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    package game;
     
    import dao.DAO;
    import dao.DAOFactory;
    import javafx.geometry.Insets;
    import javafx.scene.Parent;
    import javafx.scene.control.Button;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.Label;
    import javafx.scene.control.RadioButton;
    import javafx.scene.control.TextField;
    import javafx.scene.control.ToggleGroup;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.VBox;
    import pojo.Questions;
     
     
    /**
     *
     * @author romain boyrie
     */
    public final class Admin extends Parent {
     
     
        VBox initiatedAdm;
     
     
        public Admin() {
        }
     
     
        public BorderPane initAdm() {
     
     
            DAO<Questions> questionsDao = DAOFactory.getQuestionsDAO();
     
     
     
     
            BorderPane mainPane = new BorderPane();
            VBox modifLvlPane = new VBox();
            VBox AddLvlPane = new VBox();
     
            GridPane topPane = new GridPane();
            topPane.setHgap(20);
            topPane.setVgap(5);
            topPane.setPadding(new Insets(50));
            topPane.setMinWidth(1000);
    //        topPane.setGridLinesVisible(true);
     
            GridPane bottomPane = new GridPane();
            bottomPane.setHgap(20);
            bottomPane.setVgap(5);
            bottomPane.setPadding(new Insets(50));
            bottomPane.setMinWidth(1000);
    //        bottomPane.setGridLinesVisible(true);
     
     
            Label lblModifQuestion = new Label("Modifier une question");
            lblModifQuestion.setStyle("-fx-font: 24 arial;");
            Label lblModifChoixQuestion = new Label("Choix : ");
            lblModifChoixQuestion.setStyle("-fx-font: 24 arial;");
            Label lblAddQuestion = new Label("Ajouter une question");
            lblAddQuestion.setStyle("-fx-font: 24 arial;");
            Label lblQuestionModif = new Label("Question :");
            lblQuestionModif.setStyle("-fx-font: 24 arial;");
            Label lblReponseModif = new Label("Reponse :");
            lblReponseModif.setStyle("-fx-font: 24 arial;");
            Label lblQuestionAdd = new Label("Question :");
            lblQuestionAdd.setStyle("-fx-font: 24 arial;");
            Label lblReponseAdd = new Label("Reponse :");
            lblReponseAdd.setStyle("-fx-font: 24 arial;");
     
            Label lblLevelModif = new Label("Choix niveau :");
            lblLevelModif.setStyle("-fx-font: 24 arial;");
            Label lblLevelAdd = new Label("Choix niveau :");
            lblLevelAdd.setStyle("-fx-font: 24 arial;");
     
     
    		/*
    		Alimentation de la ComboBox
    		*/
            ComboBox<Questions> cbQuestions = new ComboBox();
            for (Integer i = 1; i <= questionsDao.count(); i++) {
                cbQuestions.getItems().add(questionsDao.find(i));
            }
     
     
            TextField tbModifQuestion = new TextField();
            TextField tbModifReponse = new TextField();
     
     
            TextField tbAddQuestion = new TextField();
            TextField tbAddReponse = new TextField();
     
            Button btnValider = new Button("Modifier");
            btnValider.setPrefSize(100, 50);
            Button btnDelet = new Button("Supprimer");
            btnDelet.setPrefSize(100, 50);
            Button btnAdd = new Button("Ajouter");
            btnAdd.setPrefSize(100, 50);
     
     
            final ToggleGroup rbGroupModifLvl = new ToggleGroup();
            RadioButton rbEasyLevelModif = new RadioButton("Niveau Facile");
            RadioButton rbDifficultLevelModif = new RadioButton("Niveau Difficile");
            rbDifficultLevelModif.setStyle("-fx-font: 20 arial;");
            rbEasyLevelModif.setStyle("-fx-font: 20 arial;");
            rbEasyLevelModif.setToggleGroup(rbGroupModifLvl);
            rbDifficultLevelModif.setToggleGroup(rbGroupModifLvl);
            modifLvlPane.getChildren().addAll(rbEasyLevelModif,rbDifficultLevelModif);
     
            final ToggleGroup rbGroupAddLvl = new ToggleGroup();
            RadioButton rbEasyLevelAdd = new RadioButton("Niveau Facile");
            RadioButton rbDifficultLevelAdd = new RadioButton("Niveau Difficile");
            rbDifficultLevelAdd.setStyle("-fx-font: 20 arial;");
            rbEasyLevelAdd.setStyle("-fx-font: 20 arial;");
            rbEasyLevelAdd.setToggleGroup(rbGroupAddLvl);
            rbDifficultLevelAdd.setToggleGroup(rbGroupAddLvl);
            AddLvlPane.getChildren().addAll(rbEasyLevelAdd,rbDifficultLevelAdd);  
     
            topPane.setConstraints(lblModifQuestion, 0,0);
            topPane.setConstraints(lblModifChoixQuestion, 0,1);
            topPane.setConstraints(lblQuestionModif, 0,2);
            topPane.setConstraints(lblReponseModif, 0,3);
            topPane.setConstraints(cbQuestions, 1,1);
            topPane.setConstraints(tbModifQuestion, 1,2);
            topPane.setConstraints(tbModifReponse, 1,3);
            topPane.setConstraints(modifLvlPane, 2,3);
            topPane.setConstraints(btnValider, 3,2);
            topPane.setConstraints(btnDelet, 3,3);
     
     
            topPane.getChildren().add(lblModifQuestion);
            topPane.getChildren().add(lblModifChoixQuestion);
            topPane.getChildren().add(lblQuestionModif);
            topPane.getChildren().add(lblReponseModif);
            topPane.getChildren().add(cbQuestions);
            topPane.getChildren().add(tbModifQuestion);
            topPane.getChildren().add(tbModifReponse);
            topPane.getChildren().add(modifLvlPane);
            topPane.getChildren().add(btnValider);
            topPane.getChildren().add(btnDelet);
     
            bottomPane.setConstraints(lblAddQuestion, 0,0);
            bottomPane.setConstraints(lblQuestionAdd, 0,2);
            bottomPane.setConstraints(lblReponseAdd, 0,3);
            bottomPane.setConstraints(tbAddQuestion, 1,2);
            bottomPane.setConstraints(tbAddReponse, 1,3);
            bottomPane.setConstraints(AddLvlPane, 2,3);
            bottomPane.setConstraints(btnAdd, 3,3);
     
     
            bottomPane.getChildren().add(lblAddQuestion);
            bottomPane.getChildren().add(lblQuestionAdd);
            bottomPane.getChildren().add(lblReponseAdd);
            bottomPane.getChildren().add(tbAddQuestion);
            bottomPane.getChildren().add(tbAddReponse);
            bottomPane.getChildren().add(AddLvlPane);
            bottomPane.getChildren().add(btnAdd);
     
            mainPane.setTop(topPane);
            mainPane.setBottom(bottomPane);
     
     
     
     
            return mainPane;
     
        }   
    }
    Merci d'avance pour votre aide.

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 854
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 854
    Points : 22 878
    Points
    22 878
    Billets dans le blog
    51
    Par défaut
    Simple a mettre en place par un ecouteur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { textField.setText(...) });
    Ou par binding :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    textField.textProperty().bind(new StringBinding() {
        {
            bind(comboBox.getSelectionModel().selectedItemProperty());
        }
     
     
        @Override
        public void dispose() {
            unbind(comboBox.getSelectionModel().selectedItemProperty());
        }
     
        @Override
        protected String computeValue() {
            Toto value = comboBox.getSelectionModel().getSelectedItem();
            String result = (value == null) ? null : ...
            return result;
        }
    });

  3. #3
    Membre averti
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Juillet 2013
    Messages
    269
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2013
    Messages : 269
    Points : 434
    Points
    434
    Par défaut
    Bonjour bouye,
    je me permets de rebondir sur le sujet en attendant qu'il soit marqué résolu ; tu proposes 2 solutions pour répondre à un même besoin, la méthode "classique" et le binding. Or il me semble avoir lu une fois qu'il ne fallait pas abuser du binding à outrance, ce que j'ai tendance à faire. Est ce une légende urbaine ou cela pénalise-t-il réellement les performances ? Si oui, quelle serait la "bonne pratique" ?
    Merci pour ta réponse et désolé pour le léger HS.

  4. #4
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 854
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 854
    Points : 22 878
    Points
    22 878
    Billets dans le blog
    51
    Par défaut
    Certes mais d'un autre cote un binding bien optimise est plus efficace qu'une palanquée de ChangeListener imbriques les un derrière les autres. Ce qu'il faut c'est ne pas en avoir 450.000 et surtout penser a les casser (unbind(), dispose()) quand on en a plus besoin. La on en a qu'un seul, je ne pense pas que ca soit un trou de performance...

  5. #5
    Membre averti
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Juillet 2013
    Messages
    269
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2013
    Messages : 269
    Points : 434
    Points
    434
    Par défaut
    OK, en fait je parlais de mon cas perso où j'ai par exemple un écran avec une vingtaine de champs bindés sur les attributs d'une entité. Au moment de la fermeture de mon onglet, je les unbind tous un par un.
    Voilà je me demandais simplement si c'était cette approche à privilégier.
    Merci pour ta réponse.

Discussions similaires

  1. Afficher contenu combobox dans une JTable
    Par hama amadou dans le forum Débuter
    Réponses: 4
    Dernier message: 01/07/2015, 15h39
  2. Comment afficher 1 combobox dans 2 textbox
    Par scorpionrun dans le forum VB.NET
    Réponses: 4
    Dernier message: 20/08/2009, 04h28
  3. Afficher contenu console dans textview !
    Par mddpp dans le forum GTK+ avec C & C++
    Réponses: 0
    Dernier message: 13/07/2009, 18h19
  4. [AC-2000] Afficher contenu repertoire dans Listbox
    Par Remus91 dans le forum IHM
    Réponses: 7
    Dernier message: 20/05/2009, 17h09
  5. Pb Affiche contenu Recordset dans zone de liste
    Par stan314 dans le forum Access
    Réponses: 5
    Dernier message: 11/05/2006, 16h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo