Menu Bars and Menus A toplevel window can
Menu Bars and Menus
A top-level window can have a menu bar associated with it. This is implemented by the following classes: Menu. Bar, Menu, and Menu. Item. • A menu bar contains one or more Menu objects. • Each Menu object contains a list of Menu. Item objects. • Each Menu. Item object represents something that can be selected by the user. • Since Menu is a subclass of Menu. Item, a hierarchy of nested submenus can be created. • It is also possible to include checkable menu items. These are menu options of type Checkbox. Menu. Item and will have a check mark next to them when they are selected.
Creating a menu on Frame / Applet
How to Use Menus? public class Test. Menu extends Frame { public Test. Menu(String title){ super(title); Menu. Bar mb = new Menu. Bar(); set. Menu. Bar(mb); Menu m 1 = new Menu("Menu 1"); mb. add(m 1); Menu. Item mi 1_1 = new Menu. Item("Menu Item 1_1"); m 1. add(mi 1_1); m 1. add. Separator(); Menu. Item mi 1_2 = new Menu. Item("Menu Item 1_2"); m 1. add(mi 1_2); Menu m 2 = new Menu("Menu 2"); // mb. add(m 2); m 1. add(m 2); Menu. Item mi 2_1 = new Checkbox. Menu. Item("Menu Item 2_1"); m 2. add(mi 2_1); } … 4
Constructors for 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.
Constructors for Menu. Item( ) -Constructs a new Menu. Item with an empty label and no keyboard shortcut. Menu. Item(String label) -Constructs a new Menu. Item with the specified label and no keyboard shortcut. Menu. Item(String label, Menu. Shortcut s) -Create a menu item with an associated keyboard shortcut.
Methods of Menu. Item void set. Enabled(boolean enabled. Flag) -enables or disables a menu item. If the argument enabled. Flag is true, the menu item is enabled. If false, the menu item is disabled. boolean is. Enabled( ) -determines an item’s status. Returns true if the menu item on which it is called is enabled. Otherwise, it returns false. void set. Label(String new. Name) - changes the name of a menu item. String get. Label( ) -retrieve the current name of menu item.
Checkable Menu. Item • The Checkbox. Menu. Item 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.
Constructors for Checkable Menu. Item Checkbox. Menu. Item() -Create a check box menu item with an empty label. Checkbox. Menu. Item(String label) -Create a check box menu item with the specified label. Checkbox. Menu. Item(String label, boolean state) -Create a check box menu item with the specified label and state.
import java. awt. *; import java. awt. event. *; import java. applet. *; public class exp 31 extends Frame{ public static void main(String args[]){ exp 31 e=new exp 31(); e. set. Visible(true); e. set. Size(300, 200); Menu. Bar mbr=new Menu. Bar(); e. set. Menu. Bar(mbr); Menu mnu. Home=new Menu("Home"); Menu mnu. Insert=new Menu("Insert"); mbr. add(mnu. Home); mbr. add(mnu. Insert); Checkbox. Menu. Item Picture 1=new Checkbox. Menu. Item("Picture"); Menu. Item Paste 1=new Menu. Item("Paste"); mnu. Home. add(Paste 1); mnu. Insert. add(Picture 1); } } e. add. Window. Listener(new Window. Adapter(){ public void window. Closing(Window. Event e) { System. exit(0); }});
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.
• Dialog – A window for dialog box – Can be modal
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 Layout. Manager: Border. Layout • 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(parent. Frame, title. String, false); Dialog modal. Dialog = new Dialog(parent. Frame, title. String, true); AWT Components 13
Constructors for Dialog Box Dialog(Frame parent. Window) Constructs an initially invisible, modeless Dialog with the specified parent. Window Frame and an empty title. Dialog(Frame parent. Window, boolean modal) Constructs an initially invisible Dialog with the specified parent. Window Frame and modality and an empty title. Dialog(Frame parent. Window, String title) Constructs an initially invisible, modeless Dialog with the specified parent. Window Frame and title. Dialog(Frame parent. Window, String title, boolean modal) Constructs an initially invisible Dialog with the specified parent. Window Frame, title and modality.
Methods for Dialog Box String get. Title() -Gets the title of the dialog. void set. Title(String title) -Sets the title of the Dialog. boolean is. Modal() -Indicates whether the dialog is modal. void set. Modal(boolean modal) -Specifies whether this dialog should be modal. boolean is. Resizable() -Indicates whether this dialog is resizable by the user. void set. Resizable(boolean resizable) -Sets whether this dialog is resizable by the user.
import java. awt. *; import java. awt. event. *; public class Dialog. Example { private static Dialog d; Dialog. Example() { Frame f= new Frame(); d = new Dialog(f , "Dialog Example", true); d. set. Layout( new Flow. Layout() ); Button b = new Button ("OK"); /* b. add. Action. Listener ( new Action. Listener() { public void action. Performed( Action. Event e ) { Dialog. Example. d. set. Visible(false); } }); */ d. add( new Label ("Click button to continue. ")); d. add(b); d. set. Size(300, 300); d. set. Visible(true); } public static void main(String args[]) { new Dialog. Example(); } }
File. Dialog 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 File. Dialog.
Constructors for File. Dialog(Frame parent) Creates a file dialog for loading a file. File. Dialog(Frame parent, String title) Creates a file dialog window with the specified title for loading a file. File. Dialog(Frame parent, String title, int mode) Creates a file dialog window with the specified title for loading or saving a file. If mode is File. Dialog. LOAD then selecting a file for reading. If mode is File. Dialog. SAVE, then selecting a file for writing.
Methods for File. Dialog String get. Directory() Gets the directory of this file dialog. String get. File() Gets the selected file of this file dialog. int get. Mode() Indicates whether this file dialog box is for loading from a file or for saving to a file. void set. Mode(int mode) Sets the mode of the file dialog.
- Slides: 19