如何在JComboBox中细分个别值?

时间:2014-03-20 00:45:34

标签: java sql swing jcombobox

以下是我的代码,用于设置JComboBox

中的值
 public void setDiscountNames(String type, JComboBox cbox) {

    cbox.removeAllItems();

    ArrayList<Discount> names = new ArrayList<Discount>();
    try {        
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/abpp034?user=abpp034&password=120001772");



       stmt = con.prepareStatement("SELECT Name FROM Discount WHERE Type = \"" + type + "\"");
       rs = stmt.executeQuery();



       List<String> strings = new ArrayList<String>();
        while(rs.next()){

        strings.add(rs.getString("Name"));  // Confirm if "Name" is valid


}
        cbox.addItem(strings);

    } catch (SQLException ex) {
        Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
    } 

}

这是我调用方法的地方,并使用值设置组合框:

   public DiscountGUIView() {
    initComponents();
    model.setDiscountNames("Fixed", jComboBox1);



}

现在的问题是,当我点击我的组合框时,我在我的组合框[Travel Standard, Fixed Standard]中得到了这个。

虽然我希望它们在没有&#34; [&#34;和&#34;]&#34;。

2 个答案:

答案 0 :(得分:3)

cbox.addItem(strings);正在将List作为单个元素添加到JComboBox

如果您有previously been advised,则应将值添加到ComboBoxModel,例如......

DefaultComboBoxModel model = new DefaultComboBoxModel();
try {        
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/abpp034?user=abpp034&password=120001772");

    stmt = con.prepareStatement("SELECT Name FROM Discount WHERE Type = \"" + type + "\"");
    rs = stmt.executeQuery();

    while(rs.next()){
        model.addElement(rs.getString("Name"));
    }

} catch (SQLException ex) {
    Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
} 
cbox.setModel(model);

请查看How to Use Combo Boxes了解详情

您可能还想查看Using Prepared Statements,因为您没有正确使用它们......这也在您之前的问题中突出显示...

正如您之前所知,当您完成数据库时,您应该将资源关闭到数据库,否则可能会影响您的数据库性能......

Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {        
    //...
} catch (SQLException ex) {
    Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
} finally {
    try {
        rs.close();
    } catch (Exception exp) {
    }
    try {
        stmt.close();
    } catch (Exception exp) {
    }
    try {
        con.close();
    } catch (Exception exp) {
    }
}

或者如果你正在使用Java 7

try (Connection con =  DriverManager.getConnection("jdbc:mysql://localhost:3306/abpp034?user=abpp034&password=120001772")){        
    try (PreparedStatement stmt = con.prepareStatement("SELECT Name FROM Discount WHERE Type = \"" + type + "\"")) {
        try (ResultSet rs = stmt.executeQuery) {
            //...
        } catch (SQLException exp) {
            Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (SQLException exp) {
        Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
    }
} catch (SQLException ex) {
    Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
}

答案 1 :(得分:2)

 List<String> strings = new ArrayList<String>();
 while(rs.next()){
     strings.add(rs.getString("Name"));  // Confirm if "Name" is valid
 }
 cbox.addItem(strings);

您只是在组合框中添加一个项,即List strings。正如您所见,作为字符串的列表将<{em>}为[something, something, something]

您需要单独添加项目,最好使用组合框的模型(正如MadProgrammer在答案中指出的那样,我也在your last post中指出)而不是直接组合框。