-4

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);
        }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Paras Dhawan
  • 364
  • 8
  • 23
  • "is displaying an error" is never enough information. Please read http://tinyurl.com/so-list – Jon Skeet Jul 27 '13 at 08:03
  • 30 seconds reading the [JavaDocs](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html) would have at least told you why you had an error – MadProgrammer Jul 27 '13 at 08:09
  • @JonSkeet Yes you are right , but I saw his profile and he hasn't accepted any answers yet , even though they were helpful ! – AllTooSir Jul 27 '13 at 08:28

1 Answers1

5

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

AllTooSir
  • 48,828
  • 16
  • 130
  • 164