简体   繁体   中英

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

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;
    }
}

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 .

The cleanest solution I could find for this case was to overwrite the JTable's editCellAt and inform the CellEditor of how the edit was triggered:

@Override
public boolean editCellAt(int row, int column, EventObject e) {
    cellEditor.setKeyTriggered(e instanceof KeyEvent);
    return super.editCellAt(row, column, e);
}

And here is the relevant CellEditor code:

public class MyCellEditor extends DefaultCellEditor {

    private boolean keyTriggered;

    public MyCellEditor() {
        super(new JTextField());
        final JTextField textField = (JTextField) getComponent();
        textField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        if (!keyTriggered) {
                            textField.selectAll();
                        }
                    }
                });
            }
        });
    }

    public void setKeyTriggered(boolean keyTriggered) {
        this.keyTriggered = keyTriggered;
    }

    @Override
    public Component getTableCellEditorComponent(
            JTable table, Object value, boolean isSelected, int row, int column) {
        final JTextField textField = (JTextField)
                super.getTableCellEditorComponent(table, value, isSelected, row, column);
        textField.selectAll();
        return textField;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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