Hi,
I am new to java.Can u Plz help me.I have a JcomboBox. I want to display all the ItemId from the database in that JComboBox. I don't know how to do that.Its Displaying only 1 item.ie.first record.Can any one plz help me.

The code is:-

public String  selectItemID(){
    String itemNo = null;

    try {
        statement = dbConnection.prepareStatement("select ItemId from ItemDetails");
         ResultSet itemNumberResult = statement.executeQuery();
   while(itemNumberResult.next()) 
         {
             itemNo = itemNumberResult.getString(1);
             System.out.println("itemNo  " +itemNo);
              //itemNumber = ArrayList.class. 
             //System.out.println(" itemNumber = " + itemNumber);
              return  itemNo ;
         }
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return itemNo;

}

DbActivity dbconnection = new DbActivity();

 String  result= dbconnection.selectItemID();
  itemno.addItem(result);

Dani AI

Generated

was right about the immediate bug: the early return inside the loop terminates after the first row. After you removed that, the reason you're still seeing only one value in the JComboBox is that your code ultimately hands the UI a single value (the last one) or constructs a new JComboBox but never puts it into the visible container. 's hint ("it does exactly what you tell it to do") is the right mindset: either add each item into the combo as you read rows, or build a collection/model and set that on the combo once.

Checklist and practical fixes

  • Populate the combo model while iterating, or collect all IDs into a List/Vector and then set the combo's model. Do not loop just to set a single returned String.
  • If you create a new JComboBox instance, make sure you add it to the panel/frame and call revalidate()/repaint() (or pack()) so it becomes visible. Constructing it alone won't change the displayed UI.
  • Make all Swing component creation/updates on the Event Dispatch Thread (use SwingUtilities.invokeLater).
  • Close DB resources (PreparedStatement/ResultSet). In modern Java use try-with-resources.

Example (conceptual) — build a model, then set it on the combo:

DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
try (PreparedStatement ps = conn.prepareStatement("SELECT ItemId FROM ItemDetails");
     ResultSet rs = ps.executeQuery()) {
    while (rs.next()) {
        model.addElement(rs.getString("ItemId"));
    }
}
SwingUtilities.invokeLater(() -> existingCombo.setModel(model));

Extra tips

  • If the query can be slow, run it off the EDT (SwingWorker) to avoid freezing the UI.
  • If you reuse an existing combo, call removeAllItems() before adding new ones.
    Following these steps will make sure every ItemId from the result set appears in the JComboBox and that the UI reliably shows it.

Recommended Answers

All 4 Replies

The while loop only runs one time because of the statement

return itemNo ;

Once that statement executes the loop (and the entire method) are ended.

Take out the return and it should work. Why are you putting a return there anyways?

:cool: For more help,

Hi ,
I commented the return statement. It is printing all the itemId in the Console.but in the combobox it is printing only the last record id. Can u please tell me what to do.

The code is:-

public String selectItemID(){
String itemNo = null;

try {
statement = dbConnection.prepareStatement("select ItemId from ItemDetails");
ResultSet itemNumberResult = statement.executeQuery();
while(itemNumberResult.next())
{
itemNo = itemNumberResult.getString(1);
System.out.println("itemNo " +itemNo);
//return itemNo ;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return itemNo;

}
itemno=new JComboBox();
DbActivity dbconnection = new DbActivity();

String result= dbconnection.selectItemID();
itemno.addItem(result);
System.out.println("Result "+result);

It does exactly what you tell it to do.
First you loop over the entire resultset, then you add the current entry (which at that point is the last one) to the control.

I suggest you analyse what you're doing, you'll soon enough figure out why it's not what you want and how to fix it.
That way you'll actually learn from your mistakes, as opposed to reading a ready-made solution.

Hi,
I used Vector.but the records are not displaying in the jcombobox.It is printing the values in the console. I think the problem is with the line 'itemno=new JComboBox(result);' . I am getting the values from the selectItemID() method. but it is not assigning it to the jcombobox.

The Code is:-

public Vector selectItemID(){
Vector result = new Vector();

try {
statement = dbConnection.prepareStatement("select ItemId from ItemDetails");
ResultSet itemNumberResult = statement.executeQuery();
while(itemNumberResult.next())
{
String itemNo = itemNumberResult.getString(1);
System.out.println("itemNo " +itemNo);
result.add(itemNo); // add all found itemNos
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;

}


DbActivity dbconnection = new DbActivity();

Vector result= dbconnection.selectItemID();
itemno=new JComboBox(result); // construct Combo with Vector
System.out.println("Result "+result);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.