首页 > 解决方案 > How to disable the ability to click on the JTextArea

问题描述

I need to disable the ability to click your mouse in the JTextArea.

Below is what I currently have for 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)

Once you press on the JTextArea with your mouse, it almost stops the whole DefaultCaret method from working. I thought previously it was because you could highlight but that was not the case.

The DefaultCaret method causes the JTextArea to always be at the bottom when new text gets added to the textarea.

标签: javaswinguser-interfacejtextarea

解决方案


textArea1.setEnabled(false); // text area can neither be entered nor text selected

在此处输入图像描述

插入符号显示在默认位置,是第一个可聚焦的组件。


推荐阅读