0

I am using Java Netbeans 6.9.1. I have an Table Called Workers in JavaDB. I want to Display Names of Workers in to Combo box. I am using Combobox on JinternalFrame.

Thanks in advance..

try{
String host="jdbc:derby://localhost:1527/Employees";
String uName="admin";
String uPass="admin";
con=DriverManager.getConnection(host, uName, uPass);
stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
String sql= "SELECT FIRST_NAME FROM APP.Workers";
rs=stmt.executeQuery(sql);
 while (rs.next()) {
String s = rs.getString("FIRST_NAME");
jComboBox1.addItem(s.trim());  } }catch (SQLException err) { System.out.println(err.getMessage() );}      `
Code Hungry
  • 3,930
  • 22
  • 67
  • 95

2 Answers2

1

Have you tried looking at this previous SO post and this other forum post?

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
0

You can populate a swing's JComboBox using setModel() method. This outlines what you should have:

String[] list = new String[10]; // for example
int count = 0;
while (rs.next()) {
    list[count] = rs.getString("FIRST_NAME");
    count++;
}
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(list));

setModel() has one parameter which is a DefaultListComboBoxModel object, and this object is initialized using an array of strings as a list of models.