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