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

 Java Discussion :

[Swing] Remplir une JList


Sujet :

Java

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 72
    Points : 35
    Points
    35
    Par défaut [Swing] Remplir une JList
    Bonjour,

    Je souhaite remplire une jListe. Ca ne doit pas etre compliqué mais ca le devien quand on ne parle pas anglais ou je ne sais pas bien chercher.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     private javax.swing.JList jList1;
     private javax.swing.JList jList2;
    c'est le code generé par netbeans.

    Je fait un applet.

    merci d'avance

  2. #2
    Membre confirmé Avatar de NeptuS
    Profil pro
    Inscrit en
    Août 2005
    Messages
    392
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2005
    Messages : 392
    Points : 508
    Points
    508
    Par défaut
    euu ... sans regarder la javadoc ... ça serait pas jList1.add("toto"); par hasard ?

  3. #3
    Membre confirmé Avatar de NeptuS
    Profil pro
    Inscrit en
    Août 2005
    Messages
    392
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2005
    Messages : 392
    Points : 508
    Points
    508
    Par défaut
    au cas où ... jte file 1 très bonne adresse (je crois pas que tu puisse trouver mieux, mis à part côté explications ...)

    http://java.sun.com/j2se/1.5.0/docs/api/index.html

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 72
    Points : 35
    Points
    35
    Par défaut
    non ce n'est pas jList1.add("toto");

    et je suis un peu faché avec l'anglais

    le message d'erreur est
    can't find symbole
    symbole : methode add(java.lang.string)
    location : class javax.swing.jlist

  5. #5
    Membre confirmé Avatar de NeptuS
    Profil pro
    Inscrit en
    Août 2005
    Messages
    392
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2005
    Messages : 392
    Points : 508
    Points
    508
    Par défaut
    voilou voilou .. je me suis penché pour toi sur cette javadoc.
    On y trouve deux méthodes (de la classe JList) :

    void setListData(Object[] listData)
    void setListData(Vector<?> listData)
    Donc, pour ton information, tu peux procéder de la façon suivante :


    Etape 1 : créer un Vector (java.util.Vector) et y stocker la liste de tes données.

    Etape 2 : créer ta JList en lui passant en paramètre ton Vector : [quoteJList(Vector<?> listData)[/quote]


    Etape 3 : lors d'une modification quelconque, tu modifie ton Vector et tu utilise la méthode
    void setListData(Vector<?> listData)


    Voila ... je vois pas ce qu'il y a de plus à savoir ..... ahh si .... parler Anglais .. C'est très utile tu sais .... sauf en mapple je crois

  6. #6
    Membre éprouvé
    Profil pro
    Eleveur de cornichons
    Inscrit en
    Juin 2002
    Messages
    1 074
    Détails du profil
    Informations personnelles :
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Eleveur de cornichons
    Secteur : Finance

    Informations forums :
    Inscription : Juin 2002
    Messages : 1 074
    Points : 1 166
    Points
    1 166
    Par défaut
    Pour ajouter un élément, en retirer, et tout, faut passer par un DefaultListModel.
    Ensuite, addElement() pour ajouter un élément à la fin de la liste.

    Nas'

  7. #7
    Membre confirmé Avatar de NeptuS
    Profil pro
    Inscrit en
    Août 2005
    Messages
    392
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2005
    Messages : 392
    Points : 508
    Points
    508
    Par défaut
    arf c'est mieux en effet

    Merciiiiii ...

  8. #8
    Membre éprouvé
    Profil pro
    Eleveur de cornichons
    Inscrit en
    Juin 2002
    Messages
    1 074
    Détails du profil
    Informations personnelles :
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Eleveur de cornichons
    Secteur : Finance

    Informations forums :
    Inscription : Juin 2002
    Messages : 1 074
    Points : 1 166
    Points
    1 166
    Par défaut
    Citation Envoyé par NeptuS
    arf c'est mieux en effet
    Pour ajouter un seul élément, c'est même la seule solution
    Moi aussi au début j'ajoutais tout dans un Vector et insérer le tout d'un coup. Mais ce n'est pas très pratique. Puis le DefaultListModel propose plein de méthodes utiles pour manipuler la liste.

    Nas'

  9. #9
    Membre confirmé Avatar de NeptuS
    Profil pro
    Inscrit en
    Août 2005
    Messages
    392
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2005
    Messages : 392
    Points : 508
    Points
    508
    Par défaut
    et c'est surtout beaucoup plus léger ...

  10. #10
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 72
    Points : 35
    Points
    35
    Par défaut
    Je vais essayer ca tout de suite

    merci

  11. #11
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 72
    Points : 35
    Points
    35
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
        DefaultListModel model = new DefaultListModel();
        jList1=new JList(model);
        model.addElement("toto");
    avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    import javax.swing.*;
    c'est comme ca non???

    ca ne marche pas. rien ne s'affiche dans la liste.

  12. #12
    Membre averti
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    390
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 390
    Points : 432
    Points
    432
    Par défaut
    il faut que tu affectes le model apres l avoir initialisé :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    DefaultListModel model = new DefaultListModel();
    model.addElement("toto"); 
    jList1=new JList(model);
    et si tu fais des modif sur le model (ajout suppression d'element) tu fais un updateUI() sur la JLis. Donc ca ca devrait marcher :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    DefaultListModel model = new DefaultListModel();
    jList1=new JList(model);
    model.addElement("toto"); 
    jList1.updateUI();

  13. #13
    Membre habitué
    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Juin 2005
    Messages
    175
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Chef de projet NTIC

    Informations forums :
    Inscription : Juin 2005
    Messages : 175
    Points : 145
    Points
    145
    Par défaut
    Citation Envoyé par NeptuS
    au cas où ... jte file 1 très bonne adresse (je crois pas que tu puisse trouver mieux, mis à part côté explications ...)

    http://java.sun.com/j2se/1.5.0/docs/api/index.html
    sarcastique ?

    *************************************

    Pour remplir ta liste, tu peux tout simplement faire comme dans cet exemple: (ou getAllWires te renvoie un Vector rempli bien entendu)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Vector wiresVector = DbManager.getAllWires();
            JList lsWiresList = new JList(wiresVector);

  14. #14
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 72
    Points : 35
    Points
    35
    Par défaut
    barbu0055 ca ne marche pas.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
        public void init() {
            DefaultListModel model = new DefaultListModel();
            model.addElement("toto");
            jList1=new JList(model);
            jList1.updateUI();
        }
    Je fait quoi de mauvais ???

  15. #15
    Membre éprouvé
    Profil pro
    Eleveur de cornichons
    Inscrit en
    Juin 2002
    Messages
    1 074
    Détails du profil
    Informations personnelles :
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Eleveur de cornichons
    Secteur : Finance

    Informations forums :
    Inscription : Juin 2002
    Messages : 1 074
    Points : 1 166
    Points
    1 166
    Par défaut
    Essaye d'ajouter après la création de la liste :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
      public void init() {
            DefaultListModel model = new DefaultListModel();
            jList1=new JList(model);
            model.addElement("toto");      
            jList1.updateUI();
        }
    Nas'

  16. #16
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 72
    Points : 35
    Points
    35
    Par défaut
    voici mon code mais ca ne marche toujours pas

    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
    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
    import javax.xml.parsers.*;
    import java.io.*;
    import javax.swing.*;
     
    /*
     * XMLeditor.java
     *
     * Created on 23 août 2005, 09:45
     */
     
    /**
     *
     * @author  ulisateur
     */
    public class XMLeditor extends javax.swing.JApplet {
     
        /** Creates new form XMLeditor */
        public XMLeditor() {
            initComponents();
        }
     
     
     
        // <editor-fold defaultstate="collapsed" desc=" ParseXML ">
        public void ParseXML() {
     
        }
        // </editor-fold>
     
        /** 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.
         */
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
        private void initComponents() {
            jButton1 = new javax.swing.JButton();
            jTextField1 = new javax.swing.JTextField();
            jButton2 = new javax.swing.JButton();
            jSeparator1 = new javax.swing.JSeparator();
            jList1 = new javax.swing.JList();
            jList2 = new javax.swing.JList();
            jButton3 = new javax.swing.JButton();
            jButton4 = new javax.swing.JButton();
            jTextField2 = new javax.swing.JTextField();
            jComboBox1 = new javax.swing.JComboBox();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jButton5 = new javax.swing.JButton();
     
            getContentPane().setLayout(null);
     
            jButton1.setText("Ajout");
            getContentPane().add(jButton1);
            jButton1.setBounds(10, 270, 160, 25);
     
            getContentPane().add(jTextField1);
            jTextField1.setBounds(10, 250, 160, 20);
     
            jButton2.setText("Suppression");
            getContentPane().add(jButton2);
            jButton2.setBounds(10, 300, 160, 25);
     
            jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
            getContentPane().add(jSeparator1);
            jSeparator1.setBounds(190, 0, 10, 340);
     
            jList1.setMaximumSize(new java.awt.Dimension(100, 100));
            jList1.setMinimumSize(new java.awt.Dimension(100, 100));
            jList1.setPreferredSize(new java.awt.Dimension(100, 100));
            getContentPane().add(jList1);
            jList1.setBounds(10, 30, 160, 210);
     
            jList2.setMaximumSize(new java.awt.Dimension(100, 100));
            jList2.setMinimumSize(new java.awt.Dimension(100, 100));
            jList2.setPreferredSize(new java.awt.Dimension(100, 100));
            getContentPane().add(jList2);
            jList2.setBounds(200, 30, 160, 210);
     
            jButton3.setText("Ajout");
            getContentPane().add(jButton3);
            jButton3.setBounds(200, 270, 160, 25);
     
            jButton4.setText("Suppression");
            getContentPane().add(jButton4);
            jButton4.setBounds(200, 300, 160, 25);
     
            getContentPane().add(jTextField2);
            jTextField2.setBounds(200, 250, 160, 21);
     
            getContentPane().add(jComboBox1);
            jComboBox1.setBounds(420, 20, 80, 21);
     
            jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel1.setText("Cat\u00e9gories");
            getContentPane().add(jLabel1);
            jLabel1.setBounds(10, 10, 150, 15);
     
            jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel2.setText("Liens");
            getContentPane().add(jLabel2);
            jLabel2.setBounds(190, 10, 170, 15);
     
            jLabel3.setText("Langue");
            getContentPane().add(jLabel3);
            jLabel3.setBounds(370, 20, 50, 15);
     
            jButton5.setText("Avanc\u00e9e");
            getContentPane().add(jButton5);
            jButton5.setBounds(379, 230, 100, 25);
     
        }
        // </editor-fold>
     
     
     
     
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JButton jButton5;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JList jList1;
        private javax.swing.JList jList2;
        private javax.swing.JSeparator jSeparator1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        // End of variables declaration
     
        public void init() { 
          DefaultListModel model = new DefaultListModel(); 
          jList1=new JList(model);
          model.addElement("toto");      
          jList1.updateUI(); 
        } 
    }

  17. #17
    Futur Membre du Club
    Inscrit en
    Août 2005
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Août 2005
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    je suis pas sur de moi car j'ai jamais programmé d'applet mais...

    Dans ton code tu ajout la methode init a la fin.

    Je pense plutôt que tu devrait ajouter la déclaration de ton model comme ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    javax.swing.JTextField.DefaultListModel model
    Dans la partie variables déclaration qui est générée automatiquement.

    et les lignes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    jList1=new JList(model);
          model.addElement("toto");     
          jList1.updateUI();
    a la fin de la méthode private void initComponents() qui doit aussi être auto générée.

    Voilà parce que sinon le code qu'on t'as donné marche bien (je l'utilise pour une application Java).

  18. #18
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 72
    Points : 35
    Points
    35
    Par défaut
    non ca ne marche toujour pas

    je pensai que cela m'aurai pris 2min

    mais bon pour trouver le petit truc qui cloche c'est toujour long

  19. #19
    Membre confirmé Avatar de NeptuS
    Profil pro
    Inscrit en
    Août 2005
    Messages
    392
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2005
    Messages : 392
    Points : 508
    Points
    508
    Par défaut
    ben finalement essaye peut-être ma méthode ... elle prend peur-être plus de temps mais elle est plus ... "intuitive" ... et puis peut-être qu'elle marche après tout, elle!

    ++ et bon courage ...

  20. #20
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 72
    Points : 35
    Points
    35
    Par défaut
    pourkoi aucune de ces methode ne marche ???

    Un applet avec une JList n'est pas possible ou quoi ???

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Réponses: 19
    Dernier message: 14/05/2011, 03h13
  2. Remplir une jList à partir d'une base mySQL
    Par aymentouhent dans le forum JDBC
    Réponses: 1
    Dernier message: 26/10/2009, 12h27
  3. Remplir une JList
    Par RodEpsi dans le forum Composants
    Réponses: 1
    Dernier message: 11/08/2008, 16h18
  4. [SWING]Remplir un JList de chaînes
    Par Goupo dans le forum Composants
    Réponses: 2
    Dernier message: 08/06/2007, 13h23
  5. [SWING] remplir une jtable avec une matrice de double
    Par Psykorel dans le forum Composants
    Réponses: 3
    Dernier message: 04/01/2006, 14h14

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