Reputation: 5
I have a jList that contains elements with 3 parameters. The elements are
Item(double code, String name, double price)
I am trying to get the total price for all elements in the list and display it in a text box. Is this possible and if so, how can it be done?
Upvotes: 0
Views: 568
Reputation: 347314
Get the ListModel from the JList
ListModel model = instanceOfJList.getModel();
Iterate over the model elements...
double sum = 0;
for (int index = 0; index < model.size(); index++) {
Item item = (Item)model.getElementAt(index);
sum += item.getPrice();
}
// Display sum somewhere on the screen...
Upvotes: 1
Reputation: 1193
Does your item class has getters for the price?
If yes you can iterate over your list and each time get the price and add it to a variable; call it totalSum.
At the end you should be able to display your totalSum in a text box.
Upvotes: 0
Reputation: 10662
Well, you will probably have to iterate over the list and call something like getPrice() on every element to create the sum.
Upvotes: 0