2

問題

RXTableの「編集時にすべて選択」動作を備えたJXTableの機能が必要です。単純なオーバーライドを行うことは問題ありませんが、RXTable のダブルクリック機能は JXTable では機能しません。ボタン アクション モードを使用する場合は問題ありませんが、F2 を使用するか、JXTable で何かをダブルクリックすると、RXTable と衝突して選択が削除されるため、デフォルトの動作のままになります。内部で使用している GenericEditor のためですか、それとも何か他のものですか?

F2またはダブルクリック編集でJXTableをすべて選択するにはどうすればよいですか?

編集:これは、モデルに整数型用に定義された列がある場合にのみ発生するようです。文字列またはオブジェクト列に対して定義されている場合、期待どおりに機能します。

解決

kleopatra の修正のおかげで、selectAll メソッドを変更して、JFormattedTextFields とすべての編集ケースを処理できるようになりました。元のコードは編集するタイプで機能していたので、他のケースでは修正を使用しました。これが私が最終的に得たものです。

RXTable の selectAll を次のように置き換えます。

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

1 に答える 1

2

3 つの選択タイプのうち 2 つで機能する簡単なハック

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

How to select all text in a JFormattedTextField when it gets focus?のトリックを思い出しました。- 入力して編集を開始する場合は処理しません。この場合、コンテンツは (通常のテキストフィールドのように) 削除されませんが、新しいキーが追加されます。

于 2011-09-12T15:24:34.637 に答える