Question

Problème

Je veux les capacités de JXTable avec le comportement "Tout sélectionner lors de l'édition" de RXTable . Faire un simple remplacement serait bien mais la fonction de double-clic du RXTable ne fonctionne pas avec JXTable. Lorsque vous utilisez le mode Action de bouton, c'est bien, mais lorsque vous utilisez F2 ou un double-clic, quelque chose dans JXTable se heurte à RXTable et supprime la sélection, donc je me retrouve avec le comportement par défaut. Est-ce à cause du GenericEditor qu'il utilise en interne ou est-ce autre chose?

Comment puis-je faire en sorte que JXTable sélectionne tout sur F2 ou double-cliquez sur Modifier?

EDIT: Il semble que cela ne se produit que lorsque le modèle a la colonne définie pour le type Integer. Il fonctionne comme prévu lorsqu'il est défini pour une colonne String ou Object.

Solution

Grâce au correctif de kleopatra, j'ai pu modifier la méthode selectAll afin qu'elle gère JFormattedTextFields et tous les cas d'édition. Étant donné que le code d'origine fonctionnait sur le type à modifier, j'ai simplement utilisé le correctif pour d'autres cas. Voici ce avec quoi j'ai fini.

Remplacez selectAll dans RXTable par ce qui suit:

/*
 * 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();
        }
    });
}
Était-ce utile?

La solution

un hack rapide fonctionnant pour deux des trois types de sélection

       // 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();
                }
            });
        }

s'est souvenu de l'astuce par Comment sélectionner tout le texte dans un JFormattedTextField lorsqu'il obtient le focus? - ne gère pas le cas lors du démarrage de l'édition en tapant, dans ce cas, le contenu n'est pas supprimé (comme pour un champ de texte normal) mais la nouvelle cléest ajouté.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top