Am designing a Notepad application in java, using AWT. Now, i have created the MenuBar as well as the MenuItems, but the thing i can't get is how do i deactivate specific MenuItem, like if we haven't wrote anything in the TextArea, in the Edit Section the cut and copy option, as well as Undo option remains Deactivate. Is there any method in java to do that stuff? Am doing this Using AWT and not Swing.
Asked
Active
Viewed 414 times
1 Answers
0
Got the answer, after trials and errors and surfing. While in the constructor while initializing them we will keep their initial values as "false". Hence when the texteditor opens there is nothing on the screen. So they will appear disabled and then registering TextArea object with KeyListener will do the job of enabling that menuItems. ;)
constructor()
{
undo=new MenuItem("Undo",new MenuShortcut(90));
edit.add(undo);
un.setEnabled(false);
cut=new MenuItem("Cut",new MenuShortcut(88));
edit.add(cut);
cut.setEnabled(false);
copy=new MenuItem("Copy",new MenuShortcut(67));
edit.add(copy);
copy.setEnabled(false);
paste=new MenuItem("Paste",new MenuShortcut(86));
ed.add(paste);
selectAll=new MenuItem("Select All",new MenuShortcut(65));
ed.add(selectAll);
selectAll.setEnabled(false);
textArea.addKeyListener();
}
public void keyTyped(KeyEvent arg0)
{
if(textArea.getText()!=null)
{
cut.setEnabled(true);
copy.setEnabled(true);
undo.setEnabled(true);
selectAll.setEnabled(true);
find.setEnabled(true);
}
}

Parth Chokshi
- 642
- 4
- 16