3

How to get the selected index (from a number of jcheckbox added to the screen using for loop) of JCheckbox?.

// for some t values:
checkBoxes[t] = new JCheckBox("Approve");
checkBoxes[t].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent  e) {
        boolean selected = checkBoxes[t].isSelected();
        System.out.println("Approved"+selected);
    }
});

When i click the check box, i want to get the selected check box's index.

Frank Orellana
  • 1,820
  • 1
  • 22
  • 29
learner
  • 331
  • 1
  • 9
  • 22
  • 1
    This is a confusing request since JCheckBox doesn't have an index. Do you mean to use a set of JRadioButtons instead? Or maybe a JComboBox? Or if you have an array or collection, you can iterate through them and find out which have been selected I suppose. – Hovercraft Full Of Eels Sep 03 '12 at 12:07

4 Answers4

4

You have an array of JCheckBox, and you can simply iterate through your array and find out which JCheckBox has been selected.

Regarding:

When i click the check box, i want to get the selected check box's index.

Edit: You would find out which checkbox was selected by using the getSource() method of the ActionEvent passed into the ActionListener. For example you could change your ActionListener to as follows:

checkBoxes[t].addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent  e) {
    boolean selected = checkBoxes[t].isSelected();
    System.out.println("Approved"+selected);

    int index = -1;
    for (int i = 0; i < checkBoxes.length; i++) {
      if (checkBoxes[i] == e.getSource()) {
        index = i;
        // do something with i here
      }
    }
  }
});
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    This is indeed a way. Another possibility is to set the `actionCommand` for the `JCheckBox` as the index, and retrieve that. Or pass the index to the constructor of the listener. – Robin Sep 03 '12 at 13:14
  • @Robin, true, but the way above allows all JComboBoxes to use the same listener (if that matters, I'm not sure). – Hovercraft Full Of Eels Sep 03 '12 at 13:32
  • +1 See also this [example](http://stackoverflow.com/a/4528604/230513) maintaining state in a `JTable` context. – trashgod Sep 03 '12 at 16:14
1

As far as I understand, you want to get the index of a selected JCheckBox in order to respond appropriately on a user's action.

If this is the case, you might want to consider a different approach: you can register an ItemListener for each of your checkboxes.

JCheckBox check = new JCheckBox("Approve");
check.addItemListener(new ItemListener() {
  public void itemStateChanged(ItemEvent e) {
    if (check.isSelected()){
      System.out.println(check.getName() + " is selected");
    }
  }
});

(inspired by java2s.com tutorial)

In this case the event will be fired immediately and you will always know which checkbox was just clicked.

Dimochka
  • 191
  • 1
  • 2
  • 7
0

I would try something like:

for (int i=0; i < checkBoxes.length; i++) {
if (checkBoxes[i].isSelected() == true) {
index = i; }
return index; }

From your question, this is what I gather that you are looking for.

EDIT:

My above previous method is flawed because it makes the very naive approach that one and only one box will be selected and that no box will be deselected.

Where 'e' is the ActionEvent object,

for (int i=0; i < checkBoxes.length; i++) {
if (checkBoxes[i] == e.getSource()) {
index = i; } }
return index; 

This way the most recent selection or deselection check box is identified.

Derek W
  • 9,708
  • 5
  • 58
  • 67
  • This won't work if he deselects a certain checkbox, or when some checkboxes have already been selected – Robin Sep 03 '12 at 13:12
  • Nice call. You would have definitely to use `getSource()` on the ActionEvent object in order to find out which button was just pushed. My first idea was flawed in the regard that it would not work if it were already selected or were deselected. I was just thinking of it as a one-and-done job. – Derek W Sep 03 '12 at 23:41
0

Iterate throuh the Checkboxes and check the isSelected flag

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130