如何禁用在JTextArea中突出显示的功能 [英] How to disable the ability to highlight in JTextArea

查看:87
本文介绍了如何禁用在JTextArea中突出显示的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种禁用JTextArea中突出显示功能的方法.

I am looking for a way to disable the ability to highlight in the JTextArea.

当前,这是我的JTextArea:

Currently this is my 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);

我使用DefaultCaret类始终将JTextArea视点始终推到底部,并使用textArea1.setEditable(false)阻止最终用户输入任何内容.

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方法将停止工作.突出显示文本后,JTextArea不再停留在底部.

However, if I highlight text the DefaultCaret method just stops working. 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.

问题在于,仅当插入标记位于文档末尾时,才会自动滚动.

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.

因此,如果要始终启用自动滚动,则正确的解决方案是从文本区域中删除MouseListenerMouseMouseMotionListener,以防止所有与鼠标相关的活动.

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.

或者作为简单的技巧,您可以随时重置文档的插入符位置:

Or as a simple hack you could always reset the caret position of the Document:

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

让我们假设您有多个具有相同功能的文本区域.您无需为每个文本区域创建自定义侦听器.可以共享侦听器.该代码可以写为:

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);

这篇关于如何禁用在JTextArea中突出显示的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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