Код: Выделить всё
public static void setupID_TITLE_ComboBox(JComboBox jComboBox, String tableName) throws SQLException {
// query to select ID and TITLE from profiles table
String query = "SELECT ID,TITRE FROM " + tableName + ";";
ResultSet myJComboResultSet = SQLTools.ExecuteQuery(query);
ArrayList visualItems = new ArrayList(); // the Items of my combobox [item1,item2]
ArrayList idItems = new ArrayList(); // the corresponding IDs for each item [id1,id2]
while (myJComboResultSet.next()) {
visualItems.add(myJComboResultSet.getObject("TITRE")); // filling items set
idItems.add(myJComboResultSet.getObject("ID")); // filling IDs set
}
System.out.println("IDItems=" + idItems); // checking that my Items are filled
System.out.println("visualItems=" + visualItems); // checking that my IDs are filled
// creating a combobox of previous items
jComboBox = new JComboBox(visualItems.toArray()) {
ArrayList values = idItems;
// overriding the getSelectedItem to return the corresponding selected item's ID
@Override
public Object getSelectedItem() {
Object get = values.get(super.getSelectedIndex());
System.out.println("get = " + get);
return get;
}
};
}
Код: Выделить всё
JComboBoxTools.setupID_TITLE_ComboBox(J_Users_Profile,"profiles");
вывод:
visualItems=[Admin, Учитель, Студент]
IDItems=[0,3,5]
выбранное возвращаемое значение элемента: Учитель
Не знаю, что делать. Я хочу, чтобы он возвращал 3 — идентификатор учителя.
полный проект находится по этой ссылке.
спасибо.
Подробнее здесь: https://stackoverflow.com/questions/408 ... ot-working
Мобильная версия