无法编辑看似任意的 JTable 列

发布于 2024-09-17 06:36:02 字数 1029 浏览 9 评论 0原文

正如标题所述,我有一个 JTable,但无法编辑“任意”列。我有 4 列,只有第一列是可编辑的。第一列有文件和一个特殊编辑器,接下来的两列有字符串,最后一列有整数。我使用的是自定义模型,并且从 isCellEditable 方法返回 true。当然,我首先查阅了几个网站寻求帮助,但我找不到任何有帮助的东西。我使用java源代码重写了几个JTable方法并插入打印语句。特别是,我发现 table.editCellAt(row, col) 总是返回 false,因为从单元格编辑器返回的编辑组件始终为 null。因此,我很自然地尝试使用 table.setDefaultEditor(String.class, new MyEditor()) 替换编辑器。奇怪的是,这不起作用。 String 列的所有编辑器仍然是 JTable 默认使用的 GenericEditor。然后,我尝试通过执行以下操作将编辑器添加到每列:

TableColumnModel model = table.getColumnModel();
for(int i = 1; i < model.getColumnCount(); i++){
    model.getColumn(i).setCellEditor(new MyEditor());
}

请注意,i 从 1 开始,因为第一列已经有合适的编辑器。我现在没有想法,所以我向 Stack Overflow 的好心人寻求帮助。

编辑:我正在使用 DefaultTableModel,我只是覆盖 isCellEditable 以确保它始终返回 true (即使 DefaultTableModel 应该这样做默认)。我这样做是为了减少无用的、浪费的调试响应的数量。此外,一列可编辑但其他列不可编辑这一事实似乎表明问题出在其他地方。

编辑:问题似乎在于列创建。一位教授建议更改 setAutoCreateColumnsFromModel ,它似乎已经解决了这个问题。

As the title states, I have a JTable and I am unable to edit "arbitrary" columns. I have 4 columns and only the first column is editable. The first column has files and a special editor, the next two columns have Strings, and the last column has Integers. I'm using a custom model, and I am returning true from the isCellEditable method. Of course, I consulted several websites for help first, but I was unable to find anything that helped. I used the java source code to override several JTable methods and insert print statements. In particular, I found that table.editCellAt(row, col) always returns false because the editing component returned from the cell editor is always null. So, I naturally tried to replace the editor using table.setDefaultEditor(String.class, new MyEditor()). Oddly, that DID NOT work. All editors for the String columns were still the GenericEditor that JTable uses by default. I then tried adding the Editors to each column by doing the following:

TableColumnModel model = table.getColumnModel();
for(int i = 1; i < model.getColumnCount(); i++){
    model.getColumn(i).setCellEditor(new MyEditor());
}

Note that i starts at 1 because the first column already has an appropriate editor. I'm out of ideas at this point so I came to the good people at Stack Overflow for some help.

Edit:I am using a DefaultTableModel, I simply overrode isCellEditable to make sure it always returns true (even though DefaultTableModel is supposed to do that be default). I did this to reduce the amount of unhelpful, wasteful debugging responses. Also, the fact that one column is editable but others aren't would seem to indicate the problem is elsewhere.

Edit: It would appear the issue rested with column creation. A professor suggested changing setAutoCreateColumnsFromModel and it seems to have fixed the issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

§普罗旺斯的薰衣草 2024-09-24 06:36:04

我正在使用 DefaultTableModel,我只是覆盖 isCellEditable 以确保它始终返回 true (即使 DefaultTableModel 应该默认这样做)。我这样做是为了减少无用的、浪费的调试响应的数量。此外,一列可编辑但其他列不可编辑这一事实似乎表明问题出在其他地方。

I am using a DefaultTableModel, I simply overrode isCellEditable to make sure it always returns true (even though DefaultTableModel is supposed to do that be default). I did this to reduce the amount of unhelpful, wasteful debugging responses. Also, the fact that one column is editable but others aren't would seem to indicate the problem is elsewhere.

紫﹏色ふ单纯 2024-09-24 06:36:04

问题似乎在于列的创建。一位教授建议更改 setAutoCreateColumnsFromModel ,它似乎已经解决了这个问题。

It would appear the issue rested with column creation. A professor suggested changing setAutoCreateColumnsFromModel and it seems to have fixed the issue.

青朷 2024-09-24 06:36:03

只需 5 行“自定义代码”即可测试 JTable 的使用。其余代码是您将来可能创建的任何 SSCCE 的模板。

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        DefaultTableModel model = new DefaultTableModel(5, 3);
        JTable table = new JTable( model );
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Basic SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SSCCE() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

It only take 5 lines of "custom code" to test the usage of a JTable. The rest of the code is a template for any SSCCE you might create in the future.

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        DefaultTableModel model = new DefaultTableModel(5, 3);
        JTable table = new JTable( model );
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Basic SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SSCCE() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文