Volatil3
Volatil3

Reputation: 14978

Checkbox in JTable always return false despite of it's checked

I am using checkboxes in JTable which itsef is part of JPanel. Initially I was using JOptionPane and on clicking OK button I was getting value but now I added JPanel in a JFrame. When I click X sign on top right it is not retrieving value of clicked Checkboxes but able to fetch values of other columns. Code snippet given below:

DefaultTableModel dtm = new DefaultTableModel(rowData, columnNames)
        {
        };
for (int i = 0; i < records.size(); i++)
            {
               // System.out.println(records.get(i));
                singleRecord = records.get(i).toString().split("%");
                Pages = singleRecord[0].toString();
                BKey= singleRecord[1].toString();
                Title = singleRecord[2].toString();
                Author = singleRecord[3].toString();
                TimeStamp = singleRecord[4].toString();
                dtm.addRow(new Object[] { Boolean.FALSE ,Pages,BKey,Title,Author,TimeStamp});

        } 

table = new javax.swing.JTable(dtm)
        {
            public boolean isCellEditable(int row,int column)
            {
                /*if(column == 0)
                    return true;
                else
                    return false;
                 * 
                 */
                return(column < 2);
            }
        };

for (int i = 0; i < table.getRowCount(); i++)
            {
                 System.out.println(table.getValueAt(i,1).toString());
                 boolean isChecked = (Boolean) table.getValueAt(i,0);//always return false
                 if (isChecked)
                 {
                     System.out.println("checked ");
                   Ids+=table.getValueAt(i,2).toString()+"%";
                 }
            }

Upvotes: 0

Views: 528

Answers (1)

camickr
camickr

Reputation: 324108

  1. Follow Java variable naming standards. Variable names should NOT start with an upper case character. Some of you names are correct. Others are not. Be consistent!!!

  2. If you are always getting false then it is probably because the editor is not updating the value to Boolean.TRUE when you click on the cell. Not only do you need to override the isCellEditable(...) method to determine which cells are editable, you would need to override the getColumnClass(...) method to return the class of the cell (in this this case Boolean.class) so the table can use the appropriate renderer and editor.

Read the section from the Swing tutorial on How to Use Tables for more information and working examples.

Upvotes: 3

Related Questions