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?
Asked
Active
Viewed 606 times
0

trashgod
- 203,806
- 29
- 246
- 1,045

user3089869
- 209
- 2
- 6
- 14
-
How i can i accomplish this? - by 1. reading Oracle tutorial HOw to use Tables 2. post an SSCCE, short, runnable, compilable – mKorbel Dec 17 '13 at 11:09
-
Are you talking about a JTable? – Mr. Polywhirl Dec 17 '13 at 11:10
-
@mKorbel..what is sscce? – Java Man Dec 17 '13 at 11:13
-
Please edit your question to include an [sscce](http://sscce.org/), for [example](http://stackoverflow.com/a/4528604/230513). – trashgod Dec 17 '13 at 11:15
-
Yes i am using JTable – user3089869 Dec 17 '13 at 11:16
2 Answers
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