Get all selected index of Checkbox

六月ゝ 毕业季﹏ 提交于 2020-01-07 05:46:06

问题


I am a beginner to 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. How can I accomplish this?


回答1:


have a look at this,

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...




回答2:


As you use a JTable you are using a TableCellRenderer for the "checkbox column". As long you add check boxes to column 1 you "know" in which row the checkbox is created. As you know the row(=index) you can register an action to collect together the checked boxes 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;
}

}



来源:https://stackoverflow.com/questions/20632445/get-all-selected-index-of-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!