?
Solved

Error adding array to jComboBox

Posted on 2011-03-05
12
Medium Priority
?
655 Views
Last Modified: 2012-05-11
Hi

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++) {
               
                model.addElement(new Object[]{roundsStockObj[d].getDescription()});
             
            }

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
0
Comment
Question by:BinaryFlusher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35043700
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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35043708
You probabky used [] brackets instead of () parentheses after new Object - please check
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35043709
You probabky used [] brackets instead of () parentheses after new Object - please check
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:BinaryFlusher
ID: 35043760
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?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35043821
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  
0
 

Author Comment

by:BinaryFlusher
ID: 35043932
If the combo box is already instantiated, how best can I add the array to the combo box, as in not using the constructor?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 35044075
You can simplify that code to the below: you're only adding the description string as the element at the moment anyway
for (int d = 0; d < roundsData.length; d++) {
            model.addElement(roundStockObj[d].getDescription());
        }

Open in new window

0
 

Author Comment

by:BinaryFlusher
ID: 35044081
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.

Very strange and very frustrating...

Thanks for your help though.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 2000 total points
ID: 35044085
>>If the combo box is already instantiated, how best can I add the array to the combo box, as in not using the constructor?

Just create a new model and use the code i just posted above
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35044101
:)
0
 
LVL 92

Expert Comment

by:objects
ID: 35044952
thats a poor solution, you have no way of knowing which objects are in your list
you should instead use a custom renderer
0
 
LVL 92

Expert Comment

by:objects
ID: 35044972
>             model.addElement(roundStockObj[d].getDescription());

And thats also just copying what for_yan had already suggested, so you should have accepted his comment.

for_yan>  You should add individual objects to each element - not arrays

0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This question got me thinking... (http://www.experts-exchange.com/questions/28707487/GLOBALS.html) Why shouldn't we use Globals? This is a simple question without a simple answer.  How do you explain these concepts to a programmer w…
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question