how do I simulate “onStartCellEditing” for DefaultCellEditor

前端 未结 2 793
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-25 01:28

CellEditorListener has \"editingStopped\" and \"editingCancelled\". But how might I implement a piece of code which needs to run when a cell editing session starts?

A t

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-25 01:56

    A typical example might be where you want the text of a JTextField editor component to go selectAll() when you start editing.

    You can override the editCellAt(...) method of JTable to select the text once editing has started:

    @Override
    public boolean editCellAt(int row, int column, EventObject e)
    {
        boolean result = super.editCellAt(row, column, e);
        final Component editor = getEditorComponent();
    
        if (editor != null && editor instanceof JTextComponent)
        {
            ((JTextComponent)editor).selectAll();
    
            if (e == null)
            {
                ((JTextComponent)editor).selectAll();
            }
            else if (e instanceof MouseEvent)
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        ((JTextComponent)editor).selectAll();
                    }
                });
            }
        }
    
        return result;
    }
    

提交回复
热议问题