在 JTable 中编辑单元格时提供附加行为

发布于 2024-08-11 14:25:43 字数 1310 浏览 6 评论 0原文

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

在默认的 JTable 中,仅当我双击单元格时,它才变得可编辑。但在我的 CellEditor 实现中,单元格一旦成为焦点就变得可编辑。

这是我的自定义 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();
    }


}

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

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.

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.

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.

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

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

发布评论

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

评论(3

醉生梦死 2024-08-18 14:25:43

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

如果您愿意,可以通过重写 stopCellEditing() 方法在单元格编辑器中正确执行此操作。

仅当我在默认 JTable 中
双击一个单元格,它会变成
可编辑。但在我的实施中
CellEditor 单元格变得可编辑
一旦进入焦点。

扩展 DefaultCellEditor。这是由 setClickCountToStart() 方法控制的。

所以理想情况下,这会发生在
编辑后单元格失去焦点

我同意另一个建议,即您可能应该将 TableModelListener 添加到 TableModel。尽管根据您的要求,您可能需要考虑使用 Table Cell Listener

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

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

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.

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

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

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.

雅心素梦 2024-08-18 14:25:43

我认为提供自定义单元格编辑器不能满足您的目的。

如果您想根据用户操作进行一些处理,那么您的表模型应该具有
一组侦听器(实现 TableModelListener)和您的逻辑应该放在
在“tableChanged”方法中。

另请查看有关 Swing 教程的本节:
http://java.sun.com/docs/books/教程/uiswing/components/table.html

I don't think by providing custom cell editor serves your purpose.

If you want to do some processing based on user actions then your table model should have
a set of listeners (that implement TableModelListener) and your logic should be put
in the "tableChanged" method.

Check this section on Swing tutorial as well:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

蓝梦月影 2024-08-18 14:25:43

我通过重写 stopCellEditing 实现了这种类型的行为(我使用 AbstractCellEditor 的自定义实现)

public boolean stopCellEditing()
{
String s = (String) getCellEditorValue();
if ( ! valueValidator.isValid(s) )
  {
  editorComponent.setBorder(new LineBorder(Color.red));        
  Toolkit.getDefaultToolkit().beep();
  return false;
  }
}
else { ........

i achieved that type of behaviour by overriding stopCellEditing ( i use a custom implementation of AbstractCellEditor )

public boolean stopCellEditing()
{
String s = (String) getCellEditorValue();
if ( ! valueValidator.isValid(s) )
  {
  editorComponent.setBorder(new LineBorder(Color.red));        
  Toolkit.getDefaultToolkit().beep();
  return false;
  }
}
else { ........
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文