Results 1 to 3 of 3
  1. #1
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Rep Power

    Default gathering jtable rowdata from checkbox selection

    I'm working with a JTable. I've added a Boolean.FALSE value in as the first element of my data [][] array that is picked up by table model thusly:

    Java Code:
        	model = new DefaultTableModel(data, colNames) {
        		public boolean isCellEditable(int rowIndex, int mColIndex) {
        			if(mColIndex > 0){
        			return false;
        			else return true;
        	public Class getColumnClass(int col) {
        	Object o = getValueAt(0, col);     
        	if(o == null){                 
        		return Object.class;   
        	else     {         
        		return o.getClass();   
    I know the checkbox created by the Boolean.FALSE is editable per above. I'd like to listen for changes to the box selection so I can gather data on the corresponding row to send it into an array to be returned later.

    Using SSCCE below as a model for my project, I'd expect the tableChanged(TableModelEvent e) listener to print the row and column (column would always be 0) when the box is checked. I'm not getting any runtime errors, but I'm not seeing the values printed either. I'd like to resolve this prior to doing more work on returning the rowdata values. Any help is greatly appreciated.

    Java Code:
    import java.awt.BorderLayout;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.ListSelectionModel;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.event.TableModelEvent;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.TableModel;
    public class TableSelectionTest extends JFrame implements ListSelectionListener {
    private final int COLUMN_COUNT = 5;
    private TblModel model;
    public TableSelectionTest() {
    private void initialize() {
    List data = new ArrayList();
    for (int i = 0; i < 10; i++) {
    Object record[] = new Object[COLUMN_COUNT];
    record[0] = Boolean.FALSE;
    for (int j = 1; j < COLUMN_COUNT; j++) {
    record[j] = new Integer(j);
    model = new TblModel(data);
    JTable table = new JTable(model);
    table.getSelectionModel().addListSelectionListener (this);
    JScrollPane scroll = new JScrollPane(table);
    getContentPane().add(scroll, BorderLayout.CENTER);
    public static void main(String[] args) {
    TableSelectionTest f = new TableSelectionTest();;
    class TblModel extends AbstractTableModel {
    private List data;
    public TblModel(List data) { = data;
    public int getColumnCount() {
    return COLUMN_COUNT;
    public int getRowCount() {
    return data == null ? 0 : data.size();
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
    getRecord(rowIndex)[columnIndex] = value;
    super.fireTableCellUpdated(rowIndex, columnIndex);
    public Object getValueAt(int rowIndex, int columnIndex) {
    return getRecord(rowIndex)[columnIndex];
    public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
    public Class getColumnClass(int columnIndex) {
    if (data == null || data.size() == 0) {
    return Object.class;
    Object o = getValueAt(0, columnIndex);
    return o == null ? Object.class : o.getClass();
    private Object[] getRecord(int rowIndex) {
    return (Object[]) data.get(rowIndex);
    public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting()) {
    int index = e.getFirstIndex();
    model.setValueAt(Boolean.TRUE, index, 0);
    public void tableChanged(TableModelEvent e) {
        int row = e.getFirstRow();
        int column = e.getColumn();        
        int columns = e.ALL_COLUMNS;
        TableModel model = (TableModel)e.getSource();
        String columnName = model.getColumnName(column);
        Object data = model.getValueAt(row, column);

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: gathering jtable rowdata from checkbox selection

    Please edit your post and format the code with appropriate indents. Unindented code without so much as a blank line between methods is next to impossible to read.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Rep Power

    Default Re: gathering jtable rowdata from checkbox selection

    Forgive me, but I've already corrected above problem.

    Directly related to this, however, is the next step in the project. I need to scan the table model and for rows with checbox Boolean.TRUE, get the rowdata in the jtable, and add it to some form of data structure. I was considering an List<Array>. Below is what I've sketched out so far, but am getting a "The method addAll(Collection<? extends Array>) in the type List<Array> is not applicable for the arguments (Array[])" warning from my IDE on the 'printgroup.add(printdata);' line, no matter what I do to change things around. Current (formatted ;)) code:

    Java Code:
    	 public void /*List<Array>*/ returnReportData(){
    		 TableModel tm2 = table.getModel();
    		 Boolean value;	 
    		 Array[] printdata = new Array[53];
    		 List<Array> printgroup = new  ArrayList<Array>();
    		 for(int p = 0; p < tm2.getRowCount(); p++){
    			 value = (Boolean) tm2.getValueAt(p,0);
    				 Object val;
    			        for (int i= 1; i < 53 ; i++) {
    			        	val =  tm2.getValueAt(p,i);
    			        	printdata[i]  =  (Array) val;
    	 //return printgroup;

Similar Threads

  1. How to Enable and Disable JtextField on Selection of Checkbox
    By deshmukh.niraj04 in forum New To Java
    Replies: 1
    Last Post: 04-11-2011, 12:26 PM
  2. Disable a checkbox in jtable
    By pink123 in forum AWT / Swing
    Replies: 8
    Last Post: 03-24-2011, 06:29 PM
  3. Adding Checkbox to Jtable through vectors
    By indra00 in forum AWT / Swing
    Replies: 2
    Last Post: 12-08-2010, 08:45 AM
  4. CheckBox in JTable
    By nikosa in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2009, 05:01 AM
  5. checkBox in Jtable renderining problem
    By pothraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2008, 11:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts