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 :

[JList] impossible d'avoir les valeurs


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    506
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 506
    Par défaut [JList] impossible d'avoir les valeurs
    salut,

    impossible d'avoir les valeur que je selectionne a la souris ou avec les fleches directionnelles l'element selectionner d'une jlist, chaque fois que je clique sur un element j'ai toujours une valeur null. je precise aussi que je developpe avec l'ihm integrée de netbean

    1) j'ai creer une variable nommé model1 de la clase DefaultListModel();
    j'ai initialiser model1 par Jlist.setModel(model1);
    et par jList jList1= JList(model1);

    les informations que j'ai introduit dans le model1 est un vecteur
    voici l'initialisation de model1 :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     private void conversionmodel(Vector vector){
            int i;
            for(i=0;i<vector.size();i++)
            model1.addElement(vector.elementAt(i));
        }
    la methode que j'utilise pour prendre une valeur selectionner :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {                                    
            // TODO add your handli
     
     
     
            if(jList1.getSelectedValue()==null)
            {
                JOptionPane.showMessageDialog(null,"pas de categorie"," ",JOptionPane.ERROR_MESSAGE );
            }
            else{
            jTextField1.setText((String) jList1.getSelectedValue());
        }                                   
       }
    le probleme est que j'ai toujour une valeur null quelque sois ma selection
    j'utilise la methode valuechanged

    mon code complet :
    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
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /*
     * IsousCategorie.java
     *
     * Created on 24 mai 2009, 21:18:06
     */
     
    package com.java.ihm;
     
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.DefaultListModel;
    import com.mysql.source.*;
    import java.util.Vector;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    /**
     *
     * @author modde
     */
    public class IsousCategorie extends javax.swing.JFrame {
     
     
     
        private String nomcategorie="";
        private DirectiveMysql directivemysql=new DirectiveMysql();
        private DefaultListModel model1 =new DefaultListModel();
        private DefaultListModel model2 =new DefaultListModel();
        /** Creates new form IsousCategorie */
        public IsousCategorie() throws SQLException {
            initComponents();
            directivemysql.setPassword(Attribution.password);
            directivemysql.setPseudo(Attribution.pseudo);
            //directivemysql.connectionMysql();
            conversionmodel(directivemysql.afficheCategorie());
            jList1=new JList(model1);
            jScrollPane1 =new JScrollPane(jList1);
        }
     private void conversionmodel(Vector vector){
            int i;
            for(i=0;i<vector.size();i++)
            model1.addElement(vector.elementAt(i));
        }
        /** 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() {
            bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
     
            jLabel1 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jScrollPane1 = new javax.swing.JScrollPane();
            jList1 = new javax.swing.JList();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jTextField2 = new javax.swing.JTextField();
            jScrollPane2 = new javax.swing.JScrollPane();
            jList2 = new javax.swing.JList();
            jLabel4 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jLabel5 = new javax.swing.JLabel();
            jButton3 = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jLabel1.setText("ISousCategorie");
     
            jTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField1ActionPerformed(evt);
                }
            });
     
            jList1.setModel(model1);
     
            org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jList1, org.jdesktop.beansbinding.ELProperty.create("${selectedValue}"), jList1, org.jdesktop.beansbinding.BeanProperty.create("selectedElement"));
            bindingGroup.addBinding(binding);
     
            jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
                public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                    jList1ValueChanged(evt);
                }
            });
            jScrollPane1.setViewportView(jList1);
     
            jLabel2.setText("Categorie choisie :");
     
            jLabel3.setText("Creation d'une sous categorie :");
     
            jList2.setModel(model2);
            jScrollPane2.setViewportView(jList2);
     
            jLabel4.setText("Refresh");
     
            jButton1.setText("Valider");
     
            jButton2.setText("Effacer");
     
            jLabel5.setText("Nom Sous Categorie :");
     
            jButton3.setText("Effacer");
     
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(32, 32, 32)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 167, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, jTextField1)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(layout.createSequentialGroup()
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(layout.createSequentialGroup()
                                            .add(12, 12, 12)
                                            .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, jButton3, 0, 0, Short.MAX_VALUE)
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                        .add(jLabel5))
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                        .add(org.jdesktop.layout.GroupLayout.LEADING, jTextField2)
                                        .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                                    .add(300, 300, 300))
                                .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 362, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .add(layout.createSequentialGroup()
                            .add(215, 215, 215)
                            .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 149, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jLabel1)
                    .add(33, 33, 33)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jLabel3))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jLabel5)
                        .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 104, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(jButton3)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(jButton2)
                                    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(84, Short.MAX_VALUE))
            );
     
            bindingGroup.bind();
     
            pack();
        }// </editor-fold>                        
     
        private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {                                    
            // TODO add your handli
     
     
     
            if(jList1.getSelectedValue()==null)
            {
                JOptionPane.showMessageDialog(null,"pas de categorie"," ",JOptionPane.ERROR_MESSAGE );
            }
            else{
            jTextField1.setText((String) jList1.getSelectedValue());
        }                                   
       }
        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
        }                                           
     
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        new IsousCategorie().setVisible(true);
                    } catch (SQLException ex) {
                        Logger.getLogger(IsousCategorie.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JList jList1;
        private javax.swing.JList jList2;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        private org.jdesktop.beansbinding.BindingGroup bindingGroup;
        // End of variables declaration                   
     
    }


    merci d'avance !

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    506
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 506
    Par défaut
    pourtant j'ai bien une liste qui s'affiche , j'ai testé pour voir si les element sont bien inserer
    dans le model, il y a pas de probleme, tous les element s'affiche.

    c'est comme si ma Jlist ne voyait aucun element pourtant elle m'affiche bien la liste.

    help help !
    quelle galere netbean !

  3. #3
    Membre chevronné Avatar de Jabbal'H
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Octobre 2004
    Messages
    403
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2004
    Messages : 403
    Par défaut
    Déjà tu instancie deux fois ta liste, dans le initialize component et dans ton constructeur, de meme pour le modèle.
    Cependant, je ne pense pas que ca vienne de là.
    J'ai vus que tu utilisais du binding, ca ne viendrait pas de là ?

  4. #4
    Membre éclairé
    Inscrit en
    Décembre 2003
    Messages
    44
    Détails du profil
    Informations forums :
    Inscription : Décembre 2003
    Messages : 44
    Par défaut
    Dans ta méthode jList1ValueChanged, tu oublies de faire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) { 
    if (!evt.getValueIsAdjusting()) {
     // le test ici
    }
    }
    Car le listener est toujours appeler avant de sélectionner l'element pour ajustement.

Discussions similaires

  1. Réponses: 2
    Dernier message: 21/11/2008, 13h21
  2. Impossible de charger les valeurs d'un combobox
    Par philoflore dans le forum Macros et VBA Excel
    Réponses: 13
    Dernier message: 08/07/2008, 11h01
  3. [JSTL] Impossible d'afficher les valeurs d'un c:out
    Par nicolas.pied dans le forum Taglibs
    Réponses: 8
    Dernier message: 27/04/2008, 23h49
  4. [AJAX] Impossible de récupérer les valeurs en Post
    Par Tommyl dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 19/06/2007, 21h30
  5. [PEAR][HTML_QuickForm] Impossible de récupérer les valeurs du formulaire
    Par Mainman dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 29/05/2007, 13h15

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