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.t xt"));
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.nextTo ken()));
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(foodFromA rrayList.g etFood());
}
comboPanel.add ( comboBox );
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.t
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.nextTo
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(foodFromA
}
comboPanel.add ( comboBox );
ASKER
Thanks, let me read over that and see what i can come up with.
ASKER
Like this?
final DefaultComboBoxModel model = new DefaultComboBoxModel();
comboBox = new JComboBox(model);
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(foodFro mArrayList .getFood() );
}
final DefaultComboBoxModel model = new DefaultComboBoxModel(foodC ombo);
comboBox = new JComboBox(model);
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(foodFro
}
final DefaultComboBoxModel model = new DefaultComboBoxModel(foodC
comboBox = new JComboBox(model);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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
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.
setText () will over-write.
Duh~! I'm not refreshing soon.
I think that I misunderstand the asker's question, don't I?
:-)
David