Menu Bars and Menus.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces (Part IV)
Advertisements

Simple Java I/O Part I General Principles. 2 Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
Swing Components III Chapter 12 - Student JFrame, Component Methods.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
Io package as Java’s basic I/O system continue’d.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
1 Windows, Networking and other Tidbits Chapter Fourteen.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java Programming 1 Java Programming II Events, AWT, and Swing.
C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
Event Handling Mohanraj S AP / IT Angel College of Engg & Tech.,
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
1 Lecture 6 Using AWT controls, Layout Managers, and Menus.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Csc Basic Graphical User Interface (GUI) Components.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
Computer Science 209 GUIs Model/View/Controller Layouts.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Basics of GUI Programming Chapter 11 and Chapter 22.
CSI 3125, Preliminaries, page 1 AWT Control. CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
TCU CoSc Programming with Java The JFrame Class.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
1 IM103 week 8 (C&K ch17, p412) Advanced graphic programming Learning objectives By the end of this chapter you should be able to:  create dialogue windows.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Java and AWT CPS 470 Spring 1998 Laura Campbell.
Customizing Menus and Toolbars CHAPTER 12 Customizing Menus and Toolbars.
C13b, AWT cont.. Panel Container that acts like a Component Can be added into other components (like Frames, other Panels) private Panel makeScrollBars()
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 4: Laying out the GUI Useful containers –Panels –Frames –Dialogs Layout mechanisms –FlowLayout –BorderLayout –GridLayout –CardLayout Menus.
A Quick Java Swing Tutorial
CSC 205 Programming II Lecture 5 AWT - I.
Excel Tutorial 8 Developing an Excel Application
Fundamental of Java Programming Abstract Window Toolkit
Advanced GUIs II CS Lecture
GUIs Model/View/Controller Layouts
TOPICS Labels Using Buttons Applying Check Boxes CheckBox Group
Java Swing.
A Quick Java Swing Tutorial
AWT Components.
Abstract Window ToolKit (AWT)
Important terms Black-box testing White-box testing Regression testing
GUI Programming III: Events
Important terms Black-box testing White-box testing Regression testing
AWT.
Predefined Dialog Boxes
AWT Components and Containers
Ch. No Name Marks 01 AWT & SWING EVENT HANDLING NETWORKING
Graphics Programming - Frames
A Quick Java Swing Tutorial
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Programming Graphical User Interface (GUI)
Advanced GUIs II CS Lecture
Graphical User Interface
Presentation transcript:

Menu Bars and Menus

A top-level window can have a menu bar associated with it 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.

Creating a menu on Frame / Applet

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); } …

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.

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.

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.

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.

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.

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); }}); }

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 Default LayoutManager: BorderLayout 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); AWT Components

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.

Methods for Dialog Box String getTitle() -Gets the title of the dialog. void setTitle(String title) -Sets the title of the Dialog. boolean isModal() -Indicates whether the dialog is modal. void setModal(boolean modal) -Specifies whether this dialog should be modal. boolean isResizable() -Indicates whether this dialog is resizable by the user. void setResizable(boolean resizable) -Sets whether this dialog is resizable by the user.

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);       }      public static void main(String args[])       {           new DialogExample();       }   } 

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.

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.

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.