ValueMember and DisplayMember in Java ComboBox?

2

I have a JComboBox in Java, where I show a list of employees, result of a Query, I store it in a ResultSet and I want it to be that when it is selected, I take the ID and not the name, unlike C # , Java does not have a tool like DisplayMember or ValueMember that allows me to easily display a text and when selecting it to take the value, the method where I assign the resultSet to my combo is as follows:

while(rs.next())
            {
                fila = new Object[colNo];
                for(int i=0;i<2;i++)
                {
                    fila[i] = rs.getObject(i+1);
                    employeesCombo.addItem(fila[i]);
                }   
            }

My question is, what is the easiest way to do it? Since if it shows the data, but not in the way I want, I'm new to Java, so I'm not that familiar. This is the way they are displayed, first show the ID and then the name of the employee:

    
asked by Kevin M. 22.04.2016 в 23:40
source

2 answers

1

Even though you're going in the right direction, I'll add some things to you:

In java all objects extend from Object but there are primitive types, and their wraper classes, so it is a little unnatural to create a class as you have done, more natural would be something like this:

public class Employee {
  public int id;
  public String name;

  public Employee(int id,String name) {
    this.id=id;
    this.name=name;
  }

  public int getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  public void setId(int id) {
    this.id=id;
  }

  public void setName(String name) {
    this.name=name;
  }

  //Esto es lo que hace que el JComboBox se visualize correctamente
  @Override
  public String toString() {
    return id + " " + name;
  } 

}

This allows you to have a class used with methods to access its attributes. Basically it is the class that maps the result of your ResultSet

Recover the ResultSet and use the specific methods it has depending on the type to obtain the values you need to create used objects:

while(rs.next()) {
  Employee empleado=new Employee(rs.getInt(1),rs.getString(2));
  employeesCombo.addItem(empleado);
}

I hope it helps you.

    
answered by 23.04.2016 / 01:30
source
0

I answer my own question, what I did in the end was to create a class, where I add the data, row by row of the Object that has all the results, I take the real value and the String method returns the value to show:

public class DisplayEmployeeList 
{
    public Object displayMember;
    public Object valueMember;

    public DisplayEmployeeList(Object display,Object value)
    {
        displayMember=display;
        valueMember=value;
    }

    public String toString()
    {
        return displayMember.toString();
    }
}

And finally, in the main method where I add each value, I add them to the object that I think of the class during a while, and add the item to the JCombo.

while(rs.next())
            {
                fila = new Object[colNo];
                for(int i=0;i<2;i++)
                {
                    fila[i] = rs.getObject(i+1);
                }
                emp = new DisplayEmployeeList(fila[1], fila[0]);
                employeesCombo.addItem(emp);
            }
            con.close(); 
    
answered by 23.04.2016 в 00:31