Reputation: 1497
When the code below is run and the Beta check box is selected, then the Alpha check box, the text reads "Selected check boxes: Alpha, Beta" not "Selected check boxes: Beta, Alpha". Why do they appear in the opposite order to how they were selected?
// Demonstrate check boxes.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CBDemo implements ItemListener {
JLabel jlabSelected;
JLabel jlabChanged;
JCheckBox jcbAlpha;
JCheckBox jcbBeta;
JCheckBox jcbGamma;
CBDemo() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("Demonstrate Check Boxes");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(280, 120);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create empty labels.
jlabSelected = new JLabel("");
jlabChanged = new JLabel("");
// Make check boxes.
jcbAlpha = new JCheckBox("Alpha");
jcbBeta = new JCheckBox("Beta");
jcbGamma = new JCheckBox("Gamma");
// Events generated by the check boxes
// are handled in common by the itemStateChanged()
// method implemented by CBDemo.
jcbAlpha.addItemListener(this);
jcbBeta.addItemListener(this);
jcbGamma.addItemListener(this);
// Add checkboxes and labels to the content pane.
jfrm.add(jcbAlpha);
jfrm.add(jcbBeta);
jfrm.add(jcbGamma);
jfrm.add(jlabChanged);
jfrm.add(jlabSelected);
// Display the frame.
jfrm.setVisible(true);
}
// This is the handler for the check boxes.
public void itemStateChanged(ItemEvent ie) {
String str = "";
// Obtain a reference to the check box that
// caused the event.
JCheckBox cb = (JCheckBox) ie.getItem();
// Report what check box changed.
if(cb.isSelected())
jlabChanged.setText(cb.getText() + " was just selected.");
else
jlabChanged.setText(cb.getText() + " was just cleared.");
// Report all selected boxes.
if(jcbAlpha.isSelected()) {
str += "Alpha ";
}
if(jcbBeta.isSelected()) {
str += "Beta ";
}
if(jcbGamma.isSelected()) {
str += "Gamma";
}
jlabSelected.setText("Selected check boxes: " + str);
}
public static void main(String args[]) {
// Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CBDemo();
}
});
}
}
Upvotes: 1
Views: 129
Reputation: 36703
When any check box is clicked itemStateChanged() is called, the order of the string is driven by the order of your str+= statements in the code, not the temporal order of the clicks.
if(jcbAlpha.isSelected()) {
str += "Alpha ";
}
if(jcbBeta.isSelected()) {
str += "Beta ";
}
if(jcbGamma.isSelected()) {
str += "Gamma";
}
To achieve the desired behaviour
Try changing the 3 ifs to a single:
if (cb.isSelected()) {
selectionOrder.add(cb.getText()); // will return Alpha, Beta depending which is selected
}
jlabSelected.setText("Selected check boxes: " + selectionOrder);
Where selectionOrder is a field at the top of your CBDemo class
private List<String> selectionOrder = new ArrayList<String>();
This will obviously keep growing the list indefinitely, but fine for a demo.
Upvotes: 2
Reputation: 7730
Since Your order of Appending value to String is Alpha --then-->Beta--then-->Gamma
// Report all selected boxes.
if(jcbAlpha.isSelected()) {
str += "Alpha ";
}
if(jcbBeta.isSelected()) {
str += "Beta ";
}
if(jcbGamma.isSelected()) {
str += "Gamma";
}
So No Matter in which order you select the checkbox .
To achieve your desired output Use
// Report all selected boxes.
if(jcbAlpha.isSelected()) {
str += "Alpha ";
jcbAlpha.setSelected(false);// So when Next Time you click on other checkbox this condtion does not append result to Str
}
if(jcbBeta.isSelected()) {
str += "Beta ";
jcbBeta.setSelected(false);
}
if(jcbGamma.isSelected()) {
str += "Gamma";
jcbGamma.setSelected(false);
}
Upvotes: 0