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

avec Java Discussion :

Add Item dans une Jlist


Sujet :

avec Java

  1. #1
    Futur Membre du Club
    Inscrit en
    Janvier 2008
    Messages
    16
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 16
    Points : 9
    Points
    9
    Par défaut Add Item dans une Jlist
    Bonjour à tous, je reviens par chez vous, car un autre problème me tracasse.
    Je n'ai absolument pas la moindre idée de comment obtenir mon résultat...
    Je m'explique, J'ai une Jframe, et j'ajoute une Jlist dedans (en mode design)...donc ma Jlist est declarée (automatiquement) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
        private javax.swing.JList list_Format;

    Ensuite j'aimerais faire dans le style :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     list_Format.add("item1") ;
    Pour le moment, en dur, ensuite j'essayerais d'y faire passé des variables !

    Merci encore pour votre aide...

    ps: j'ai aussi tenté un
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
      listModel = new DefaultListModel();
                listModel.addElement("item1");
                listModel.addElement("item2");
                listModel.addElement("item3");             
                list_Format = new JList(listModel);
    Sans succés...

  2. #2
    Membre actif Avatar de Seb19
    Homme Profil pro
    Concepteur SOA
    Inscrit en
    Septembre 2005
    Messages
    217
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Deux Sèvres (Poitou Charente)

    Informations professionnelles :
    Activité : Concepteur SOA

    Informations forums :
    Inscription : Septembre 2005
    Messages : 217
    Points : 292
    Points
    292
    Par défaut
    Bonjour,

    Voici un lien qui peut t'aider :
    http://javasearch.developpez.com/j2s...ing/JList.html

  3. #3
    Membre chevronné
    Avatar de CheryBen
    Inscrit en
    Mai 2005
    Messages
    1 599
    Détails du profil
    Informations personnelles :
    Âge : 42

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 599
    Points : 2 197
    Points
    2 197
    Par défaut
    Bonjour, regarde ce tutoriel de sun, c'est bien expliqué.

  4. #4
    Futur Membre du Club
    Inscrit en
    Janvier 2008
    Messages
    16
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 16
    Points : 9
    Points
    9
    Par défaut
    Merci pour ces docs, mais hélas, je ne m'en sors pas !

    Je ne sais pas comment affecter une methode a ma JList qui s'appelle format_List, juste pour faire un ADD ou un REMOVE.

    En prenant l'example de Sun, j'essaye de copié un peu leur logique, et j'ai cette ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     list_Format.addListSelectionListener(this);
    qui ne fonctionne pas !

    Encore merci !

  5. #5
    Futur Membre du Club
    Inscrit en
    Janvier 2008
    Messages
    16
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 16
    Points : 9
    Points
    9
    Par défaut
    Est ce que ce code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    list_Format.setFixedCellHeight(20);
    Appliqué sur un bouton, devrai modifier la taille de ma JList ?
    (sachant qu'a la base le FixedCellHeight = 10 )

    car cela ne fonctionne pas

    Gracie mille !

  6. #6
    Membre actif Avatar de Seb19
    Homme Profil pro
    Concepteur SOA
    Inscrit en
    Septembre 2005
    Messages
    217
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Deux Sèvres (Poitou Charente)

    Informations professionnelles :
    Activité : Concepteur SOA

    Informations forums :
    Inscription : Septembre 2005
    Messages : 217
    Points : 292
    Points
    292
    Par défaut
    Il n'existe pas de méthode ADD et REMOVE pour la class JList.
    Pour initialiser ta liste, il faut :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    String[] data = ["lundi","mardi","mercredi","jeudi","vendredi","samedi","dimanche"]
    JListe list_Format = new JList(data);
    Pour ajouter ou supprimer ensuite un élément de la liste, il faut récupérer les données de la liste par la méthode getModel(), puis la modifier et la recharger dans la liste avec setModel(ListModel model)

  7. #7
    Membre actif Avatar de Seb19
    Homme Profil pro
    Concepteur SOA
    Inscrit en
    Septembre 2005
    Messages
    217
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Deux Sèvres (Poitou Charente)

    Informations professionnelles :
    Activité : Concepteur SOA

    Informations forums :
    Inscription : Septembre 2005
    Messages : 217
    Points : 292
    Points
    292
    Par défaut
    Citation Envoyé par batmat Voir le message
    Est ce que ce code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    list_Format.setFixedCellHeight(20);
    Appliqué sur un bouton, devrai modifier la taille de ma JList ?
    (sachant qu'a la base le FixedCellHeight = 10 )

    car cela ne fonctionne pas

    Gracie mille !
    Peux tu nous fournir ton code complet ?

  8. #8
    Membre éprouvé
    Profil pro
    Développeur Back-End
    Inscrit en
    Avril 2003
    Messages
    782
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Back-End

    Informations forums :
    Inscription : Avril 2003
    Messages : 782
    Points : 935
    Points
    935
    Par défaut
    Bonjour,

    Globalement il faut faire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    javax.swing.ListModel model = new javax.swing.DefaultListModel();
    javax.swing.JList liste = new javax.swing.JList(model);
     
    //ajout d'un élément dans la liste
    ((javax.swing.DefaultListModel)liste.getModel()).addElement("item");

  9. #9
    Futur Membre du Club
    Inscrit en
    Janvier 2008
    Messages
    16
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 16
    Points : 9
    Points
    9
    Par défaut
    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
     
    /*
     * EtiquetteFrame.java
     *
     * Created on 3 avril 2008, 11:01
     */
    package paxar;
     
    import javax.swing.JOptionPane;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    /**
     *
     * @author  batmat
     */
    public class EtiquetteFrame extends javax.swing.JFrame {
     
        private DefaultListModel listModel;
     
        /** Creates new form EtiquetteFrame */
        public EtiquetteFrame() {
            initComponents();
            listModel = new DefaultListModel();
            listModel.addElement("A");
            listModel.addElement("B");
            listModel.addElement("C");
            list_Format = new JList(listModel);
     
     
            list_Format.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list_Format.setSelectedIndex(0);
            list_Format.addListSelectionListener(this);
     
        }
     
        /** 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() {
     
            jScrollPane1 = new javax.swing.JScrollPane();
            jList1 = new javax.swing.JList();
            FieldX = new javax.swing.JTextField();
            FieldY = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jScrollPane2 = new javax.swing.JScrollPane();
            list_Format = new javax.swing.JList();
            jButton2 = new javax.swing.JButton();
     
            jList1.setModel(new javax.swing.AbstractListModel() {
                String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
            jScrollPane1.setViewportView(jList1);
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jButton1.setText("Ajouter un format");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
     
            list_Format.setModel(new javax.swing.AbstractListModel() {
                String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
            list_Format.setFixedCellWidth(10);
            jScrollPane2.setViewportView(list_Format);
     
            jButton2.setText("jButton2");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(49, 49, 49)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(FieldX, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(FieldY, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(56, 56, 56))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jButton2)
                            .addGap(88, 88, 88))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addGap(65, 65, 65))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(62, 62, 62)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(FieldX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(FieldY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addComponent(jButton1)
                            .addGap(11, 11, 11)
                            .addComponent(jButton2)))
                    .addContainerGap(286, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
            String xSize = FieldX.getText();
            String ySize = FieldY.getText();
            if ((xSize.equals("") || (ySize.equals("")))) {
                JOptionPane.showMessageDialog(null, "veuillez remplir les champs");
            } else {
                // list_Format.add(tableau
                JOptionPane.showMessageDialog(null, "les champs sont remplis");
                list_Format.setVisibleRowCount(5);
                System.out.println(xSize);
         }
        // TODO add your handling code here:
        }                                        
     
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
     
     
            list_Format = new JList();
     
            list_Format.setFixedCellHeight(20);
     
     
     
     
     
        // 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() {
                    new EtiquetteFrame().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JTextField FieldX;
        private javax.swing.JTextField FieldY;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JList jList1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JList list_Format;
        // End of variables declaration
    }
    Je viens de me rendre compte de quelque chose en copiant collant mon code, toute la partie private void initComponents() { ... } est grisée, et declare pas mal de chose qui me sont impossible de modifier. Comment agir sur ces parametres ?

    Encore MERCI !!

Discussions similaires

  1. Réponses: 7
    Dernier message: 16/03/2015, 13h37
  2. WPF Bind add new item dans une DataGrid
    Par draco951 dans le forum C#
    Réponses: 12
    Dernier message: 05/02/2013, 08h06
  3. Comment récupérer le contenu d'un item dans une JList ?
    Par julie_lab dans le forum Composants
    Réponses: 3
    Dernier message: 01/11/2010, 12h25
  4. Suppression d'item dans une arborescence
    Par jonzuzu dans le forum MFC
    Réponses: 2
    Dernier message: 24/02/2004, 17h45
  5. Réponses: 2
    Dernier message: 17/08/2003, 20h07

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