Hi everyone,
I got a small problem with my JList with MULTIPLE INTERVAL SELECTION.
When i click on a item it adds the price to the total and when i ctrl+click another item it again adds the price to the total
Then problem is when i deselect one of the selected items it adds the price to the total again.
Is there someway to subtract the selected items price from the total?.
Heres a snippet of code.

JListList  tempList = new JList(list);
tempList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
this.tempList.addListSelectionListener(this);

public void valueChanged(ListSelectionEvent  evt) {
	int minIndex = tempList .getMinSelectionIndex();
	int maxIndex = tempList .getMaxSelectionIndex();
		for (int i = minIndex; i <= maxIndex; i++) {
		   if (evt.getValueIsAdjusting() == false & toppingList.isSelectedIndex(i)) {
				double extra = 1.1;
				double item = Double.parseDouble(itemprice.getText());
				price = item + extra;
				String res = Double.toString(price);
				itemprice.setText(res);
}

Thanks.

Recalculate the price after a selection change.

public void valueChanged(ListSelectionEvent evt){
        if (!evt.getValueIsAdjusting()){
            price = basePrice;
            if (evt.getSource() instanceof JList){
                JList list = (JList)evt.getSource();                
                for(Object value : list.getSelectedValues()){
                    //price += value;
                }                
            }
        }
    }
This article has been dead for over six months. Start a new discussion instead.