• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

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

 
0
reefcrazed
Asked:
reefcrazed
  • 4
  • 3
  • 3
  • +1
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now