Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2536 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  JCombobox : comment remettre la liste à 0?

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

JCombobox : comment remettre la liste à 0?

n°2218855
elyenko
Posté le 06-02-2014 à 21:41:44  profilanswer
 

J'ai un problème avec une Jcombobox et la fonction .removeAllItems.
 
En fait, j'ai deux JCombobox, une appelée "combo" qui contient des ingrédients et une seconde appelée "combo_unit" qui contient des unités.
La deuxième doit se mettre à jour en fonction de ce qui est sélectionné dans la première.  
Ex:  
--> si je sélectionne "carottes", combo_unit se remplit avec "gramme" et "kg".
--> si je sélectionne "farine", combo_unit se remplit avec "cuillère à soupe" et "gramme".
etc.
 
Pour mettre tout ça en place, j'ai créé une méthode Maj_combo_unit :
 
 

Code :
  1. ArrayList<String> i_Units = new ArrayList<String>();
  2. (...)
  3. public void Maj_combo_unit(){
  4.     combo_unit.removeAllItems();
  5.     i_Units = Get_Ingredient().to_Unit_List();  ==> Remplit la liste i_Units avec les unités correspondant à l'ingrédient sélectionné
  6.     for (int k =0 ; k<i_Units.size() ; k++){
  7.         combo_unit.addItem(i_Units.get(k));}  ==> Charge la liste i_Units dans "combo_unit"
  8. }


 
Celle-ci doit actualiser la deuxième liste déroulante : à chaque appel de la première JCombobox, la méthode Maj_combo_unit se lance.
 
Le problème c'est que depuis que j'ai ajouté la fonction combo_unit.removeAllItems(), j'ai ceci :
 
Citation:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Ajouter2$ComboUnitListener.actionPerformed(Ajouter2.java:120)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.JComboBox.intervalRemoved(Unknown Source)
at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
at javax.swing.DefaultComboBoxModel.removeAllElements(Unknown Source)
at javax.swing.JComboBox.removeAllItems(Unknown Source)
at Ajouter2.Maj_combo_unit(Ajouter2.java:102)
at Ajouter2$ComboListener.actionPerformed(Ajouter2.java:113)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
 
Désolé la liste des erreurs est longue... :/
Quelqu'un a une idée d'où vient le problème?  
Je ne comprends pas comment remettre ma liste déroulante à 0 pour charger les nouvelles unités.

Message cité 1 fois
Message édité par elyenko le 06-02-2014 à 21:42:43
mood
Publicité
Posté le 06-02-2014 à 21:41:44  profilanswer
 

n°2218872
honrisse
Posté le 07-02-2014 à 10:29:09  profilanswer
 

elyenko a écrit :


Citation:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Ajouter2$ComboUnitListener.actionPerformed(Ajouter2.java:120)
 
Quelqu'un a une idée d'où vient le problème?  
Je ne comprends pas comment remettre ma liste déroulante à 0 pour charger les nouvelles unités.


 
Difficile de dire sans plus d'infos sur la ligne 120.
Pour mettre à 0 removeAllItems marche. Par exemple :

Code :
  1. import java.awt.Dimension;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JComboBox;
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.SwingUtilities;
  8. /**
  9. *  
  10. * @see http://forum.hardware.fr/hfr/Progr [...] 1324_1.htm
  11. * @see http://docs.oracle.com/javase/tuto [...] bobox.html
  12. *
  13. */
  14. public class Main {
  15. public static void createAndShowGui() {
  16.  final JFrame frame = new JFrame("JComboBox" );
  17.  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.  final String[] list1 = new String[]{"Carotte", "Farine"};
  19.  final String[][] list2 = new String[][]{{"Gramme", "Kg"}, {"Cuillère à soupe", "Kg"}};
  20.  JPanel panel = new JPanel();
  21.  final JComboBox<String> box1 = new JComboBox<>(list1);
  22.  final JComboBox<String> box2 = new JComboBox<>(list2[0]);
  23.  box1.addActionListener(new ActionListener() {
  24.   @Override
  25.   public void actionPerformed(ActionEvent arg0) {
  26.    box2.removeAllItems();
  27.    int indexSelected = box1.getSelectedIndex();
  28.    for(String s : list2[indexSelected]) {
  29.     box2.addItem(s);
  30.    }
  31.   }
  32.  });
  33.  panel.add(box1);
  34.  panel.add(box2);
  35.  frame.add(panel);
  36.  frame.setMinimumSize(new Dimension(250, 100));
  37.  frame.setLocationRelativeTo(null);
  38.  frame.setVisible(true);
  39. }
  40. public static void main(String[] args) {
  41.  SwingUtilities.invokeLater(new Runnable() {
  42.   @Override
  43.   public void run() {
  44.    createAndShowGui();
  45.   }
  46.  });
  47. }
  48. }


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  JCombobox : comment remettre la liste à 0?

 

Sujets relatifs
Retirer une personne d'une liste de diffusion avant envoiProblème création liste par VBA
[Rés]Incrémentation, redimenssionnement, valeur d'une liste modifiableCopier une partie d'une liste doublement chaînée
Quoi modifier du PHP.INI pour remettre mon blog sur piedListe de puces sur un blog : personnalisation
Garder en mémoire une liste déroulante[Résolu] Détecter le sens de parcours d'une liste de coordonnées
Afficher une Liste.Problème Scrit qui liste les fichiers
Plus de sujets relatifs à : JCombobox : comment remettre la liste à 0?


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR