问题
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