简体   繁体   English

获取复选框的所有选定索引

[英]Get all selected index of Checkbox

I am a beginner to Java Swing. 我是Java Swing的初学者。 I have a table with 3 columns. 我有一张三列的桌子。 The first column has only check boxes. 第一列只有复选框。 I wanted to get the index of all the selected items of the check box and store it in an ArrayList . 我想获取复选框所有选定项的索引,并将其存储在ArrayList How can I accomplish this? 我该怎么做?

have a look at this, 看看这个

http://www.java2s.com/Code/Java/Swing-JFC/SwingCheckBoxDemo.htm http://www.java2s.com/Code/Java/Swing-JFC/SwingCheckBoxDemo.htm

If you want to return all selected items, you can use List or Set for this. 如果要返回所有选定的项目,可以为此使用“列表”或“设置”。

Post the code what you have. 将代码发布出来。 I may assist... 我可以协助...

As you use a JTable you are using a TableCellRenderer for the "checkbox column". 当您使用JTable时,您将使用TableCellRenderer作为“复选框列”。 As long you add check boxes to column 1 you "know" in which row the checkbox is created. 只要将复选框添加到第1列,您就可以“知道”在其中创建该复选框的行。 As you know the row(=index) you can register an action to collect together the checked boxes index. 如您所知,该行(= index)可以注册一个动作以将复选框索引收集在一起。

(from scratch) (从头开始)

public class MyRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, final int row, int column) {
    if (column != 1) {
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
    JCheckBox cb = new JCheckBox();
    cb.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            list.add(row); // whatever list is ...
        }

    });
    return cb;
}

} }

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

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