Problem with combobox and duplicate items

1

I have a combobox that I fill it with data from mysql , the problem that I currently have is that the combobox doubles the values, for example, if I add 5 items, it will show me 5 and 5 others that are copies of the first, I leave the code and a sample photo:

public void docenteAlumno(String seccion,JComboBox cb,JComboBox cb2,DocenteMenu estado){
         ArrayList <String> usuarios= new ArrayList();
         Connection con= new AbrirBasedeDatos().conectar();
         int yy=cb.getItemCount();
         String xx=String.valueOf(yy);
         if(xx!=null && !xx.isEmpty()){
            cb.removeAll();
         }
         usuarios.clear();
         cb.addItem("Seleccione Alumno");


          try {
           if(con!=null){
             Statement st = con.createStatement();
             String query ="select * from inasistencia where seccion = '"+seccion+"'and leida=false and justificado='si'";

             ResultSet rs = st.executeQuery(query);

             while(rs.next()){
                 usuarios.add(rs.getString("user"));


             }
             removeDuplicates(usuarios);



             for(int i=0;i<usuarios.size();i++){
                 String query2="select * from usuario where user='"+usuarios.get(i)+"'";

                 ResultSet rss=st.executeQuery(query2);
                 while(rss.next()){

                     cb.addItem(rss.getString("nombre"));
                 }
             }
            cb2.setEnabled(true);
           }else{
               JOptionPane.showMessageDialog(estado,"Asegurate de estar conectado a internet");
           }
          }catch (SQLException sqle){
              System.out.println("el error fue: "+sqle.getMessage());
              JOptionPane.showMessageDialog(estado,"Ocurrio un error, vuelve a intentarlo más tarde");
          }
       new AbrirBasedeDatos().cerrarConexion();





    }

    private void removeDuplicates(ArrayList<String> list){
    int index = 0;
    int count = 0;

    while (index < list.size() - 1) {
        String item = list.get(index);
       List<String> tail = list.subList(index + 1, list.size());
        while (tail.equals(item)) {
            tail.remove(item);
            count++;
        }
        index++;
    }

       }

that's what it looks like now:

I hope you can help me

    
asked by zhet 27.11.2017 в 06:22
source

2 answers

1

At the request of @gbianchi, it seems that the problem is generated by the itemStateChanged , since this event is called twice as normal.

The first time when the selected item ceases to be (ie, its status changes to deselected ) and the second time when the new item is selected (goes from unselected to selected ).

As I mentioned in the comment:

  

The ItemStateChanged starts working   incorrectly   when you leave one item and place another.

     

Do not use an ItemStateChanged,   instead of this, it uses an ActionListener which is good for   manipulate events of a ComboBox.

     

You can use the ItemStateListener   if apart you need to check when the item is selected and   then stop being.

     

Changing the status of the item causes this   a redial to itemStateChanged

So, it's better to use the ActionPerformed from ActionListener from JComboBox .

If by force you must use the ItemStateChanged then, check with if if the status of the item is "selected", so you get the item and it will not happen twice, the status of the item you get with e.getStateChanged();

    
answered by 27.11.2017 / 07:09
source
1

I do it this way first I create a resultset type method in the data access layer.

That's where I execute the sentence sql

select distic nombre from grupo

And then in the form of the view I create a method to load the data and I start it in the constructor method of the class.

//metodo contructor


public IfrmSalidaEG() {
    initComponents();
    cargarGrupo();

}

//este el metodo para cargar en el combo

 private void cargarGrupo(){
      ResultSet rs;    
         try{ 
              rs=fun.cargarGrupo();

            while(rs.next()){
                rs.getString("Gru_Sal_Id");
                        cboGrupo.addItem(rs.getString("Gru_Sal_Id"));   

            }
         }catch(Exception e){

         }
     }
    
answered by 21.09.2018 в 16:45