There are three steps to creating a menu:
- Create the MenuBar
- Create the Menus
- Create the MenuItems
A MenuBar is added to a frame.
A Menu is add to the MenuBar
A MenuItem is added to the Menupublic class JavaPad { private Frame frame = new Frame("JavaPad"); private MenuBar menuBar = new MenuBar(); private Menu fileMenu = new Menu("File"); private MenuItem fileOpen = new MenuItem("Open"); public JavaPad() { frame.setMenuBar(menuBar); menuBar.add(fileMenu); fileMenu.add(fileOpen); } }
FileDialogs
File dialogs are used for setting the file name and path - nothing more.
private FileDialog openFile = new FileDialog(frame, "Open File", FileDialog.LOAD); private FileDialog saveFile = new FileDialog(frame, "Save File", FileDialog.SAVE); openFile.show(); fileName = openFile.getFile(); pathName = openFile.getDirectory();If getFile() returns null, that means no file was selected.