Combobox with selected element based on an identifier

0

I have the following Form:

What I want you to help me is that when you put a District Id in the text box, in the comboBox you select the district corresponding to the id entered

Here I leave my code

public class daoempleado{
conexion.conecta cone=new conexion.conecta();
CallableStatement cst=null;
ResultSet rs=null; 
public DefaultComboBoxModel valorsito(){
  DefaultComboBoxModel valor=null;
   try {           
      cst=cone.xconecta().prepareCall("select * from distrito");
      rs=cst.executeQuery(); 
      valor=new DefaultComboBoxModel();

      while(rs.next())
      {
          valor.addElement(new   beanDistrito(rs.getString(2).trim(),rs.getString(1)));           
      }

      return valor;
   } catch (SQLException e) {
       JOptionPane.showMessageDialog(null, e.toString());
       return null;
   }
  }
}
  • This is my BeanDistrict class

    public class beanDistrito {
    private String nombre;
    private String id;
    
    public String getId(){
      return id;
         }
    
    public String toString(){
      return nombre;
        }
    
    public beanDistrito() {
      }
    
    public beanDistrito(String nombre, String id) {
       this.nombre = nombre;
       this.id = id;
       } 
        }
    
  • Here I call it on my form after the initcomponents

     private void ListarDistritos() {
         cbo1.setModel(d_emp.valorsito());
        }
    
  • In this way

         public NewJDialog(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
            ListarDistritos();   
           }
    
asked by Jcastillo 05.07.2017 в 23:32
source

3 answers

1

If it is by clicking on the search button, I would recommend you to collect the textField content and select the value of the comboBox by comparing the id.  An example would be

private void buscar() {
    for (int i = 0; i<comboBox.getItemCount(); i++){
        if (persona.getId().equals(textField.getText()))            
          comboBox.setSelectedItem(persona);
    }
}

If it is not by pressing the button you could use a textField listener that changes the value of the comboBox using the same method.

Greetings and luck.

    
answered by 06.07.2017 в 11:34
0

Do you need the code to capture the value? Or you already have it but it does not bring you the id? Because I see that you load the comboBox, but you would need an event, for example 'onclick' for when you select the selected index ... something else in this line cst = cone.xconecta (). PrepareCall ("select * from district "); you should modify it and order it by the id so that when you select the first result it is 1

    
answered by 06.07.2017 в 01:58
0

When you press the search button, checking that you enter a correct value (neither empty space nor a letter ...) you have to retrieve the list of items contained in the combobox. Once you have the items, you only have to compare the number entered in the text box and if you find it among the items mark it as selected with the setSelectedItem of the defaultcombobox.

To select the item, I think you could do it like this (I'm not 100% sure that I'll go because I do not usually work with this but it would be something like that):

for(int i=0; i<rs.length; i++)
{
    if((beanDistrito)valor.getElementAt(i).getID()==valor_de_textbox)
        valor.setSelectedItem((beanDistrito)valor.getElementAt(i));
}
    
answered by 06.07.2017 в 11:00