Code Hungry
Code Hungry

Reputation: 4000

How to fill Combo box from JAVADB

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() );}      `

Upvotes: 0

Views: 1765

Answers (2)

user743234
user743234

Reputation:

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.

Upvotes: 0

npinti
npinti

Reputation: 52185

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

Upvotes: 1

Related Questions