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 :

Comment rafraîchir mon JComboBox ?


Sujet :

avec Java

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2015
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Février 2015
    Messages : 17
    Points : 13
    Points
    13
    Par défaut Comment rafraîchir mon JComboBox ?
    Bonjour, j'ai vue qu'il y avait beaucoup de posts sur ce sujet mais aucun ne règle mon problème, (je viens de débuter en java).
    J'aimerai rafraîchir le contenue de ma JComboBox en fonction d'un choix d'une JCheckBox. j'ai essayer avec repaint() ou revalidate() ou updateUI() ou removeAllItems() mais rien ne marche, ma JCombobox ce vide mais n’affiche pas les nouvelles valeur. Pouvez vous m'aider merci .
    La classe :
    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
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
     
    public class Structure extends JFrame {
    	  JPanel content = new JPanel();
    	  JCheckBox prot_structure = new JCheckBox("protéine");
    	  JCheckBox adn_structure = new JCheckBox("ADN");
    	  String[] tab_matrice;
    	  JComboBox c_matrice = new JComboBox();
     
     
    	public Structure(){
    		// Création de la fenetre
    	    this.setTitle("t");
    	    this.setSize(900, 600);
    	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    this.setLocationRelativeTo(null);
     
     
    //JCheckBox--------------------------------------------------------------------------
    		JPanel f_structure = new JPanel();
    		JLabel m_structure = new JLabel("Type de structure :");  
    	    adn_structure = new JCheckBox("ADN");
    	    prot_structure = new JCheckBox("protéine");
    	    f_structure.add(m_structure);
    	    f_structure.add(adn_structure);
    	    f_structure.add(prot_structure);
    	    content.add(f_structure);
     
    	    //Définition de l'action
    	    adn_structure.addActionListener(new ActionListener(){
    	      public void actionPerformed(ActionEvent e){
    	    	  if (((JCheckBox)e.getSource()).isSelected() == true){
    	    		  c_matrice.removeAllItems();
    	    		  tab_matrice = new String[] {"unitaire", "transition//transvertion"};
    	    		  c_matrice = new JComboBox(tab_matrice);
    	    		  c_matrice.setSelectedIndex(0);
    	    		  c_matrice.repaint();  
    	    		  c_matrice.revalidate();
    	    		  c_matrice.updateUI();
    	    		  prot_structure.setSelected(false);
    	    	  }
    	      }
    	    });
    	    //Définition de l'action
    	    prot_structure.addActionListener(new ActionListener(){
    	      public void actionPerformed(ActionEvent e){
    	    	  if (((JCheckBox)e.getSource()).isSelected() == true){
    	    		  c_matrice.removeAllItems();
    	    		  tab_matrice = new String[] {"BLOSUM45", "BLOSUM50", "BLOSUM62", "BLOSUM80", "BLOSUM90", "PAM30", "PAM70", "PAM250"};
    	    		  c_matrice = new JComboBox(tab_matrice);
    	    		  c_matrice.setSelectedIndex(2);
    	    		  c_matrice.repaint();
    	    		  c_matrice.revalidate();
    	    		  c_matrice.updateUI();
    	    		  adn_structure.setSelected(false);
    	    	  }
    	      }
    	    });
     
    //JComboBox--------------------------------------------------------------------------	    
    	    JPanel f_matrice = new JPanel();
    	    JLabel m_matrice = new JLabel("Choix de la matrice :"); 
    	    f_matrice.add(m_matrice);
    	    f_matrice.add(c_matrice);
    	    content.add(f_matrice);
     
    	    this.getContentPane().add(content);
    	    this.setVisible(true);
    	}
    }
    Le main :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    public class test {
    	public static void main(String[] args) {
     
    		Structure fen = new Structure();	
    	}
    }

  2. #2
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Salut,

    En faisant c_matrice = new JComboBox(tab_matrice); dans les écouteurs des checkbox, tu crées simplement une nouvelle combobox : la variable c_matrice pointe bien dessus, mais celle qui est affichée dans la fenêtre est toujours celle que tu as mise par f_matrice.add(c_matrice); à la création de la fenêtre.

    Au lieu de recréer une nouvelle JComboBox, il suffit de remplacer le contenu de la première, en supprimant les items déjà présents (ce que tu fais déjà), puis en ajoutant un à un les nouveaux (une autre solution consisterait à remplacer le modèle complètement, par c_matrice.setModel(...), ce qui est utile avec un modèle beaucoup plus élaboré)) :

    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
    //Définition de l'action
    	    adn_structure.addActionListener(new ActionListener(){
    	      public void actionPerformed(ActionEvent e){
    	    	  if (((JCheckBox)e.getSource()).isSelected() == true){
    	    		  c_matrice.removeAllItems();
    	    		  tab_matrice = new String[] {"unitaire", "transition//transvertion"};
     
    	    		  for(String matrice : tab_matrice) { // parcourir les nouveaux items
    	    			  c_matrice.addItem(matrice); // et les ajouter un à un
    	    		  }
    	    		  c_matrice.setSelectedIndex(0);
     
    	    		  prot_structure.setSelected(false);
    	    	  }
    	      }
    	    });

    A noter que tu as intérêt à paramétrer ta JComboBox, en indiquant le type des items que tu mets dedans :
    JComboBox<String> c_matrice = new JComboBox<>();

  3. #3
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2015
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Février 2015
    Messages : 17
    Points : 13
    Points
    13
    Par défaut
    Super j'ai compris merci beaucoup !

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

Discussions similaires

  1. comment changé mon ip sous dos
    Par maichants20 dans le forum Windows 2000/Me/98/95
    Réponses: 2
    Dernier message: 14/07/2005, 13h25
  2. Réponses: 1
    Dernier message: 24/01/2005, 06h55
  3. [Servlet] Comment référencer mon fichier CSS
    Par fytheone dans le forum Servlets/JSP
    Réponses: 3
    Dernier message: 07/01/2005, 09h58
  4. Comment faire mon choix
    Par SoubeigAbraham dans le forum Débuter
    Réponses: 1
    Dernier message: 07/10/2004, 14h29
  5. Réponses: 7
    Dernier message: 04/06/2004, 15h20

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