0

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?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user3089869
  • 209
  • 2
  • 6
  • 14

2 Answers2

0

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

Tejas jain
  • 742
  • 7
  • 20
  • Please the cite the original [tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#checkbox); the knock-off is incomplete. – trashgod Dec 17 '13 at 11:18
0

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

}

PeterMmm
  • 24,152
  • 13
  • 73
  • 111