【问题标题】:Cell in a JTable not editable when cell type is not String?当单元格类型不是字符串时,JTable 中的单元格不可编辑?
【发布时间】:2012-07-08 04:26:03
【问题描述】:

我有自己的TableModel 实现,旨在显示来自SQL 数据库的数据。我已经覆盖了所有必要的方法,对列名使用字符串数组,对数据使用arraylist<Object[]>,对可以从数据库中检索的所有不同类型使用Class<?>[] 数组。我还有一个布尔数组,它指示哪些列是可编辑的,哪些是不可编辑的。在我将表中的所有内容都存储为对象并且尚未实现类型部分并且它运行良好之前。现在我已将类型添加到模型中,我无法编辑任何 int 类型的列,即使该列在我的布尔数组中是可编辑的。我已经覆盖了isEditable() 方法以简单地从该布尔数组中返回值,并且在有问题的 into 列上返回 true - 但它仍然不可编辑。这是定义行为还是有问题?恐怕我目前无法发布代码,因为我正在使用手机,我的笔记本电脑目前没有互联网连接,并且要到本周末才能发布。我已经搜索过,但谷歌只显示了很多关于使单元格可编辑或不可编辑的问题,而不是为什么你不能编辑 int 列。 编辑:这是一个显示我的问题的 pastebin:http://pastebin.com/cYJnyyqy

使用 jdk7 并且只有字符串列是可编辑的,即使 isEditable() 对所有列都返回 true。

【问题讨论】:

  • 如果您确定 isEditable 返回 true,您可能需要检查此列的编辑器。
  • 查看您的通知,但为了获得更好的帮助,请尽快发布 SSCCE 证明您的 JTable 问题
  • “将类型设置为 int”是什么意思?

标签: java swing jtable tablecellrenderer tablecelleditor


【解决方案1】:

嗯。我从未将原始类型(例如 int.class)用于 getColumnClass()。我一直使用“包装”类型,例如Integer.class.

尝试更改您的Class<?>[] types 以使用包装类而不是原语。例如

 Class<?>[] types = {
            String.class,
            Character.class,
            Integer.class,
            ...

这可能需要 Swing 找到正确的 Renderer/TableCellEditor。但我不确定……

【讨论】:

  • 这让我可以编辑除 char 列之外的所有列。诡异的。我会接受这个答案,但我很想知道 a)为什么原语不起作用 b)为什么 char 仍然不可编辑?
  • 至少在 Java 6 中,JTable.createDefaultEditors() 创建了一个编辑器的 HashMap,但用于 Number.class。 getDefaultEditor() 将(最终)发现 Number 是 Integer 的超类并使用适当的编辑器。但 int.class 不匹配。可以说,这是 Swing 代码中的一个错误。或者另一个由自动装箱引起的奇怪错误的例子。
【解决方案2】:

回答后续问题

  • 为什么 char 仍然不可编辑

Reason 是默认的通用编辑器:它只能处理具有以 String 作为参数的构造函数的类,而 Character 不能。出路是 Character 类的特定自定义编辑器。

这是 JTable.GenericEditor 抛出的地方:

public Component getTableCellEditorComponent(JTable table, Object value,
                                         boolean isSelected,
                                         int row, int column) {
    this.value = null;
    ((JComponent)getComponent()).setBorder(new LineBorder(Color.black));
    try {
        Class<?> type = table.getColumnClass(column);
        // Since our obligation is to produce a value which is
        // assignable for the required type it is OK to use the
        // String constructor for columns which are declared
        // to contain Objects. A String is an Object.
        if (type == Object.class) {
            type = String.class;
        }

        // JW: following line fails  
        constructor = type.getConstructor(argTypes);
    }
    catch (Exception e) {
        // JW: so the editor returns a null
        return null;
    }
    return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}

这里是 JTable 处理 null 的地方:

// JTable.editCellAt(...)
TableCellEditor editor = getCellEditor(row, column);
if (editor != null && editor.isCellEditable(e)) {
    editorComp = prepareEditor(editor, row, column);
    if (editorComp == null) {
        // JW: back out if the comp is null
        removeEditor();
        return false;
    }

【讨论】:

  • 这可能也是它不喜欢原语的原因,它们都不使用字符串,甚至没有构造函数?
猜你喜欢
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 1970-01-01
  • 2013-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多