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

Composants Java Discussion :

Problème avec JComboBox


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Invité
    Invité(e)
    Par défaut Problème avec JComboBox
    Bonjour,

    Je suis entrain de développer un petit programme java avec une interface graphique. Je développe avec NetBeans IDE 6.1 et j'utilise la librairie SWING.

    Le problème auquel je suis confronter, est d'alimenter une JComboBox à partir d'un Object [].

    Ci-joint le code source de la classe ou se situe mon Object [] (ListeOfServers.java)

    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
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package com.labosun.servertraveler.classes;
     
    /**
     *
     * @author Adrien
     */
     
    public class ListOfServers 
    {
        public static final String bordeaux = "bordeaux2";
        public static final String caen = "caen2";
        public static final String nantes = "nantes2";
     
     
     
        public static Object[] getFrance()
        {
            Object[] tabFrance = new Object[] {bordeaux,caen,nantes};
     
            return tabFrance;
        }
    }
    et voici le code de la classe ou je veux alimenter ma JComboBox, c'est la classe qui contient l'interface graphique (Main.java)

    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
    /*
     * Main.java
     *
     * Created on 11 février 2009, 16:16
     */
     
    package com.labosun.servertraveler.view;
     
    import com.labosun.servertraveler.model.TestConnection;
    import com.labosun.servertraveler.classes.ListOfServers;
    import javax.swing.JComboBox;
    /**
     *
     * @author  Adrien
     */
    public class Main extends javax.swing.JFrame {
     
        /** Creates new form Main */
        public Main() {
            initComponents();
            init();
        }
     
        public void init()
        {
            jComboBox1.addItem(ListOfServers.getFrance());
            jTextField1.setText("LOGIN");
        }
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            jToggleButton1 = new javax.swing.JToggleButton();
            jPasswordField1 = new javax.swing.JPasswordField();
            jTextField1 = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jComboBox1 = new javax.swing.JComboBox();
            jLabel3 = new javax.swing.JLabel();
            jToggleButton2 = new javax.swing.JToggleButton();
            jLabel4 = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jToggleButton1.setText("Connexion");
            jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jToggleButton1ActionPerformed(evt);
                }
            });
     
            jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jPasswordField1ActionPerformed(evt);
                }
            });
     
            jLabel1.setText("ID_Booster");
     
            jLabel2.setText("Password");
     
            jLabel3.setText("Server");
     
            jToggleButton2.setText("Exit");
            jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jToggleButton2ActionPerformed(evt);
                }
            });
     
            jLabel4.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
            jLabel4.setText("Server Traveler");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jToggleButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                        .addComponent(jTextField1))
                    .addContainerGap(163, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(263, Short.MAX_VALUE)
                    .addComponent(jToggleButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
                .addGroup(layout.createSequentialGroup()
                    .addGap(79, 79, 79)
                    .addComponent(jLabel4)
                    .addContainerGap(91, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(30, 30, 30)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(8, 8, 8)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jToggleButton1)
                    .addGap(44, 44, 44)
                    .addComponent(jToggleButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>
     
    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
        TestConnection connect = new TestConnection("nantes",jTextField1.getText(), jPasswordField1.getText());
    }
     
    private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }
     
    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
        //Deconnecter
    }
     
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) 
        {
            java.awt.EventQueue.invokeLater(new Runnable() 
            {
                public void run() 
                {
                    new Main().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JPasswordField jPasswordField1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JToggleButton jToggleButton1;
        private javax.swing.JToggleButton jToggleButton2;
        // End of variables declaration
     
    }
    Donc le but recherché, est de récupérer dans ma JComboBox toutes les valeurs de mon Object. La ligne de code source correspondante se trouve dans le deuxieme code source, dans la fonction init()
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    jComboBox1.addItem(ListOfServers.getFrance());
    PS: J'ai mis l'intégralité du code source de mes deux classes concernées, car étant débutant en java mais pas en prog je ne voulais pas passer à coté d'un petit détail


    Merci de votre aide

  2. #2
    Membre émérite Avatar de herch
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    655
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations forums :
    Inscription : Mai 2006
    Messages : 655
    Par défaut
    salut,
    jComboBox1.addItem(ListOfServers.getFrance());
    la méthode addItem ajoute un Object et pas un tableau d'Object. si tu veux ajouter un tableau à ta combobox, ou bien tu passes le tableau au constructeur de JComboBox, ou bien tu boucles sur ton tableau et tu ajoutes les éléments au combo
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    Object[] servers = ListOfServers.getFrance();
    for (int i = 0; i < servers.length; i++)
        jComboBox1.addItem(servers[i]);

  3. #3
    Invité
    Invité(e)
    Par défaut
    Merci c'est sympa, ça marche. Effectivement je n'était pas forcément dans la bonne direction. Mais bon la j'ai compris le système.

    Encore merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème avec JComboBox
    Par amine1980 dans le forum AWT/Swing
    Réponses: 21
    Dernier message: 02/01/2011, 21h20
  2. Problème avec JComboBox
    Par muslim1987 dans le forum Débuter
    Réponses: 2
    Dernier message: 30/06/2008, 16h22
  3. problème avec jcombobox
    Par minooo dans le forum Composants
    Réponses: 2
    Dernier message: 15/01/2008, 12h20
  4. problème avec JComboBox
    Par ulysse031 dans le forum AWT/Swing
    Réponses: 3
    Dernier message: 29/12/2007, 00h01
  5. problème avec JComboBox
    Par ulysse031 dans le forum AWT/Swing
    Réponses: 9
    Dernier message: 22/04/2007, 16h51

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