Understanding Menu Bars and Menus: a Comprehensive Guide
Learn about implementing menu bars and menus in Java including subclasses like Menu, MenuItem, and CheckboxMenuItem. Explore how to create menus in a top-level window, on a Frame/Applet, and constructors/methods available for Menu and MenuItem objects. Dive deep into enabling/disabling menu items, changing item names, and creating checkable menu items in Java with detailed explanations and code snippets.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
Menu Bars and Menus SACHIN KHARADE
A top-level window can have a menu bar associated with it. This is implemented by the following classes: MenuBar, Menu, and MenuItem. A menu bar contains one or more Menu objects. Each Menu object contains a list of MenuItem objects. Each MenuItem object represents something that can be selected by the user. Since Menu is a subclass of MenuItem, a hierarchy of nested submenus can be created. It is also possible to include checkable menu items. These are menu options of type CheckboxMenuItem and will have a check mark next to them when they are selected. SACHIN KHARADE
Creating a menu on Frame / Applet SACHIN KHARADE
How to Use Menus? public class TestMenu extends Frame { public TestMenu(String title){ super(title); MenuBar mb = new MenuBar(); setMenuBar(mb); Menu m1 = new Menu("Menu 1"); mb.add(m1); MenuItem mi1_1 = new MenuItem("Menu Item 1_1"); m1.add(mi1_1); m1.addSeparator(); MenuItem mi1_2 = new MenuItem("Menu Item 1_2"); m1.add(mi1_2); } Menu m2 = new Menu("Menu 2"); // mb.add(m2); m1.add(m2); MenuItem mi2_1 = new CheckboxMenuItem("Menu Item 2_1"); m2.add(mi2_1); SACHIN KHARADE
Constructors for Menu Menu( ) -Creates an empty menu. Menu(String label) -Constructs a new menu with the specified label. Menu(String label, boolean removable) -Constructs a new menu with the specified label. If removable is true, the pop-up menu can be removed and allowed to float free. Otherwise, it will remain attached to the menu bar. SACHIN KHARADE
Constructors for MenuItem MenuItem( ) -Constructs a new MenuItem with an empty label and no keyboard shortcut. MenuItem(String label) -Constructs a new MenuItem with the specified label and no keyboard shortcut. MenuItem(String label, MenuShortcut s) -Create a menu item with an associated keyboard shortcut. SACHIN KHARADE
Methods of MenuItem void setEnabled(boolean enabledFlag) -enables or disables a menu item. If the argument enabledFlag is true, the menu item is enabled. If false, the menu item is disabled. boolean isEnabled( ) -determines an item s status. Returns true if the menu item on which it is called is enabled. Otherwise, it returns false. void setLabel(String newName) - changes the name of a menu item. String getLabel( ) -retrieve the current name of menu item. SACHIN KHARADE
Checkable MenuItem The CheckboxMenuItem class represents a check box which can be included in a menu. Selecting the check box in the menu changes control's state from on to off or from off to on. SACHIN KHARADE
Constructors for Checkable MenuItem CheckboxMenuItem() -Create a check box menu item with an empty label. CheckboxMenuItem(String label) -Create a check box menu item with the specified label. CheckboxMenuItem(String label, boolean state) -Create a check box menu item with the specified label and state. SACHIN KHARADE
import java.awt.*; import java.awt.event.*; import java.applet.*; public class exp31 extends Frame{ public static void main(String args[]){ exp31 e=new exp31(); e.setVisible(true); e.setSize(300,200); MenuBar mbr=new MenuBar(); e.setMenuBar(mbr); Menu mnuHome=new Menu("Home"); Menu mnuInsert=new Menu("Insert"); mbr.add(mnuHome); mbr.add(mnuInsert); CheckboxMenuItem Picture1=new CheckboxMenuItem("Picture"); MenuItem Paste1=new MenuItem("Paste"); mnuHome.add(Paste1); mnuInsert.add(Picture1); } } e.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); }}); SACHIN KHARADE
Dialog Boxes Dialog control represents a top-level window with a title and a border used to take some form of input from the user. Dialog boxes may be modal or modeless. When a modal dialog box is active, all input is directed to it until it is closed. i.e. you cannot access other parts of program until you have closed the dialog box. When a modeless dialog box is active, input focus can be directed to another window in your program. SACHIN KHARADE
Dialog A window for dialog box Can be modal SACHIN KHARADE
Dialog Class Major Purposes A simplified Frame (no cursor, menu, icon image). A modal Dialog that freezes interaction with other AWT components until it is closed Default LayoutManager: BorderLayout Creating and Using Similar to Frame except constructor takes two additional arguments: the parent Frame and a boolean specifying whether or not it is modal Dialog dialog = new Dialog(parentFrame, titleString, false); Dialog modalDialog = new Dialog(parentFrame, titleString, true); SACHIN KHARADE
Constructors for Dialog Box Dialog(Frame parentWindow) Constructs an initially invisible, modeless Dialog with the specified parentWindow Frame and an empty title. Dialog(Frame parentWindow, boolean modal) Constructs an initially invisible Dialog with the specified parentWindow Frame and modality and an empty title. Dialog(Frame parentWindow, String title) Constructs an initially invisible, modeless Dialog with the specified parentWindow Frame and title. Dialog(Frame parentWindow, String title, boolean modal) Constructs an initially invisible Dialog with the specified parentWindow Frame, title and modality. SACHIN KHARADE
Methods for Dialog Box String getTitle() -Gets the title of the dialog. voidsetTitle(String title) -Sets the title of the Dialog. booleanisModal() -Indicates whether the dialog is modal. voidsetModal(boolean modal) -Specifies whether this dialog should be modal. booleanisResizable() -Indicates whether this dialog is resizable by the user. void setResizable(boolean resizable) -Sets whether this dialog is resizable by the user. SACHIN KHARADE
public static void main(String args[]) { new DialogExample(); } import java.awt.*; import java.awt.event.*; public class DialogExample { private static Dialog d; DialogExample() { Frame f= new Frame(); d = new Dialog(f , "Dialog Example", true); d.setLayout( new FlowLayout() ); Button b = new Button ("OK"); /* b.addActionListener ( new ActionListener() { public void actionPerformed( ActionEvent e ) { DialogExample.d.setVisible(false); } }); */ d.add( new Label ("Click button to continue.")); d.add(b); d.setSize(300,300); d.setVisible(true); } } SACHIN KHARADE
FileDialog FileDialog control represents a dialog window from which the user can select a file. To display a file dialog, you must instantiate an object of type FileDialog. SACHIN KHARADE
Constructors for FileDialog FileDialog(Frame parent) Creates a file dialog for loading a file. FileDialog(Frame parent, String title) Creates a file dialog window with the specified title for loading a file. FileDialog(Frame parent, String title, int mode) Creates a file dialog window with the specified title for loading or saving a file. If mode is FileDialog.LOAD then selecting a file for reading. If mode is FileDialog.SAVE, then selecting a file for writing. SACHIN KHARADE
Methods for FileDialog String getDirectory() Gets the directory of this file dialog. String getFile() Gets the selected file of this file dialog. int getMode() Indicates whether this file dialog box is for loading from a file or for saving to a file. void setMode(int mode) Sets the mode of the file dialog. SACHIN KHARADE