简体   繁体   English

如何禁用在 JTextArea 中突出显示的功能

[英]How to disable the ability to highlight in JTextArea

I am looking for a way to disable the ability to highlight in the JTextArea.我正在寻找一种方法来禁用在 JTextArea 中突出显示的功能。

Currently this is my JTextArea:目前这是我的 JTextArea:

textArea1 = new JTextArea();
textArea1.setBorder(BorderFactory.createLineBorder(Color.black, 1));
DefaultCaret caret = (DefaultCaret) textArea1.getCaret(); // this line and the line below was inspired by a comment found here: https://stackoverflow.com/questions/15623287/how-to-always-scroll-to-bottom-of-text-area
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
textArea1.setEditable(false);
JScrollPane scrollPane1 = new JScrollPane(textArea1);

I use the DefaultCaret class to always have the JTextArea viewpoint pushed to the bottom at all times and textArea1.setEditable(false) to stop end users being able to type anything in.我使用 DefaultCaret class 始终将 JTextArea 视点推到底部,并textArea1.setEditable(false)阻止最终用户输入任何内容。

However, if I highlight text the DefaultCaret method just stops working.但是,如果我突出显示文本,DefaultCaret 方法就会停止工作。 Once you highglight the text, the JTextArea does not stick to the bottom anymore.突出显示文本后,JTextArea 不再粘在底部。

Once you highglight the text, the JTextArea does not stick to the bottom anymore.突出显示文本后,JTextArea 不再粘在底部。

The problem is that automatic scrolling will only happen when the caret is at the end of the Document.问题是只有当插入符号位于文档末尾时才会发生自动滚动。

Highlighting text isn't strictly the problem.突出显示文本并不是严格意义上的问题。 The problem is the user clicking the mouse anywhere in the text area since that will change the caret position.问题是用户在文本区域的任意位置单击鼠标,因为这将更改插入符号 position。

So if you want automatic scrolling to always be enabled the proper solution would be to remove the MouseListener and MouseMouseMotionListener from the text area to prevent all mouse related activity.因此,如果您希望始终启用自动滚动,正确的解决方案是从文本区域中删除MouseListenerMouseMouseMotionListener以防止所有与鼠标相关的活动。

Or as a simple hack you could always reset the caret position of the Document:或者作为一个简单的技巧,您可以随时重置文档的插入符号 position:

textArea.addMouseListener( new MouseAdapter()
{
    @Override
    public void mouseReleased(MouseEvent e)
    {
        JTextArea textArea = (JTextArea)e.getSource();
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }
});

Edit:编辑:

Lets assume you have multiple text areas to have the same functionality.假设您有多个文本区域具有相同的功能。 You don't need to create a custom listener for each text area.您不需要为每个文本区域创建自定义侦听器。 The listener can be shared.监听器可以共享。 The code could be written as:代码可以写成:

    MouseListener ml = new new MouseAdapter()
    {
        @Override
        public void mouseReleased(MouseEvent e)
        {
            JTextArea textArea = (JTextArea)e.getSource();
            textArea.setCaretPosition(textArea.getDocument().getLength());
        }
    };

    textArea1.addMouseListener(ml);
    textArea2.addMouseListener(ml);

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

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