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 :

Remplir une jComboBox et affecter des couleurs


Sujet :

Composants Java

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 22
    Points : 17
    Points
    17
    Par défaut Remplir une jComboBox et affecter des couleurs
    Bonjour,

    J'ai réussi à remplir une jComboBox avec une boucle depuis un tableau. Mais à chaque fois que je fais un "jComboBox.additem()", je voudrai en même temps lui affecter une couleur.

    Après de nombreuses lectures, dont sur ce forum, j'ai pu trouvé que ce nétait pas native et qu'il fallait utiliser "listcellrenderer " mais malgrés les exemples. Je n'arrive pas à les adapter pour moi même.

    Dans ces exemple, le plus compréhensible mais pas assez a été celui ci :

    http://imss-www.upmf-grenoble.fr/pre...ing/JList.html

    Voici un extrait de mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    else if ( PathFile[i].isDirectory() && PathFile[i].toString().substring(PathFile[i].toString().lastIndexOf("\\")+1, PathFile[i].toString().lastIndexOf("\\")+11).equals(operation.toString().substring(0,10)) ){
                           jComboBox3.addItem(operation.toString());
                           // Setbackground color.orange
                           //System.out.println("orange");
                           i = PathFile.length+1;
     
                        }

  2. #2
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Il faut que tu crée un objet contenant ton operation et la couleur associée.

    Puis que tu crées un ListCellRenderer qui applique la couleur que contient cet objet (setOpaque(true) et setBackground) et fait un setText à partir de l'operation contenue dans le même objet.

    Pour créer le ListCellRenderer: http://docs.oracle.com/javase/tutori....html#renderer
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 22
    Points : 17
    Points
    17
    Par défaut
    J'ai du mal à me retrouver dans cet doc car j'utilise le logiciel Netbeans et ma jComboBox est déjà créée et il instancie des paramètres pour la création de sa fenêtre, que j'ai déjà.
    Faut-il obligatoirement créer la jComboBox avec ça ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    JComboBox petList = new JComboBox(intArray);
    Et faut il obligatoirement créer une classe ListCellrender ?
    Appel: ComboBoxRenderer renderer= new ComboBoxRenderer();
    Cela :
    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
    class ComboBoxRenderer extends JLabel
                               implements ListCellRenderer {
            private Font uhOhFont;
     
            public ComboBoxRenderer() {
                setOpaque(true);
                setHorizontalAlignment(CENTER);
                setVerticalAlignment(CENTER);
            }
     
            /*
             * This method finds the image and text corresponding
             * to the selected value and returns the label, set up
             * to display the text and image.
             */
            public Component getListCellRendererComponent(
                                               JList list,
                                               Object value,
                                               int index,
                                               boolean isSelected,
                                               boolean cellHasFocus) {
                //Get the selected index. (The index param isn't
                //always valid, so just use the value.)
                int selectedIndex = ((Integer)value).intValue();
     
                if (isSelected) {
                    setBackground(list.getSelectionBackground());
                    setForeground(list.getSelectionForeground());
                } else {
                    setBackground(list.getBackground());
                    setForeground(list.getForeground());
                }
     
                //Set the icon and text.  If icon was null, say so.
                ImageIcon icon = images[selectedIndex];
                String pet = petStrings[selectedIndex];
                setIcon(icon);
                if (icon != null) {
                    setText(pet);
                    setFont(list.getFont());
                } else {
                    setUhOhText(pet + " (no image available)",
                                list.getFont());
                }
     
                return this;
            }

    Voici la class existante qui rempli déjà ma jCOmboBox grâce à la table Mysql et en plus, compare si la BDD dit vrai. Si le chemin du dossier est bien existant.
    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
     public void RefOpe() {
            Conf.Path = "R:"+File.separator+"Fichiers"+File.separator+jComboBox1.getSelectedItem().toString();
            //System.out.println(Conf.Path);
            int i;
            File[] PathFile= new File(Conf.Path).listFiles();
     
            //jComboBox3.setRenderer(PathFile.length); // c'est l'initialisatrion du composant ?
     
            int idClient1 = id_Client(jComboBox1.getSelectedItem().toString());
                mainQuery = test_reception_fichierPUEntityManager.createNamedQuery("Main.findByIdClientM");
                mainQuery.setParameter("idClientM", idClient1);
                jComboBox3.removeAllItems();
                for (Object Ope : mainQuery.getResultList()) {
                    Main operation = (Main)Ope;
     
                    for (i=0; i < PathFile.length; i++) {
    //                    System.out.println(operation.toString()+" = "+PathFile[i].toString().substring(PathFile[i].toString().lastIndexOf("\\")+1));
                        //System.out.println(operation.toString()+" - "+PathFile[i].toString());
                        //System.out.println(PathFile[i].toString().substring(PathFile[i].toString().lastIndexOf("\\")+1, PathFile[i].toString().lastIndexOf("\\")+11).equals(operation.toString().substring(0,10))+" "+operation.toString().substring(0,10)+" / "+PathFile[i].toString().substring(PathFile[i].toString().lastIndexOf("\\")+1, PathFile[i].toString().lastIndexOf("\\")+11));
                        if ( PathFile[i].isDirectory() && PathFile[i].toString().substring(PathFile[i].toString().lastIndexOf("\\")+1).equals(operation.toString()) ){
                            jComboBox3.addItem(operation.toString());
                            // Background Vert = Strictement identique
                            i = PathFile.length+1;
     
                        }
                        else if ( PathFile[i].isDirectory() && PathFile[i].toString().substring(PathFile[i].toString().lastIndexOf("\\")+1, PathFile[i].toString().lastIndexOf("\\")+11).equals(operation.toString().substring(0,10)) ){
                           jComboBox3.addItem(operation.toString());
                           // Background Orange = Que le début qui est identique
                           i = PathFile.length+1;
                        }
     
                    }
     
                    if ( PathFile.length == i){
                        jComboBox3.addItem(operation.toString()); 
                        // Background Rouge = Dossier non présent.
                    }
                } 
        }

Discussions similaires

  1. Remplir une jcombobox
    Par philou029 dans le forum Débuter
    Réponses: 3
    Dernier message: 03/04/2008, 05h16
  2. Affecter des couleurs à un nuage de points 3D
    Par LeRocher dans le forum MATLAB
    Réponses: 2
    Dernier message: 21/12/2007, 11h47
  3. Remplir une JComboBox à l'aide d'une requête ?
    Par spl0tch dans le forum AWT/Swing
    Réponses: 9
    Dernier message: 10/10/2007, 21h57
  4. Réponses: 1
    Dernier message: 21/04/2006, 17h25
  5. remplir une table en fonction des résultats
    Par Psychomantis dans le forum SQL Procédural
    Réponses: 5
    Dernier message: 19/10/2004, 12h22

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