質問

Here is my code that sets the values in the 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);
    } 

}

Here is where I call the method and set the combo box with the values:

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



}

Now the problem is I am getting this in my combo box [Travel Standard, Fixed Standard] exactly as this when I click on my combo box.

Though I want it them to be split apart in their own index without the "[" and "]".

役に立ちましたか?

解決

cbox.addItem(strings); is adding the List as single element to the JComboBox.

As you have previously been advised, you should add the values to a ComboBoxModel, for example...

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);

Take a look at How to Use Combo Boxes for more details

You may also want to take a look at Using Prepared Statements as you are not using them properly...which was also highlighted in your previous question...

As you have previously been advised, you should be closing your resources to the database when you are done with them, failing to do so could impare you databases performance...

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) {
    }
}

Or if you're using 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);
}

他のヒント

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

You're only adding one item the combo box, which is the List strings. A List as a string will be [something, something, something] as you are experiencing.

You need to add the items individually, preferably using the the model of the combobox (as MadProgrammer points out in his answer, and I also pointed out in your last post) and not the combo box directly.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top