JTextArea - 仅在文档​​末尾启用编辑 [英] JTextArea - enable edit only at the end of document

查看:174
本文介绍了JTextArea - 仅在文档​​末尾启用编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 JTextArea 使用双击我可以在任何地方选择单词,但我不想启用编辑。这意味着文本只能在文本区域的末尾输入,而不能在两者之间的任何位置输入。

I use a JTextArea where using double click I can able to select the word at any place but I don't want to enable edit. Which means text can be entered only at the end of text area and not anywhere in between.

我尝试过如下鼠标监听器:

I have tried with mouse listeners like below:

@Override
public void mouseClicked(MouseEvent me) {
       if(SwingUtilities.isLeftMouseButton(me)){
        System.err.println("clicked");
        int pos = textArea.getCaretPosition();
        if(pos < textArea.getDocument().getLength()){
            textArea.setCaretPosition(textArea.getDocument().getLength());
        }
    }
   }

这使双击不选择这个词。我理解这是因为插入位置被移动到最后。但是我怎样才能做到这一点?

This makes double click not to select the word. I understand it is because caret position is moved to end. But how can I achieve this?

推荐答案

查看受保护的文本组件,允许您保护文档的多个区域不被更改。

Check out the Protected Text Component which allows you to protect multiple areas of a Document from change.

或者,如果您不需要能够选择任何受保护的文本而不是更简单的解决方案就是使用NavigationFilter:

Or if you don't need to be able to "select" any of the protected text than a simpler solution is to use a 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();
            }
        });
    }
}

这篇关于JTextArea - 仅在文档​​末尾启用编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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