Skip to main content

Checkbox Problem...The Itemstatchanged method !

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
3 replies [Last post]
ady_bavarezu89
Offline
Joined: 2011-06-09

Hello everyone ! I have a little problem with some checkboxes..I have this little program :

</p>
<p>private static JCheckBox box1, box2;</p>
<p>private static JLabel label1;</p>
<p>private static int total_cost = 0</p>
<p>....</p>
<p>public void itemStateChanged(ItemEvent event) {<br />
        if(event.getStateChange() == ItemEvent.SELECTED){<br />
            total_cost = total_cost + 10;<br />
            jLabel1.setText("Total Cost : " + total_cost);<br />
        }<br />
        else {<br />
            total_cost = total_cost - 10;<br />
            jLabel1.setText("Total Cost : " + total_cost);<br />
         }<br />
    } }</p>
<p>

So what the program does is this :...I have 2 checkboxes : box1 and box2. If I select box1 or box2, there is a JLabel (label1) which will show the total cost. So if :

-neither of the boxes are selected the label shows 0

-if box 1 is selected and box2 not selected the label shows 10(same if it's box2 the selected one and box1 deselected)

- if both are selected the label shows 20

The program works perfectly fine...But I need something else.... I don't wan't to add 10 everytime...there are products that are more expensive in my program...so for example if box1 is selected add 10, if box2 is selected add 20...and I have no idea how I could acces each JCheckBox. Can someone help me, please?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Rameye
Offline
Joined: 2011-06-09

import java.awt.GridLayout;
import java.awt.ItemSelectable;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.lang.reflect.InvocationTargetException;
import java.util.Hashtable;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PriceTotaler implements ItemListener {
private static int NUM_ITEMS = 10;
private static double totalPrice = 0;
private static JCheckBox[] boxes = new JCheckBox[NUM_ITEMS];
private static JFrame frame;
private static double[] prices = {
1.59,
2.59,
5.79,
7.50,
8.88,
9.99,
11.37,
13.29,
15.19,
17.23
};
Hashtable priceTable;
private static JLabel lblTotal;
private static JPanel panel;

PriceTotaler() {
frame = new JFrame("Tally It Up");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new GridLayout(0,1));
priceTable = new Hashtable();

for (int i=0; i boxes[i] = new JCheckBox("Item " + String.valueOf(i+1));
boxes[i].addItemListener(this);
priceTable.put(boxes[i], prices[i]);
panel.add(boxes[i]);
}

lblTotal = new JLabel("Current Total: $0");
panel.add(lblTotal);

frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);

}

public static void main(String[] args) throws InterruptedException, InvocationTargetException {
Runnable thread = new Runnable() {
public void run() {
new PriceTotaler();
}
};
javax.swing.SwingUtilities.invokeAndWait(thread);
}

public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
totalPrice += priceTable.get(e.getItemSelectable());
} else {
totalPrice -= priceTable.get(e.getItemSelectable());
}
lblTotal.setText("Current total: " + String.format("$%.2f", totalPrice));
}
}

ady_bavarezu89
Offline
Joined: 2011-06-09

wow awesome program man !! works perfectly. I'm curios, is it possible to do this even without threads?

thanks again !

Rameye
Offline
Joined: 2011-06-09

Yes, just code

new PriceTotaler();

within the main() method block, and nothing else. But unless you absolutely need not to for some reason, it is wiser I think to run your code on the event dispatching thread. That way your program's GUI stays responsive. And when you have big tasks (image fetching for example) use SwingWorker and let them execute in the background.