1 Lecture 6 Using AWT controls, Layout Managers, and Menus.

Slides:



Advertisements
Similar presentations
Objectives The objectives of this chapter are: To discuss the classes present in the java.awt package To understand the inheritance hierarchy of the AWT.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Jan AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
LAB SESSION 10 *LAYOUT MANAGER *LISTENERS. Laying the components manually by using a null layout is tedious. Each container object has a layout manager.
Components. Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent –TextArea –TextField.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Introduction To Form Builder
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
12-Jul-15 Components. 2 Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent TextArea TextField.
Chapter 20 Event Handling. Event handling Is the Core of successful applet Programming For the user to interact with a GUI, the underlying operating system.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Using Inheritance to Customize Frames Use inheritance for complex frames to make programs easier to understand Design a subclass of JFrame Store the components.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 15 Advanced Tables.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
WaveMaker Visual AJAX Studio 4.0 Training Basics: Building Your First Application Designer Basics.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Using AWT Controls, Layout Managers, and Menus Controls are components that allow a user to interact with your application in various ways A layout manager.
CHAPTER:07 JAVA IDE PROGRAMMING-II Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Microsoft Access 2000 Presentation 3 Creating Databases Part II (Creating Forms)
Computer Programming with JAVA Chapter 7. Event-Driven Programming Using the AWT Event-Driven Programming GUIs and the AWT Simple Window Interfaces Components,
May 12, 1998CS102-01Lecture 7-3 Building GUIs in Java I CS Lecture 7-3 A picture's worth a thousand words.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
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.
AWT Package. Java GUI classes are contained in the java.awt package. Java GUI classes are contained in the java.awt package. A graphical Java program.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
CSI 3125, Preliminaries, page 1 AWT Control. CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Java Swing Controls. JButton One of the most commonly used swing component is Push Button or simply button which is created through JButton class of Swing.
1 Lecture 8: User Interface Components with Swing.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Chapter 3: Widgets for a GUI General Component methods Useful widgets classes –Text classes Label TextField TextArea –Active widgets Button Checkbox Choice.
AWT AWT stands for Abstract Windowing Toolkit. It contains all classes to write the program that interface between the user and different windowing toolkits.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
CSC 205 Programming II Lecture 5 AWT - I.
Fundamental of Java Programming Abstract Window Toolkit
Christopher Budo, Davis Nygren, spencer franks, Luke miller
TOPICS Labels Using Buttons Applying Check Boxes CheckBox Group
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
AWT Controls,Layout Manager,Menu and Menubar
AWT Components and Containers
Ch. No Name Marks 01 AWT & SWING EVENT HANDLING NETWORKING
Introducing the AWT: Working with Windows, Graphics, and Text
OBJECT ORIENTED PROGRAMMING
Menu Bars and Menus.
AWT Components Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e.
A picture's worth a thousand words
Presentation transcript:

1 Lecture 6 Using AWT controls, Layout Managers, and Menus

2 Introduction Controls are components that allow a user to interact with your application in various ways A layout manager automatically positions components within a container Frame window can include a standard-style menu bar. Each entry in a menu bar activates a drop-down menu of options from which user can choose

3 Control fundamentals The Awt support the following types of controls: Labels Push buttons Check boxes Lists Scroll bars Text editing These controls are subclass of components

4 Adding and removing controls To include a control in a window, we have to First create an instance of the desired control Then add it to a window by calling add() (defined by Container) i.e. Component add (Component compObj) Once a control has been added, it will automatically be visible whenever its parent window is displayed We can remove control from a window by calling void remove(Component obj) We can remove all controls by calling removeAll() Except labels (passive control), all controls generate events when they are accessed by the user

5 Labels A label is an object of type Label It contains a String which it displays Label defines the following constructors: Label() // create blank label Label (String str) // create label with the string in left-justified Label (String str, int how) // create label with the string using alignment specified by how The value of how must be one of the three constants: Label.LEFT, Label.Right or Label.CENTER We can set or change the text by setText(String str) method Current label can be obtained by String getText() method Methods are also available like setAlignment() or getAlignment (See LabelDemo for example)

6 Buttons Push buttons are objects of type Button Button() Button(String str) // str will label Label can be set by setLabel(String str) and label can be retrieved by String getLabel() Each time a button is pressed, an action event is generated, which is sent to registered listeners Each listener implements the ActionListener interface That interface defines the actionPerformed() method, which is called when an event occurs An ActionEvent object is supplied as an argument

7

8 Buttons (contd.) Which button has been pushed can be identified by a reference to the button that generated the event or by a reference to the label of the button The reference to the button can be obtained by getSource() method of ActionEvent object The label can be obtained by calling getActionCommand() method of ActionEvent object Button references can be stored into an array of buttons Example: ButtonDemo and ButtonList

9 Check Boxes A control that is used to turn an option on or off Check box can be used individually or as part of a group Check boxes are objects of the Checkbox class Checkbox() //unchecked checkbox with blank label Checkbox(String str) // unchecked but have a label Checkbox(String str, boolean on) //can be checked and have a label Checkbox(String str, boolean on, CheckboxGroup cbGroup) Checkbox(String str, CheckboxGroup cbGroup, boolean on) //can be included in a group if cbGroup is not null

10 Check Boxes (contd.) Current state of a check box can be set by setState(boolean on) or retreived by boolean getState() Each time a check box is selected or deselected, an item event is generated Each listener implements the ItemListener interface that defines itemStateChanged() method An ItemEvent object is supplied as the argument to this method, which contain information about the event Example: CheckboxDemo

11 Check Box Group Mutually exclusive check boxes in which one and only one check box in the group can be checked at a time Also called radio buttons First define a check box group by creating an object of type CheckboxGroup Then mention the group name in the check box constructor Which check box is selected can be determined by Checkbox getSelectedCheckbox() Any chekbox can be set by setSelectedCheckbox(Checkbox which) Example: CBGroup

12 Choice Controls Choice class is used to create a pop-up list of items from which user may choose Each item in the list is a string that appears as a left- justified label in the order it is added to the Choice object void addItem (String name) void add(String name) Which item is selected can be can be determined by String getSelectedItem() or int getSelectedIndex() Number of items can be determined by int getItemCount()

13 Choice Controls (contd.) Any item can be set by select (int index) or select (String name) Any item name can be found by index by calling String getItem(int index) Choice control creates ItemEvent object Listeners implement ItemListener interface, that defines itemStateChanged() method Example: ChoiceDemo

14 Lists List class provides a compact, multiple-choice, scrolling selection list List object can be constructed to show any number of choices in the visible window List() //allow one item to be selected List(int numRows) //specifies number of rows List(int numRows, boolean multipleSelect) //multiple item can be selected To add a slection to the list, call add(String name) or add(String name, int index) Indexing begin at 0and –1 indicates end of the list

15 Lists (contd.) Which item is/are currently selected can be determined by String getSelectedItem(), int getSelectedIndex() Or String[ ] getSelectedItems(), int[ ] getSelectedIndexes Similar methods available like select(), getItem(), getItemCount() etc For double clicking an item, list control produce ActionEvent type object Implement ActionListener interface, which has actionPerformed() method Object’s getActionCommand() is used to track which item is double-clicked

16 Lists (contd.) For single clicking an item, list control produce ItemEvent type object Implement ItemListener interface, which has itemStateChanged() method Object’s getStateChanged() is used to track whether any item is selected or deselected Object’s getItemSelectable() is used to track which item triggered this event Example: Listdemo

17 Scroll Bars This control is used to select continuous values between a specified minimum and maximum It may be oriented horizontally or vertically Each end has an arrow, click on it move current value of the scroll bar one unit in the direction The current value of the scroll bar relative to its minimum and maximum values is indicated by the slider box (or thumb) for the scroll bar Slider can be dragged to a new position By mouse click, slider can jump by more than 1 (like page up and page down)

18 Scroll Bars (contd.) Scroll bars are encapsulated by the class Scrollbar Scrollbar() //create vertical scroll bar Scrollbar(int style) //style is Scrollbar.VERTICAL or Scrollbar.HORIZONTAL Scrollbar(int style, int initialValue, int thumbSize, int min, int max) //specifies initial position, height of thumb, minimum and maximum position If Scroll bar is created by one of the first two constructors, then its other parameters must be set by setValues(int initialValue, int thumbSize, int min, int max)

19 Scroll Bars (contd.) getValue() is used to obtain the current value of the scroll bar. setValue(int newValue) is used to set current value Minumim and maximum values can be obtained by getMinimum() and getMaximum() By default increment or decrement is 1. It can be changed by setUnitIncrement(int newIncr) By default, page-up and page-down increments are 10. It can be changed by setBlockIncrement(int newIncr)

20 Scroll Bars (contd.) Interaction with a scroll bar creates AdjustmentEvent object Its getAdjustmentType() is used to determine type of adjustment (i.e. BLOCK_DECREMENT, TRACK, UNIT_INCREMENT etc) To handle this event, we need to implement AdjustmentListener interface Example: SBDemo

21 TextField/Edit control TextField is a single-line text-entry and a subclass of TextComponent Textfield defines the following constructors: TextField() //default text field TextField(int numChars) //wide specified TextField(String str) //default string in the text field TextField(String str, int numChars) Method to obtain the current string is String getText() To set a text, call setText(String str) A portion of the text under selection can be obtained by String getSelectedText() To select a portion of a text, call select(int startIndex,int endIndex)

22 TextField/Edit control (contd.) Editibility of a text field can be set by setEditable(boolean canEdit) Text can be modified or not is checked by boolean isEditable() We can disable the echoing of the characters and display another character by setEchoChar(char ch) Similar methods are boolean echoCharIsSet() and char getEchoChar() TextField generates ActionEvent object Example: TextFieldDemo

23 TextArea It is a multiline editor TextArea() TextArea(int numLines, int numChars) //height specified TextArea(String str) TextArea(String str,int numLines, int numChars) TextArea(String str,int numLines,int numChars,int sBars) //specify scroll bar to have with it TextArea is a subclass of TextComponent, so it supports the getText(), setText(), getSelectedText(), select(), isEditable(), setEditable() etc. methods

24 TextArea (contd.) TextArea adds the following methods append(String str) //appends str at the end of current text insert(String str, int index) //insert str at specified index replaceRange(String str, int startIndex, int endIndex) TextArea generates got-focus and lost- focus events Example: TextAreaDemo

25 Understanding Layout Managers Layout manager automatically arranges controls within a window by using some types of algorithm Until now, we have used he default layout manager Placing controls in a window by hand is not feasible, because: It is very tedious Sometimes the width and height information is not yet available

26 Layout Managers (contd.) Each container object has a layout manager associated with it. A layout manager is an instance of any class that implements the LayoutManager interface The layout manager is set by setLayout(LayoutManager layoutObj) layoutObj is a reference to the desired layout manager If layoutObj is set null, we have to position components manually

27 Layout Managers (contd.) The layout manager is notified each time a component is added to a container Whenever a container needs to be resized, the layout manager is consulted via its minimumLayoutSize() and preferredLayoutSize() methods Each component that is being managed by a layout manager contains the getPreferredSize() and getMinimumSize() methods The layout manager will honor these requests if at all possible, maintaining the integrity of the layout policy

28 FlowLayout It is the default layout manager, similar to how words flow in a text FlowLayout() //centers components and leaves five pixels between each component FlowLayout(int how) //how can be FlowLayout.LEFT..etc FlowLayout(int how, int horz, int vert) //specifies horizontal and vertical spacing between components Example: FlowLayoutDemo

29 Border Layout It has 4 narrow, fixed-width components at the edges and one large area in the center 4 sides are called north, south, east and west, middle are is called center BorderLayout() //default order layout BorderLayout(int horz,int vert) //allows to specify horizontal and vertical space between components Components are added by void add(Component compObj, Object region); Example: BorderLayoutDemo

30 Using Insets Allows to leave a small amount of space between the container that holds our components and the window that contains it Insets(int top,int left,int bottom,int right) Insets is created in the overridden getInsets() method defined by Container Example: InsetsDemo

31 Grid Layout It lays out components in a two-dimensional grid GridLayout() //single column grid layout GridLayout(int numRows, int numColumns) GridLayout(int numRows,int numColumns,int horz,int vert) If numRows=0, the column length is unlimited. Also if numColumns=0, the row length is unlimited Example: GridLayoutDemo

32 Menu Bars and Menus A menu bar displays a list of top-level menu choices Each choice is associated with a drop-down menu Menu is implemented by the classes MenuBar, Menu and MenuItem. First create a Menu bar, that contains one or more Menu objects MenuBar() Menu object can be created by Menu() Menu(String optionName)

33 Menu Bars and Menus (contd.) Each Menu object contains a list of MenuItem objects, that can be selected by user. Its constructors are MenuItem() MenuItem (String itemName) MenuItem (String itemName, MenuShortcut keyAcccel) Since Menu is a subclass of MenuItem, hierarchy of nested submenus can be created A menu item can be enabled or disable by setEnable() method An items status can be determined by isEnabled() The name of a menu item can be changed by setLabel(); name can be retrieved by getLabel()

34 Menu Bars and Menus (contd.) A checkable menu item can be created by CheckboxMenuItem, a subclass of MenuItem MenuItem can be added to a Menu by add() Also menu is added to a ManuBar by add() Menus only generate only ActionEvent when an item of type MenuItem is selected (ItemEvent is generated by CheckboxMenuItem) getItem() method of ItemEvent returns a reference to the item that generated this event Eample: MenuDemo

35 Dialog Boxes It holds a set of related controls and primarily used to obtain user input They are always a child window and don’t have any menu bars Dialog boxes can be modal (all input is directed to it until it is closed, other part of the program remains inactive) or modeless (input focus can be directed to another window) Dialog (Frame parentWindow, boolean mode) Dialog (Frame parentWindow, String title, boolean mode) When the dialog box is closed, dispose() is called, which is defined by Window Example: DialogDemo

36 File dialog It is used to specify a file FileDialog(Frame parent, String boxName) //parent is the honour of the dialog box, boxName is the title of the box FileDialog(Frame parent, String boxName,int how) // how = FileDialog.LOAD for reading and FileDialog.SAVE for writing FileDialog(Frame parent) getDirectory() and getFile() is used to get directory and file name Example: FileDialogDemo