简体   繁体   English

如何获取JCheckbox的选定索引?

[英]How to get the selected index of JCheckbox?

How to get the selected index (from a number of jcheckbox added to the screen using for loop) of JCheckbox?. 如何从JCheckbox中获取所选索引(从使用for循环添加到屏幕的一些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. 当我单击复选框时,我想获得所选复选框的索引。

You have an array of JCheckBox, and you can simply iterate through your array and find out which JCheckBox has been selected. 你有一个JCheckBox数组,你可以简单地遍历你的数组并找出已经选择了哪个JCheckBox。

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. 编辑:您将通过使用传递给ActionListener的ActionEvent的getSource()方法找出选择了哪个复选框。 For example you could change your ActionListener to as follows: 例如,您可以将ActionListener更改为如下所示:

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
      }
    }
  }
});

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. 据我所知,您希望获取所选JCheckBox的索引,以便对用户的操作做出适当的响应。

If this is the case, you might want to consider a different approach: you can register an ItemListener for each of your checkboxes. 如果是这种情况,您可能需要考虑不同的方法:您可以为每个复选框注册ItemListener

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 ) (受java2s.com教程启发)

In this case the event will be fired immediately and you will always know which checkbox was just clicked. 在这种情况下,事件将立即触发,您将始终知道刚刚单击了哪个复选框。

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, 'e'是ActionEvent对象,

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. 这样,识别出最近的选择或取消选择复选框。

通过复选框迭代并检查isSelected标志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM