0

The first time I select an option from the list, the value is displayed on the jtextarea The second time I select an option, the value doesn't change. Is there a refresh option? Or a better approach for this problem? Thanks!

Here's a snippet of the code:

String[] choices = {"Apple","Orange", "Pear"}; 

String fruit= (String) JOptionPane.showInputDialog(null, "Select Fruit:","Select Fruit", JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);  

if (fruit!= null){

jtextarea.append("Name\t:  " + fruit.getName() + "\n"); 
jtextarea.append("Color\t:  " + fruit.getColor() + "\n");

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Lily S
  • 245
  • 1
  • 8
  • 18

1 Answers1

2
fruit.getName()
fruit.getColor()

These two are wrong since it's a refrence to what your JOptionPane returns and not an object with an getter. As it is not your array choices contains strings. Simply append what your joptionpane returns which is fruit(This will be either Apple, Orange or Pear) :

jtextarea.append("Name\t:  " + fruit + "\n"); 
John Snow
  • 5,214
  • 4
  • 37
  • 44
  • 1
    If he wants to do it the way he's got it set up, he could also use a small inner class 'fruit' and populate the JOptionPane with three fruit objects. – Charles Apr 19 '12 at 17:53
  • I am able to get the values from my Fruit. But when I try the 2nd time, the value doesn't refresh. – Lily S Apr 19 '12 at 18:00