Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all text in JTable cell when editing but not when typing?

Tags:

java

swing

jtable

The default behavior of a JTable is to append to the contents when you start typing, and to place the caret at the clicked location when clicking. I want the behavior of both these things to change, so the contents is replaced when I edit a cell, either by typing or by clicking and then typing. When I click a cell and then change the caret position, however, I want the contents to stay so I can change it.

I know how to select all when the cell becomes editing, by replacing the cell editor with one that selects all inside a SwingUtilities.invokeLater (see elsewhere), but that causes the typing behavior to break. When I do this and start typing in a cell, first the typed character is appended to the string, then it is selected (but the selection is invisible!) and when typing another character the contents gets replaced by that.

Is there a way to replace the contents immediately when typing in a highlighted (but not editing) cell, but select all when clicking a cell?

Here is the code I use for the CellEditor:

public class TextFieldCellEditor extends JTextField implements TableCellEditor
{
    private CellEditorListener  cellEditorListener  = null;

    private boolean             isInteger           = false;
    private Object              oldValue;

    // Start editing
    @Override
    public Component getTableCellEditorComponent(JTable table, Object obj, boolean isSelected, int row, int column)
    {
        Color color2 = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
        super.setBackground(color2 != null && (row & 1) == 1? color2 : table.getBackground());
        super.setForeground(table.getForeground());
        super.setBorder(DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder"));

        super.setText(obj.toString());

        isInteger = obj instanceof Integer;
        if (isInteger)
        {
            super.setHorizontalAlignment(SwingConstants.RIGHT);
            oldValue = obj;
        }

        // SwingUtilities.invokeLater(new Runnable()
        // {
        // public void run()
        // {
        // TextFieldCellEditor.this.selectAll();
        // }
        // });

        return this;
    }

    // Retrieve e dited value
    @Override
    public Object getCellEditorValue()
    {
        if (isInteger)
        {
            // Try to convert to integer. If input is invalid, revert.
            try
            {
                return new Integer(super.getText());
            }
            catch (NumberFormatException e)
            {
                return oldValue;
            }
        }
        return super.getText();
    }

    @Override
    public boolean isCellEditable(EventObject e)
    {
        return true;
    }

    @Override
    public boolean shouldSelectCell(EventObject e)
    {
        return true;
    }

    @Override
    public boolean stopCellEditing()
    {
        cellEditorListener.editingStopped(new ChangeEvent(this));
        return true;
    }

    @Override
    public void cancelCellEditing()
    {
        cellEditorListener.editingCanceled(new ChangeEvent(this));
    }

    @Override
    public void addCellEditorListener(CellEditorListener celleditorlistener)
    {
        cellEditorListener = celleditorlistener;
    }

    @Override
    public void removeCellEditorListener(CellEditorListener celleditorlistener)
    {
        if (cellEditorListener == cellEditorListener) cellEditorListener = null;
    }
}
like image 780
Mark Jeronimus Avatar asked Nov 17 '11 17:11

Mark Jeronimus


1 Answers

In your getTableCellEditorComponent() implementation, add the following:

if (isSelected) {
    this.selectAll();
}

As an aside, why not extend AbstractCellEditor or DefaultCellEditor(JTextField textField)? See also How to Use Tables: Using Other Editors.

Addendum: See also Table Select All Renderer and Table Select All Editor.

like image 159
trashgod Avatar answered Sep 28 '22 03:09

trashgod