Select all data when start editing JTable Cell
Asked Answered
C

6

2

I'm trying to make a table that works like Excel. Meaning, when a user starts to insert data into the cells the content into them is selected and changed by the new data inserted.

Clearness answered 11/9, 2009 at 17:25 Comment(0)
D
3

You can create a custom TableCellEditor for your table. This class will have an instance variable of a TextField, lets call it textField. Then the getTableCellEditorComponent method could look like this:

public Component getTableCellEditorComponent(JTable table, Object value, 
                             boolean isSelected, int row, int column ) {
    textField.setText(value.toString());
    textField.selectAll();
    return textField;
}
Dominate answered 11/9, 2009 at 17:40 Comment(1)
I don't think this solution will work in all cases... try wrapping selectAll in SwingUtilities.invokeLater(new Runnable(){public void Run(){textField.selectAll();}}); And don't forget to make textField final.Filicide
T
3

Creating a custom editor works fine if you only ever have String data in the table and only need a single editor. However, if you have multiple different types of data, like String, Integer, Double, currencies, percentages etc which all use a JTextField as an editor then you need to create multiple custom editors.

You can read up on the Table Select All Editor for another possible solution.

Texas answered 11/9, 2009 at 20:44 Comment(1)
Great Thanks your solution works perfect for me but i want to show cursor after edit, how can it possible. It requires Clicking after making edit to show Cursor. @CamickrWandis
A
2

Note that there is also another possibility, you can override JTable#prepareEditor like the following:

@Override
public Component prepareEditor(TableCellEditor editor, int row, int column) {
    Component c = super.prepareEditor(editor, row, column);
    if (c instanceof JTextComponent) {
        ((JTextComponent) c).selectAll();
    } 
    return c;
}
Atreus answered 27/1, 2010 at 12:58 Comment(0)
L
0

The solution above doesn't work when editing is started by a mouse click.

For some people the solution is to call selectAll() in an invokeLater() so that the text gets selected after the mouse events have been dispatched, but this isn't working for me (possibly because I'm using Substance look and feel?)

Swing internals get a mouseReleased() event later and change the caret again, as shown in this stack trace:

at javax.swing.text.JTextComponent.fireCaretUpdate(Unknown Source)
at javax.swing.text.JTextComponent$MutableCaretEvent.fire(Unknown Source)
at javax.swing.text.JTextComponent$MutableCaretEvent.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI$Handler.repostEvent(Unknown Source)

Here is my solution: Listen for caret position changes, and the first time the selection goes from all selected to none selected after cell editing has started, call selectAll() again. The caret listener can be installed by a custom cell editor as shown here, or in an overridden editCellAt() method in a custom JTable.

private class SelectAllCellEditor extends DefaultCellEditor
{
    public SelectAllCellEditor( JTextField tf )
    {
        super( tf );
    }

    /**
     * Flag to ensure we only install the caret listener on the editor once.
     */
    boolean listenerInstalled   = false;
    /**
     * Primes the caret listener to override deselection when the first mouseReleased() event is reposted to the editor.
     */
    boolean overrideDeselection = false;

    @Override
    public Component getTableCellEditorComponent( JTable table , Object value , boolean isSelected , int row , int column )
    {
        final JFormattedTextField tf = ( JFormattedTextField ) super.getTableCellEditorComponent( table , value , isSelected , row , column );

        if( !listenerInstalled )
        {
            tf.addCaretListener( new CaretListener( )
            {
                int lastDot     = 0;
                int lastMark    = 0;

                @Override
                public void caretUpdate( CaretEvent e )
                {
                    if( overrideDeselection )
                    {
                        int length = tf.getText( ) == null ? 0 : tf.getText( ).length( );

                        boolean wasAllSelected = ( lastDot == 0 && lastMark == length ) || ( lastDot == length && lastMark == 0 );
                        boolean nowNoneSelected = ( e.getDot( ) == 0 && e.getMark( ) == 0 ) || ( e.getDot( ) == length && e.getMark( ) == length );

                        if( wasAllSelected )
                        {
                            // don't try to override again until the next time cell editing is started
                            overrideDeselection = false;

                            // only re-select all if the selection went to none; otherwise the user clicked the cell and dragged to select part of the text
                            if( nowNoneSelected )
                            {
                                tf.selectAll( );
                            }
                        }
                    }

                    lastDot = e.getDot( );
                    lastMark = e.getMark( );
                }
            } );
            listenerInstalled = true;
        }

        // Prime the caret listener to override deselection when the first mouseReleased() event is reposted to the editor.
        overrideDeselection = true;
        tf.selectAll( );
        return tf;
    }
}
Leda answered 13/11, 2012 at 17:9 Comment(0)
S
0

If your objective is to empty the cell when editing begins, no need to use selectAll(). Simply set value to null.

Implementation example:

(overriding getTableCellEditorComponent() in DefaultCellEditor)

TableCellEditor myCellEditor = new DefaultCellEditor(new JTextField()){
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column)
    {
        // empty the cell on edit start
        delegate.setValue( (editorComponent instanceof JTextField)? null : value);
        return editorComponent;
    }
};
Service answered 14/9, 2013 at 9:39 Comment(0)
A
-1

You should look at extJS. There is a pretty steep learning curve, though..

Angelicangelica answered 11/9, 2009 at 17:30 Comment(1)
I found extJS working with java script and it is GPL liscence. I need at least a LGPL Liscence.Clearness

© 2022 - 2024 — McMap. All rights reserved.