检测JTextArea中的文本选择 [英] Detecting text selection in JTextArea
本文介绍了检测JTextArea中的文本选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个JTextArea并且正在检测是否有任何文本是选择,如果没有,那么其中两个菜单项是灰色的。我遇到的问题是,当我编译并打开应用程序时,我必须首先单击JTextArea,然后菜单项显示为灰色,如果我不这样做,即使没有选择文本也不会。我正在使用以下插入符号监听器。
I have a JTextArea and am deteching if any text is selection, if none is then two of the menu items are grayed out. The problem I have is, when I compile and open the application I have to click on the JTextArea first and then then the menu items are greyed out, if I don't they aren't even if no text is selected. I am using the following caret listener.
textArea.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent arg0) {
int dot = arg0.getDot();
int mark = arg0.getMark();
if (dot == mark) {
copy2.setEnabled(false);
cut1.setEnabled(false);
}
else{
cut1.setEnabled(true);
copy2.setEnabled(true);
}
}
});
推荐答案
你应该 setEnabled(false )
You should setEnabled(false)
on each of these menu items when you create them.
这篇关于检测JTextArea中的文本选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文