2

Problem

I want the capabilities of JXTable with the "select all on edit" behavior of RXTable. Doing a simple override would be fine but the double click function of the RXTable doesn't work with JXTable. When using the Button Action mode it's fine, but when using F2 or a double click something in JXTable clashes with RXTable and remove the selection so I'm left with default behavior. Is it because of the GenericEditor that it uses internally or is it something else?

How can I get JXTable to select all on F2 or double click edit?

EDIT: It looks like this only happens when the model has the column defined for type Integer. It works as expected when it is defined for a String or Object column.

Solution

Thanks to kleopatra's fix I was able to alter the selectAll method so it handles JFormattedTextFields and all cases of editing. Since the original code worked on type to edit I just used the fix for other cases. Here is what I ended up with.

Replace selectAll in RXTable with the following:

/*
 * Select the text when editing on a text related cell is started
 */
private void selectAll(EventObject e)
{
    final Component editor = getEditorComponent();

    if (editor == null
        || ! (editor instanceof JTextComponent 
                || editor instanceof JFormattedTextField))
        return;

    if (e == null)
    {
        ((JTextComponent)editor).selectAll();
        return;
    }

    //  Typing in the cell was used to activate the editor

    if (e instanceof KeyEvent && isSelectAllForKeyEvent)
    {
        ((JTextComponent)editor).selectAll();
        return;
    }

    // If the cell we are dealing with is a JFormattedTextField
    //    force to commit, and invoke selectall

    if (editor instanceof JFormattedTextField) {
           invokeSelectAll((JFormattedTextField)editor);
           return;
    }

    //  F2 was used to activate the editor

    if (e instanceof ActionEvent && isSelectAllForActionEvent)
    {
        ((JTextComponent)editor).selectAll();
        return;
    }

    //  A mouse click was used to activate the editor.
    //  Generally this is a double click and the second mouse click is
    //  passed to the editor which would remove the text selection unless
    //  we use the invokeLater()

    if (e instanceof MouseEvent && isSelectAllForMouseEvent)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                ((JTextComponent)editor).selectAll();
            }
        });
    }
}

private void invokeSelectAll(final JFormattedTextField editor) {
    // old trick: force to commit, and invoke selectall
    editor.setText(editor.getText());
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            editor.selectAll();
        }
    });
}
Robbie
  • 892
  • 2
  • 7
  • 19
  • worksforme - subclassing JXTable and overriding/adding the methods as in RXTable has same behaviour as doing the same with core JTable – kleopatra Sep 10 '11 at 10:09
  • @kleopatra look at my edit. It seems it's only a problem for Integer columns. – Robbie Sep 12 '11 at 14:35
  • ahh ... I see, thanks for the info: the underlying reason for the different behaviour is that xtable uses a JFormattedTextField as editing component, they are trickier to handle. No solution handy ... – kleopatra Sep 12 '11 at 14:55
  • Didn't realize that it used JFormattedTextField, not it makes sense. Thanks for your help. – Robbie Sep 12 '11 at 17:21

1 Answers1

2

a quick hack working for two of the three selection types

       // in selectAll(EventObject) special case the formatted early
       if (editor instanceof JFormattedTextField) {
           invokeSelectAll(editor);
           return;
       }


        private void invokeSelectAll(final JFormattedTextField editor) {
            // old trick: force to commit, and invoke selectall
            editor.setText(editor.getText());
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    editor.selectAll();
                }
            });
        }

was remembered of the trick by How to select all text in a JFormattedTextField when it gets focus? - doesn't handle the case when starting edit by typing, in this case the content is not deleted (as for a normal textfield) but the new key is added.

Community
  • 1
  • 1
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • Works perfect for F2 and double click, I updated my question with a solution which is a layout of selectAll which allows the normal code to work on click-to-edit and uses your fix for all other cases. Thanks! – Robbie Sep 12 '11 at 17:20
  • There seems to be a delay of about 1 second till the letter you typed appears in the editor. Any way to make it appear faster? – Alex Burdusel Nov 24 '13 at 10:03
  • @Burfee never seen any delay (in win jdk6/7) - what's your context? Anyway, no way out exact debugging and finding the reason ;-) – kleopatra Nov 24 '13 at 11:12
  • I am checking if it is a KeyEvent.VK_DELETE so I can delete the content of the cell and the cell editor is a ComboBox. probably it gets the delay somewhere along the way with all the casting and checking. More debugging is needed if you say that the delay shouldn't be noticeable. Thanks for your answer. – Alex Burdusel Nov 24 '13 at 11:21
  • @Burfee you might consider posting a new question (with SSCCE!) - it's ugly weather here in Berlin :) – kleopatra Nov 24 '13 at 11:30
  • :) thanks. I created http://stackoverflow.com/questions/20175539/jcombobox-inside-jtable-delay-on-start-start-cell-editing – Alex Burdusel Nov 24 '13 at 14:16