JTextField/JTextComponent 中的选择有限?

发布于 2024-12-05 01:11:04 字数 422 浏览 0 评论 0原文

考虑一个 JFormattedTextField(或者任何 JTextComponent,实际上),其中在字段的实际“文本”周围显示有一个前缀和一个后缀。

例如,双 3.5 将是字符串(通过格式化)“3.50”,其周围将是前缀“$”和后缀“”,用于显示文本“$ 3.50”。

显然,这很容易做到。但是,用户仍然可以选择前缀/后缀内的文本,因此他们可以想象删除部分或全部前缀/后缀。我希望用户受到限制,以便根本无法选择前缀/后缀(同时仍然是文本字段的一部分,因此没有 JLabels)。我几乎可以使用 CaretListener(或通过覆盖 setCaretPosition/moveCaretPosition)来完成此操作,这可以防止 Ca 选择整个字段,并且可以防止使用箭头键移动到前缀/后缀。但是,鼠标拖动和 Shift 箭头键仍然允许将选择移动到这些限制区域。

有什么想法吗?

Consider a JFormattedTextField (or any JTextComponent, really) wherein there is a prefix and a suffix displayed around what is the actual "text" of the field.

For instance, the double 3.5 would be the String (via formatting) "3.50" around which would be the prefix "$ " and the suffix "", for a display text of "$ 3.50".

Clearly, this is simple to do. However, the user is still allowed to select text within the prefix/suffix, so they could conceivably delete part or all of the prefix/suffix. I would prefer the user be restricted such that the prefix/suffix cannot be selected at all (while still part of the text field, so no JLabels). I can almost accomplish this with a CaretListener (or by overriding setCaretPosition/moveCaretPosition), which prevents a C-a from selecting the entire field, and it prevents using the arrow keys to move into the prefix/suffix. However, mouse dragging and shift-arrow keys still allows the selection to move into these restricted areas.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁对谁错谁最难过 2024-12-12 01:11:04

您可以为此使用 NavigationFilter。

下面是一个帮助您入门的示例:

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

public class NavigationFilterPrefixWithBackspace extends NavigationFilter
{
    private int prefixLength;
    private Action deletePrevious;

    public NavigationFilterPrefixWithBackspace(int prefixLength, JTextComponent component)
    {
        this.prefixLength = prefixLength;
        deletePrevious = component.getActionMap().get("delete-previous");
        component.getActionMap().put("delete-previous", new BackspaceAction());
        component.setCaretPosition(prefixLength);
    }

    public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias)
    {
        fb.setDot(Math.max(dot, prefixLength), bias);
    }

    public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias)
    {
        fb.moveDot(Math.max(dot, prefixLength), bias);
    }

    class BackspaceAction extends AbstractAction
    {
        public void actionPerformed(ActionEvent e)
        {
            JTextComponent component = (JTextComponent)e.getSource();

            if (component.getCaretPosition() > prefixLength)
            {
                deletePrevious.actionPerformed( null );
            }
        }
    }

    public static void main(String args[]) throws Exception {

        JTextField textField = new JTextField("Prefix_", 20);
        textField.setNavigationFilter( new NavigationFilterPrefixWithBackspace(7, textField) );

        JFrame frame = new JFrame("Navigation Filter Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(textField);
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

我相信这就是 JFormattedTextField 的工作原理。所以我不确定您是否可以将其与格式化文本字段一起使用,因为它可能会替换默认行为。

You can use a NavigationFilter for this.

Here is an example to get you started:

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

public class NavigationFilterPrefixWithBackspace extends NavigationFilter
{
    private int prefixLength;
    private Action deletePrevious;

    public NavigationFilterPrefixWithBackspace(int prefixLength, JTextComponent component)
    {
        this.prefixLength = prefixLength;
        deletePrevious = component.getActionMap().get("delete-previous");
        component.getActionMap().put("delete-previous", new BackspaceAction());
        component.setCaretPosition(prefixLength);
    }

    public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias)
    {
        fb.setDot(Math.max(dot, prefixLength), bias);
    }

    public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias)
    {
        fb.moveDot(Math.max(dot, prefixLength), bias);
    }

    class BackspaceAction extends AbstractAction
    {
        public void actionPerformed(ActionEvent e)
        {
            JTextComponent component = (JTextComponent)e.getSource();

            if (component.getCaretPosition() > prefixLength)
            {
                deletePrevious.actionPerformed( null );
            }
        }
    }

    public static void main(String args[]) throws Exception {

        JTextField textField = new JTextField("Prefix_", 20);
        textField.setNavigationFilter( new NavigationFilterPrefixWithBackspace(7, textField) );

        JFrame frame = new JFrame("Navigation Filter Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(textField);
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

I believe this is a how a JFormattedTextField works. So I'm not sure if you can use this with a formatted text field as is may replace the default behaviour.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文