检测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.