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);
}
}
});
You should setEnabled(false)
on each of these menu items when you create them.