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