简体   繁体   English

在JTable中编辑单元格时提供其他行为

[英]Provide additional behavior when editing a cell in JTable

I am creating a app in Java. 我正在用Java创建应用程序。 I need to provide additional behavior when editing of a cell in a JTable. 在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 aa Cell Editor but it is not giving me the desired behavior. 我尝试使用一个Cell Editor,但是它没有给我想要的行为。

In the default JTable only when I Double click a cell it becomes editable. 在默认的JTable中,只有当我双击一个单元格时,它才可编辑。 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, 这是我的自定义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. 根据某些后期处理,我可能会重置单元格的值。

You can do this right in the cell editor if you desire by overriding the stopCellEditing() method. 如果需要,可以通过重写stopCellEditing()方法在单元格编辑器中执行此操作。

In the default JTable only when I Double click a cell it becomes editable. 在默认的JTable中,只有当我双击一个单元格时,它才可编辑。 But in my implementation of CellEditor the cell becomes editable as soon as it comes into focus. 但是,在我实施CellEditor的过程中,该单元格一旦成为焦点,就可以进行编辑。

Extend the DefaultCellEditor. 扩展DefaultCellEditor。 This is controlled by the setClickCountToStart() method. 这由setClickCountToStart()方法控制。

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. 我同意另一个建议,您应该将TableModelListener添加到TableModel中。 Although depending on your requirement you may want to consider using the Table Cell Listener . 尽管根据您的要求,您可能需要考虑使用Table Cell Listener

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 一组侦听器(实现TableModelListener),并且应该放置您的逻辑
in the "tableChanged" method. 在“ tableChanged”方法中。

Check this section on Swing tutorial as well: 还要在Swing教程中查看此部分:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

i achieved that type of behaviour by overriding stopCellEditing ( i use a custom implementation of AbstractCellEditor ) 我通过覆盖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 { ........

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM