简体   繁体   中英

Get all selected index of Checkbox

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?

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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