returning values from a list
i have to make this food selector for a uni project and i was wondering if someone could help me i need it to when i click on the desert list it puts the information in to some lables.
private void desertlstMouseClicked(java.awt.event.MouseEvent evt) {
Food selectedfood = (Food)foodlist.getSelectedValue();
if (selectedfood != null) {
outPrintInfo(foodlist);
}
}
private void outPrintInfo(Food e){
pricelbl.setText(""+e.getCalAmount());
calorieslbl.setText(""+e.getPrice());
}
this is what i have so far but it genarates a error :/
C:\Users\houlahan\MenuApplication\src\FoodSelectorGUI.java:84: cannot find symbol
symbol : method getSelectedValue()
location: class FoodList
Food selectedfood = (Food)foodlist.getSelectedValue();
C:\Users\houlahan\MenuApplication\src\FoodSelectorGUI.java:86: setAllTextFields(Food) in FoodSelectorGUI cannot be applied to (FoodList)
setAllTextFields(foodlist);
any ideas please?
thanks in advanced masterhoulahan.
Re: returning values from a list
If getSelectedValue() belongs to the class Food, and you're trying to convert foodlist, you should do it like this:
Code:
Food selectedfood = ((Food)foodlist).getSelectedValue();
Yours won't work because the operator '.' has a higher priority than the operation of convertion.
Re: returning values from a list
The 'foodlist' variable clearly isn't the type you think it is. Unfortunately, you haven't posted the declaration or code for foodlist', so it's not possible to say more.
I don't think Helstorm's suggestion has merit (i.e. is a 'foodlist' an instance of type 'Food'? unlikely - given the context, it's probably a list of type 'Food').
To arrive at the simple is difficult...
R. Elisha
3 Attachment(s)
Re: returning values from a list
ive uploaded what i have got so far basicly i want it to return the price of the food and the amount of calories in t a lable when an item is selected from the jlist and the data comes from the food class.
thanks again masterhoulahan.
Re: returning values from a list
I was hoping you'd take my hint, but apparently not.
If you want to get the selected value, you should call getSelectedValue() on an object that has that method, e.g. desertlst.
Incidentally, it's not good practice to use floating-point types like double for money values. Use an integral type such as long or BigInteger and store the money in the lowest denomination.
Imagination is more important than knowledge...
A. Einstein
Re: returning values from a list
<3 saved my life :P thank you so much for your help!