I am trying to add an an array to a jComboBox but I cannot get the combo box list to display, the first value is displayed in the top field and the drop down does not open.
The array is an array of objects, I am trying to load a specific field from the objects in the array.
The code I am using is as follows:
roundsStockObj //an array of objects, I want to use the .getDescription() field from each object and load to the combobox, this field is a string of the product description.
DefaultComboBoxModel model = (DefaultComboBoxModel)main.roundStockPanel.cmbRoundStockList.getModel();
for (int d = 0; d < roundsStockObjData.length; d++) {
The array is there, I can see the values if I test in a text box and iterate through. But when I run the above I get a value in the top field as follows:
{Ljava.lang.Object;@92a4dd
So in summary, I think I have two issues here, firstly the combo box is not being populated with the correct data, and secondly the drop down does not work, only one value is displayed (albeit incorrect) and the drop down function to display the list doesnt work...
Any help you can give would be hugley appreciated as I am completely stuck until I resolve this.
Thanks
JavaProgramming Theory
Last Comment
Mick Barry
8/22/2022 - Mon
for_yan
It looks like you are adding an array as each element of the model, that's why you see the toString() method of the whole array displayed. You should add individual objects to each element - not arrays
for_yan
You probabky used [] brackets instead of () parentheses after new Object - please check
for_yan
You probabky used [] brackets instead of () parentheses after new Object - please check
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
BinaryFlusher
ASKER
Hi for_yan
I have amended the code as follows, to create an arrray of objects that I then pass the individual value to the combox inside the for loop.
Object[] items = new Object[roundStockObj.length];
for (int d = 0; d < roundsData.length; d++) {
items[d] = roundStockObj[d].getDescription();
model.addElement(items[d]);
}
This seems to be a bit closer, I now get a proper value in the top field for the combo box, however this valiue is the first in the array and I still cannot get the drop down to open or diosplay the other values...
Any ideas?
for_yan
This is really strange. Could it happen that you have only one roundStock object? If it added one - why should not it add more of them?
Put a printout of item descriptions before you are adding them.
If I don't need anything sophisticted, I usually just use JComboBox(Vector) constructor - don't even need to think much about models
BinaryFlusher
ASKER
If the combo box is already instantiated, how best can I add the array to the combo box, as in not using the constructor?
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
Just for your info, I tested with text boxes and the data was being populated correctly, as I expected.
After experimenting woth some of the component properties, I found that uncheking the "lightWeightPopup" value, so changing to a heavyweight popup the display now works as expected.
Unlimited question asking, solutions, articles and more.
CEHJ
:)
Mick Barry
thats a poor solution, you have no way of knowing which objects are in your list
you should instead use a custom renderer
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!