I want the combobox to store the name from database at runtime,so i creted a list but combobox is displaying an error...
List<String> s = new ArrayList<String>();
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:project","sa","123456");
Statement stmt= con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT Name FROM company");
i=0;
while(rs.next()) {
s.add(rs.getString("Name"));
}
}
catch(Exception ex) { {
JOptionPane.showConfirmDialog(f,ex);
}
cb=new JComboBox(s);
}
The probable problem is that you are passing a List<String>
reference to the JComboBox
. One correct way to do this will be to convert the List<String> s
to a String[]
array and pass it to the constructor: JComboBox(E[] items)
new JComboBox(s.toArray(new String[s.size()]));
Read also How to Use Combo Boxes