Menus, Dialogs and the Notepad App

Site Admin · 11 Sep 2026 · 1 views

Menus

Menus need a MenuBar attached to the Frame, containing Menu objects with MenuItems:

MenuBar bar = new MenuBar();
Menu file = new Menu("File");
file.add(new MenuItem("New"));
file.add(new MenuItem("Open"));
file.add(new MenuItem("Exit"));
bar.add(file);
setMenuBar(bar);

AWT menu demo

Menus can nest (sub-menus) and items fire ActionEvent just like buttons.

Nested menus demo

Sub menu demo

File and Print Dialogs

FileDialog opens native open/save dialogs without extra code:

FileDialog fd = new FileDialog(this, "Open", FileDialog.LOAD);
fd.setVisible(true);
String file = fd.getFile();

AWT file dialog

AWT print dialog

Edit Operations

A TextArea supports clipboard operations directly with cut(), copy() and paste(), which are wired into a File/Edit menu.

Cut copy paste demo

The Notepad Demo

Combining a menu bar, file dialog and text area builds a small Notepad editor - open a file into the TextArea, edit it, and save it back.

Simple notepad app in AWT

Key Points

  • MenuBar > Menu > MenuItem is the menu structure.
  • FileDialog gives native open/save without extra code.
  • cut(), copy() and paste() make an editor trivial to build.
Share this post:

Comments (0)

Please login or register to comment.