Link to home
Start Free TrialLog in
Avatar of reefcrazed
reefcrazed

asked on

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

 
Avatar of suprapto45
suprapto45
Flag of Singapore image

I think that you need to add DefaultComboBoxModel to your JComboBox

David
Avatar of reefcrazed
reefcrazed

ASKER

Thanks, let me read over that and see what i can come up with.
Like this?
        final DefaultComboBoxModel model = new DefaultComboBoxModel();
        comboBox = new JComboBox(model);
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);
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
>>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
>> I all ready have some info in the textArea, will this append to that or will it overwrite it?

setText () will over-write.
Duh~! I'm not refreshing soon.
I think that I misunderstand the asker's question, don't I?
:-)