ComboBox Selection Problems

I am writing a program that reads a file into an arrayList. The first value of the file is a description which I pull out and popualte a ComboBox with. I need to be able to make a selection from the combobox and have the entire object then be displayed in a textarea.

My first thought was to add a button to the right of the combobox that would get which line was selected then pull the approprate object from the arraylist and add it to the textarea. I just can seem to get it to work.

Here is the code for the comboBox

            String s;
            try
        {
              BufferedReader in = new BufferedReader(new FileReader("FoodCalories.txt"));
              
              while ((s = in.readLine()) != null)
              {
                    StringTokenizer st = new StringTokenizer(s, "*");
                // Fill an Array with Food Objects read from file
                    Food food = new Food(st.nextToken(), st.nextToken(), Integer.parseInt(st.nextToken()));
                foods.add(food);
              }
              in.close();
        }
        catch (IOException ex)
        {
        ex.printStackTrace();
        }
        // Create Combo Box
        JComboBox comboBox = new JComboBox();
       
        // Populate ComboBox with just Food Description
        for( int i=0; i<foods.size(); i++ )
        {
              Food foodFromArrayList = (Food)foods.get(i);
              comboBox.addItem(foodFromArrayList.getFood());
        }
            comboPanel.add ( comboBox );

 
reefcrazedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

suprapto45Commented:
I think that you need to add DefaultComboBoxModel to your JComboBox

David
0
reefcrazedAuthor Commented:
Thanks, let me read over that and see what i can come up with.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

reefcrazedAuthor Commented:
Like this?
        final DefaultComboBoxModel model = new DefaultComboBoxModel();
        comboBox = new JComboBox(model);
0
suprapto45Commented:
Yep,

But you just forgot another thing.

        String[] foodCombo = new String[foods.size()];

        // Populate ComboBox with just Food Description
        for( int i=0; i<foods.size(); i++ )
        {
             Food foodFromArrayList = (Food)foods.get(i);
             foodCombo[i] = foodFromArrayList.getFood();
             //comboBox.addItem(foodFromArrayList.getFood());
        }

        final DefaultComboBoxModel model = new DefaultComboBoxModel(foodCombo);
        comboBox = new JComboBox(model);
0
CEHJCommented:
Regardless of what kind of model you have, you can just add an ActionListener and then in its handler:

textArea.setText(comboBox.getSelectedItem().toString());

Provide an appropriate toString method for your Food class

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
reefcrazedAuthor Commented:
Thats kinda where I was going in my mind CEHJ. Somehting with getSelectedItem and toString.
I all ready have some info in the textArea, will this append to that or will it overwrite it?

Ohh and at work so not able to try anything with the code till lunch.
0
CEHJCommented:
>>I all ready have some info in the textArea, will this append to that or will it overwrite it?

It will overwite it. If you want to append, then call append instead
0
Mayank SAssociate Director - Product EngineeringCommented:
>> I all ready have some info in the textArea, will this append to that or will it overwrite it?

setText () will over-write.
0
Mayank SAssociate Director - Product EngineeringCommented:
Duh~! I'm not refreshing soon.
0
suprapto45Commented:
I think that I misunderstand the asker's question, don't I?
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.