Java:为什么 JTable 不使用 TableCellEditor?

发布于 2024-09-30 10:33:51 字数 1301 浏览 11 评论 0原文

MyTableCellEditor 的目的是使 JTable 单元格的行为类似于 Excel 单元格,IOW,单击单元格后输入一个值会覆盖而不是附加到现有值。 我没想到以下代码会在第一遍中起作用,但我确实希望在 getTableCellEditorComponent 和 getCellEditorValue 中命中调试断点。 当我使用 jTable 时,为什么没有调用 getTableCellEditorComponent 或 getCellEditorValue?

public class MyTable extends javax.swing.JFrame implements TableModelListener {
    private static final MyTableCellEditor tableCellEditor =
        new MyTableCellEditor();
        ...
    public MyTable() {
        initComponents();
        jTable.getModel().addTableModelListener(MyTable.this);
        ...
    private void initComponents() { // Generated by the Form Editor.
        jTable = new javax.swing.JTable();
        jTable.setCellEditor(tableCellEditor);
        ...
public class MyTableCellEditor extends AbstractCellEditor implements
        TableCellEditor {
    JComponent component = new JTextField();
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int rowIndex, int vColIndex) {
        if (isSelected) {
            ((JTextField)component).selectAll();
        }
        ((JTextField)component).setText((String)value);
        return component;
    }
    public Object getCellEditorValue() {
        return ((JTextField)component).getText();
    }
}

The intention of MyTableCellEditor is to make a JTable cell behave like an Excel cell, IOW, entering a value after single clicking in a cell overwrites rather than appends to the existing value.
I didn't expect the following code to work on the first pass, but I did expect to be hit debug breakpoints in getTableCellEditorComponent and getCellEditorValue.
Why isn't getTableCellEditorComponent or getCellEditorValue called when I use jTable?

public class MyTable extends javax.swing.JFrame implements TableModelListener {
    private static final MyTableCellEditor tableCellEditor =
        new MyTableCellEditor();
        ...
    public MyTable() {
        initComponents();
        jTable.getModel().addTableModelListener(MyTable.this);
        ...
    private void initComponents() { // Generated by the Form Editor.
        jTable = new javax.swing.JTable();
        jTable.setCellEditor(tableCellEditor);
        ...
public class MyTableCellEditor extends AbstractCellEditor implements
        TableCellEditor {
    JComponent component = new JTextField();
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int rowIndex, int vColIndex) {
        if (isSelected) {
            ((JTextField)component).selectAll();
        }
        ((JTextField)component).setText((String)value);
        return component;
    }
    public Object getCellEditorValue() {
        return ((JTextField)component).getText();
    }
}

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

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

发布评论

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

评论(1

梦忆晨望 2024-10-07 10:33:51

setCellEditor(TableCellEditor) 仅为当前活动单元格设置编辑器(一旦不再活动,该编辑器就会丢失。您需要调用 JTable.setDefaultEditor() 为特定类设置默认编辑器。

jTable.setDefaultEditor(String.class, tableCellEditor);

或者,您可以通过 TableColumnModel 为列设置编辑器,例如。

jTable.getColumnModel().getColumn(1).setCellEditor(tableCellEditor);

setCellEditor(TableCellEditor) sets the editor for the currently active cell only (and will be lost once it's no longer active. You want to call JTable.setDefaultEditor() to set the default editor for a specific class.

jTable.setDefaultEditor(String.class, tableCellEditor);

Alternatively, you can set an editor for the column through the TableColumnModel, eg

jTable.getColumnModel().getColumn(1).setCellEditor(tableCellEditor);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文