JTable中的复选框始终返回false,尽管它被选中 [英] Checkbox in JTable always return false despite of it's checked

查看:542
本文介绍了JTable中的复选框始终返回false,尽管它被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 JTable 中的复选框,itsef是 JPanel 的一部分。最初,我使用 JOptionPane ,然后单击确定按钮我得到了价值,但现在我添加 JPanel JFrame 。当我单击右上角的 X 时,它不会检索已单击的复选框的值,但能够获取其他列的值。以下代码段:

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()+"%";
                 }
            }


推荐答案


  1. 按照Java变量命名标准。变量名不应以大写字符开头。你们中有些人的名字是正确的。其他人不是。一致!!!

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

如果你总是得到false,那么可能是因为编辑器没有更新值Boolean.TRUE当你点击细胞。你不仅需要重载 isCellEditable(...)方法来确定哪些单元格是可编辑的,你需要重写 getColumnClass(。 ..)方法返回单元格的类(在这种情况下 Boolean.class ),因此表可以使用适当的渲染器和编辑器

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.

阅读Swing教程中如何使用表了解更多信息和工作示例。

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

这篇关于JTable中的复选框始终返回false,尽管它被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆