如何使JTextComponent的插入符号跳过所选文本? [英] How do I make the caret of a JTextComponent skip selected text?

查看:136
本文介绍了如何使JTextComponent的插入符号跳过所选文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多环境中原生文本字段的正常行为如下:

the normal behaviour of native text fields in many environments is as follows:

带文本abcdefg的文本字段。我用鼠标从左到右选择efg。插入符号现在落后于g。当我通过按左光标键一次将插入符号向左移动时,删除选择并且插入符号在e之前。当我在JTextField或JTextArea(在Mac OS上测试)中做同样的事情时,完全相同的事情会导致插入符号在g之前。

Textfield with text "abcdefg". I use the mouse to select "efg" from left to right. The caret is now behind "g". When I move the caret to the left by pressing the cursor left key once, the selection is removed and the caret is right before "e". When I do the same in a JTextField or JTextArea (tested on Mac OS) doing the exact same thing results in the caret being right before "g".

我知道如何我可以通过使用KeyListener并以每个组件注册它来以编程方式更改它,但我正在寻找一种方法来更改我的整个应用程序。那可能吗?有没有国旗,我找不到或者我不得不破解我的外表和感觉?

I know how I could change that programmatically by using a KeyListener and registering it with each component but I am looking for a way to change that for my entire application. Is that possible? Is there a Flag, I am not finding or do I have to hack my look and feel?

谢谢

推荐答案


我正在寻找一种方法来改变我的整个应用程序

I am looking for a way to change that for my entire application



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class CaretAction extends TextAction
{
    private boolean caretForward;

    public CaretAction(boolean caretForward)
    {
        super(caretForward ? "Caret Forward" : "Caret Backward");
        this.caretForward = caretForward;
    }

    public void actionPerformed(ActionEvent e)
    {
        JTextComponent textComponent = getFocusedComponent();

        int start = textComponent.getSelectionStart();
        int end = textComponent.getSelectionEnd();
        int offset = textComponent.getCaretPosition();

        if (start != end)
        {
            if (caretForward)
                offset = (offset == end) ? offset + 1 : end;
            else
                offset = (offset == start) ? offset -1 : start;
        }
        else
        {
            offset += (caretForward) ? 1 : -1;
        }

        offset = Math.max(offset, 0);
        offset = Math.min(offset, textComponent.getDocument().getLength());
        textComponent.setCaretPosition( offset );
    }

    private static void createAndShowUI()
    {
        JTextField textField1 = new JTextField(10);
        JTextField textField2 = new JTextField(10);

        JPanel panel = new JPanel();
        panel.add( textField1 );
        panel.add( textField2 );

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( panel );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );

        ActionMap map = (ActionMap)UIManager.get("TextField.actionMap");
        map.put(DefaultEditorKit.backwardAction, new CaretAction(false));
        map.put(DefaultEditorKit.forwardAction, new CaretAction(true));
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

您还需要更改JTextArea的ActionMap, JFormattedTextField ...

You would also need to change the ActionMap for JTextArea, JFormattedTextField ...

这篇关于如何使JTextComponent的插入符号跳过所选文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆