Reputation: 5
I am trying to create an enrollment system. I have 6 checkboxes, what I want to happen is if I check one of the checkbox and press the button compute, it should give the label a value of 2640. When I click on another checkbox and press the button compute again the value of the label is 5280 and so on if I click all of the 6 checkboxes.
IF ONE OF THE CHECKBOX IS NOT CLICKED AND I PRESS THE COMPUTE BUTTON, IT SHOULD SUBTRACT 2640 from the current total. Here is my code:
if (chkPLF.isSelected() == true) {
tFee = 2640;
lblMis.setText(Double.toString(misFee));
lblT.setText(Double.toString(tFee + tFee));
if (chkPLF.isSelected() == false) {
tFee = tFee - 2640;
lblMis.setText(Double.toString(misFee));
lblT.setText(Double.toString(tFee));
}
}
if (chkSAD.isSelected() == true) {
tFee = 2640 * 3;
lblMis.setText(Double.toString(misFee));
lblT.setText(Double.toString(tFee));
if (chkSAD.isSelected() == false) {
tFee = tFee - 2640;
lblMis.setText(Double.toString(misFee));
lblT.setText(Double.toString(tFee));
}
}
it only displays the current value but doesn't subtract. PLEASE HELP!
Upvotes: 0
Views: 1600
Reputation: 347244
Why not just count the number of checkboxs that are clicked an multiple the result by 2640. Right now, you seem to be overriding the result with each check box...
For example...
int checkCount = 0;
if (chk1.isSelected()) {
checkCount++;
}
if (chk2.isSelected()) {
checkCount++;
}
if (chk3.isSelected()) {
checkCount++;
}
if (chk4.isSelected()) {
checkCount++;
}
if (chk5.isSelected()) {
checkCount++;
}
if (chk6.isSelected()) {
checkCount++;
}
tFee = 2640 * checkCount;
lblMis.setText(Double.toString(misFee));
lblT.setText(Double.toString(tFee));
Right now, I'm going, this could be more easily done with a loop...
JCheckBox[] boxes = new JCheckBox[]{chk1, chk2, chk3, chk4, chk5, chk6};
tFee = 0;
for (JCheckBox box : boxes) {
if (box.isSelected()) {
tFee += 2640;
}
}
lblMis.setText(Double.toString(misFee));
lblT.setText(Double.toString(tFee));
But maybe that's because I'm crazy...
Upvotes: 1
Reputation: 2461
double amount = 0;
int sel = 0;
if(jCheckBox1.isSelected()) sel++;
if(jCheckBox2.isSelected()) sel++;
if(jCheckBox3.isSelected()) sel++;
if(jCheckBox4.isSelected()) sel++;
if(jCheckBox5.isSelected()) sel++;
if(jCheckBox6.isSelected()) sel++;
amount = 2640 * sel;
JOptionPane.showMessageDialog(rootPane, String.valueOf(amount));
Put this code in the "Compute" Click ActionListener
Upvotes: 1