在JTable中编辑单元格时提供其他行为 [英] Provide additional behavior when editing a cell in JTable

查看:49
本文介绍了在JTable中编辑单元格时提供其他行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java创建应用程序.编辑JTable中的单元格时,我需要提供其他行为.因此,理想情况下,当单元在编辑后失去焦点时,就会发生这种情况.根据某些后期处理,我可能会重置单元格的值.我尝试使用单元格编辑器",但没有给我想要的行为.

I am creating a app in Java. I need to provide additional behavior when editing of a cell in a JTable. So ideally this will happen when the cell loses focus after editing. Depending upon some post processing I might reset the value of the cell. I tried using a a Cell Editor but it is not giving me the desired behavior.

仅在默认JTable中,当我双击一个单元格时,它才变为可编辑状态.但是在我的CellEditor实现中,单元格一经​​聚焦便可以编辑.

In the default JTable only when I Double click a cell it becomes editable. But in my implementation of CellEditor the cell becomes editable as soon as it comes into focus.

这是我的自定义CellEditor"的代码,

Here is the code for the My custom CellEditor,

public class ParameterDefinitionEditor 
    extends AbstractCellEditor
    implements TableCellEditor{

    private JTable table;
    private DefaultTableModel defaultTableModel;

public ParameterDefinitionEditor(DefaultTableModel defaultTableModel,
JTable table) { 

        super();
        this.table = table;
        this.defaultTableModel = defaultTableModel;

        TableColumnModel columnModel = table.getColumnModel();
        columnModel.getColumn(0).setCellEditor(this);

}

    public Component getTableCellEditorComponent(JTable table, 
                            Object value, 
                         boolean isSelected, 
                        int row, 
                         int column) {

        if (isSelected) {
            // Do some processing.
        } 

        ((JTextField)component).setText((String)value);

        // Return the configured component
        return component;
    }

    public Object getCellEditorValue() {

        return ((JTextField)component).getText();
    }


}

任何帮助将不胜感激.谢谢.

Any help will be appreciated. Thanks.

推荐答案

取决于一些后期处理 可能会重置单元格的值.

Depending upon some post processing I might reset the value of the cell.

如果需要,可以通过重写stopCellEditing()方法在单元格编辑器中执行此操作.

You can do this right in the cell editor if you desire by overriding the stopCellEditing() method.

仅当我在默认JTable中 双击它成为一个单元格 可编辑的.但是在我的实现中 CellEditor单元格变为可编辑的 成为焦点.

In the default JTable only when I Double click a cell it becomes editable. But in my implementation of CellEditor the cell becomes editable as soon as it comes into focus.

扩展DefaultCellEditor.这由setClickCountToStart()方法控制.

Extend the DefaultCellEditor. This is controlled by the setClickCountToStart() method.

因此理想情况下,当 单元格在编辑后失去焦点

So ideally this will happen when the cell loses focus after editing

我同意另一个建议,即您应该将TableModelListener添加到TableModel中.尽管根据您的要求,您可能需要考虑使用表单元侦听器.

I agree with the other suggestion that you should probably be adding the TableModelListener to the TableModel. Although depending on your requirement you may want to consider using the Table Cell Listener.

这篇关于在JTable中编辑单元格时提供其他行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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