Ti conviene impacchettare tutto in una classe, in cui avrai un metodo getID... ti faccio un esempio banale
codice:
import javax.swing.*;
import java.awt.event.*;
/**
*
* @author Andrea
*/
public class JPersonalComboBox extends JFrame {
private class Persona {
private int id;
private String name,surname;
public Persona(String name, String surname, int id) {
this.name = name;
this.surname = surname;
this.id = id;
}
public Persona() {
this("John", "Doe", 0);
}
public String toString() {
return this.name+ " " + this.surname;
}
public String getName() {
return this.name;
}
public String getSurname() {
return this.surname;
}
public int getID() {
return this.id;
}
public void setName(String name) {
this.name = name;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void setID(int id) {
this.id = id;
}
}
/** Creates a new instance of JPersonalComboBox */
private JComboBox mycombo;
private Persona[] persona;
private String[] names = {"Andrea", "Francesca", "Paolo", "Ermenegilda", "Brandimarte"};
private String[] surnames = {"Ariosto", "Rossi", "Verdi", "Bianchi", "Bramante"};
private int[] ids = {5, 100, 2, 12, 14};
public JPersonalComboBox() {
super("ComboBox Demo");
this.setSize(400,400);
persona = new Persona[names.length];
for (int i = 0; i < persona.length; i++) {
persona[i] = new Persona(names[i], surnames[i], ids[i]);
}
mycombo = new JComboBox(persona);
this.getContentPane().add(mycombo);
mycombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println(((Persona)mycombo.getSelectedItem()).getID());
}
});
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args) {
new JPersonalComboBox();
}
}