简体   繁体   English

如何使JTextField的一部分不可编辑

[英]How to make part of a JTextField uneditable

I wanted to develop a console-like interface, similar to IDLE. 我想开发一个类似于IDLE的类似于控制台的界面。 That involved determining how to prevent a certain part of the text in a JTextField from being edited. 这涉及确定如何防止JTextField中的文本的某些部分被编辑。 For example: 例如:

>>> help

Where the ">>> " is uneditable. “ >>>”不可编辑的地方。 The caret must never move behind a certain position, and the text behind that position cannot be edited in any way. 插入符号绝对不能移到某个位置的后面,并且该位置后面的文本不能以任何方式进行编辑。

I looked at NavigationFilter, but it doesn't seem to prevent keyboard driven manipulation of the caret. 我看了一下NavigationFilter,但它似乎并不能阻止键盘驱动的插入符号操作。

This shows how to do it with a NavigationFilter : 这显示了如何使用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);
    }

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

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

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

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

    private static void createAndShowUI()
    {
        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);
    }

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

Spent a little while figuring this out, so I thought I would share my solution for anyone else with the same dilemma. 在弄清楚这一点时花了一点时间,所以我想我将与其他遇到同样困境的人分享我的解决方案。 I don't know if it's optimal, but it does seem to work. 我不知道它是否是最佳选择,但它似乎确实有效。

It prevents the user from using backspace behind the postion n . 它防止用户使用位置n后面的退格键。 It also moves the caret back to n for any other events, such as (illegally) changing the caret position with the arrow keys or mouse. 对于任何其他事件,它也会将插入符号移回n位置,例如(非法)使用箭头键或鼠标更改插入符号的位置。 Finally, it resets the text and caret position after a entry is processed. 最后,它会在处理完条目后重置文本和插入符号的位置。

EDIT: While I'm leaving this answer here for posterity, see the accepted answer for the best way to solve this problem. 编辑:虽然我将这个答案留在后头,但请参阅已接受的答案,以解决该问题的最佳方法。

    JTextField in = new JTextField();
    final String protectMe = ">>> "; //protect this text
    final int n = protectMe.length();
    in.setText(protectMe);
    in.setCaretPosition(n);

    in.addCaretListener(new CaretListener()
    {
        @Override
        public void caretUpdate(CaretEvent e)
        {
            if (e.getDot() < n)
            {
                if (!(in.getText().length() < n))
                    in.getCaret().setDot(n);
            }
        }
    });

    in.addKeyListener(new KeyListener()
    {

        @Override
        public void keyPressed(KeyEvent arg0)
        {
            if (in.getCaret().getDot() <= n)
            {
                in.setText(protectMe + in.getText().substring(n));
                arg0.consume();
            }
        }

        @Override
        public void keyReleased(KeyEvent arg0){}

        @Override
        public void keyTyped(KeyEvent arg0){}
    });
    in.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent evt)
        {
            String input = in.getText().substring(n).trim(); 

            //do something

            in.setText(protectMe);
            in.setCaretPosition(n);
        }
    });

As usual, let me know if there's anything I missed! 和往常一样,让我知道我是否错过任何事情!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM