3
我目前正在使用查询填充jcombobox的Java编写程序。我想知道在程序执行时是否有一种方法来获得默认的选定值。我的查询是按字母顺序列出的语言列表,但我很好奇,如果可以将英语(位于列表中间)作为默认值。在由查询填充的jcombobox中设置默认值
我知道,当你手动硬编码值到JComboBox中你可以设置默认的变量
jcombobox.setSelectedIndex(int anIndex);
或
jcombobox.setSelectedItem(Object anObject);
,但我不确定当一个ResultSet循环和填充JComboBox中。
目前我的代码是:
languageLabel =new JLabel("Languages:");
rowFour.add(languageLabel,BorderLayout.WEST);//adding to my current panel
langbox = new JComboBox();
rowFour.add(langbox,BorderLayout.WEST);
try
{
con = DriverManager.getConnection ("jdbc:oracle:thin:@localHost:portNumber:ORCL", "username", "password");
statement = con.createStatement();
}
catch(SQLException sqle)
{
System.out.println(sqle);
}
langbox.removeAllItems();
langbox.addItem("Please Select...");
try
{
ResultSet rs = statement.executeQuery("select language from language order by 1");
while (rs.next())
{
langbox.addItem(rs.getString(1));
//Thinking that this is where a default value would be located
}
}
catch(Exception e)
{
System.err.println(e);
}
谢谢您的时间。
非常感谢! – SamiSunshine 2012-08-01 15:31:14