awt-part2

Upload: qwernasd

Post on 03-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 AWT-part2

    1/157

  • 7/28/2019 AWT-part2

    2/157

    Control Fundamentals

    Labels

    Using Buttons Applying Check Boxes

    CheckBox Group

    Choice Controls

    Using Lists

    Managing Scroll Bars

    Using a Text Field

    Using a TextArea

    Understanding Layout Managers

    , Menu Bars and Menus

    Dialog Boxes

    FileDialog

    Handling Events by Extending AWT Components.

    TOPICS

  • 7/28/2019 AWT-part2

    3/157

    The AWT supports the following types of controls:

    Control Fundamentals

  • 7/28/2019 AWT-part2

    4/157

    To add a control to a window :

    1. first create an instance of the desired control

    2. then add it to a window by calling add( ) , which

    is defined by Container.

    Adding and Removing Controls

    Component add(Component compObj)

    compObjAn instance of the control that you want to add

    A reference to compObj is returned.

  • 7/28/2019 AWT-part2

    5/157

    To remove a control , call remove( ). This method is

    also defined by Container

    Adding and Removing Controls

    void remove(Component obj)

    compObjA reference to the control that you want to remove

    You can remove all controls by calling removeAll( )

  • 7/28/2019 AWT-part2

    6/157

    In general, your program simply implements the

    appropriate interface and then registers an event listener

    for each control that you need to monitor.

    Responding to Controls

    HeadlessException

    Generated by all constructors of the controls

    Thrown when code that is dependent on a keyboard,

    display, or mouse is called in an environment that does

    not support a keyboard, display, or mouse.

    Added by java 1.4

  • 7/28/2019 AWT-part2

    7/157

    A label is an object of type Label, and it contains a

    string, which it displays. Label defines the followingconstructors:

    Labels

    1. Label( )

    2. Label(String str)

    3. Label(String str, int how)

    1. The first version creates a blank label.

    2. The second version creates a label that contains the

    string specified by str.(left-justified.)

    3. The third version creates a label that contains the string

    specified by strusing the alignment specified by how

  • 7/28/2019 AWT-part2

    8/157

    The value ofhow must be one of these three constants:1. Label.LEFT,2. Label.RIGHT,3. Label.CENTER.

    Labels

    void setText(String str) set or change the text in a label

    StringgetText( ) obtain the current label

    void setAlignment(int how) set the alignment of the string

    intgetAlignment( ) obtain the current alignment

    how must be one of the alignment constants

    shown above

    Methods :

  • 7/28/2019 AWT-part2

    9/157

    The following example creates three labels and adds them to an

    applet:

  • 7/28/2019 AWT-part2

    10/157

    Following is the window created by the LabelDemo applet

  • 7/28/2019 AWT-part2

    11/157

    Using Buttons

    The most widely used control is the push button.

    A push button is a component that contains a label and that

    generates an event when it is pressed. Push buttons are objects

    of type Button.

    Constructors:

    1. Button( )creates an empty button

    2. Button(String str) creates a button that contains stras a label.

    Methods :1. void setLabel(String str) set the label

    2. String getLabel( ) retrieve the label

  • 7/28/2019 AWT-part2

    12/157

    Handling Buttons

    Each time a button is pressed, an Action Event is generated.

    Each listener implements theActionListenerinterface.

    This interface defines the actionPerformed( ) method, which

    is called when an event occurs.

    An ActionEvent object is supplied as the argument to this

    method.

    The label is obtained by calling getActionCommand() on

    ActionEvent Object

    (ActionEvent contains both a reference to the button that generated the

    event and a reference to the string that is the label of the button)

  • 7/28/2019 AWT-part2

    13/157

    Handling Buttons

    Here is an example that creates three buttons labeled Yes, No, and Undecided.

    Each time one is pressed, a message is displayed that reports which button has been

    pressed.

  • 7/28/2019 AWT-part2

    14/157

  • 7/28/2019 AWT-part2

    15/157

  • 7/28/2019 AWT-part2

    16/157

    Sample output

  • 7/28/2019 AWT-part2

    17/157

    Handling Buttons

    You can also determine which button has been pressed, by

    comparing the object obtained from thegetSource( ) method

    To do this, you must keep a list of the objects when they are added.

  • 7/28/2019 AWT-part2

    18/157

  • 7/28/2019 AWT-part2

    19/157

    Handling Buttons

  • 7/28/2019 AWT-part2

    20/157

    Applying Check Boxes

  • 7/28/2019 AWT-part2

    21/157

    Applying Check Boxes

    A check box is a control that is used to turn an option on or off

    There is a label associated with each checkbox that describes

    what option the box represents

    Check boxes are objects of the Checkbox class.

    Constructors:

    1. Checkbox( )

    2. Checkbox(String str)

    3. Checkbox(String str, boolean on)

    4. Checkbox(String str, boolean on, CheckboxGroup cbGroup)

    5. Checkbox(String str, CheckboxGroup cbGroup, boolean on)

  • 7/28/2019 AWT-part2

    22/157

    Applying Check Boxes

    1. The first form creates a check box whose label is initially

    blank. The state of the check box is unchecked2. The second form creates a check box whose label is

    specified by str. The state of the check box is unchecked

    3. The third form allows you to set the initial state of the

    check box.

    4. The fourth and fifth forms create a check box whose label

    is specified by str and whose group is specified by

    cbGroup. If this check box is not part of a group, then

    cbGroup must be null.

  • 7/28/2019 AWT-part2

    23/157

    Applying Check Boxes

    Methods :

    1. booleangetState( ) retrieve the current state of a

    check box

    2. void setState(boolean on) To set its state

    3. StringgetLabel( ) obtain the current label

    associated with a check box

    4. void setLabel(String str) To set the label

    ifon is true, the box is checked. If it is false, the box is

    cleared.

  • 7/28/2019 AWT-part2

    24/157

    Handling Check Boxes

    Each time a check box is selected or deselected, an

    item eventis generated

    Your program must implement ItemListenerInterface

    This interface defines the itemStateChanged( ) method

    An ItemEvent object is supplied as the argument to this

    method.

    An ItemEvent object contains information about the

    event

  • 7/28/2019 AWT-part2

    25/157

    Handling Check Boxes

    The following program creates four check boxes. The initial

    state of the first box is checked. The status of each check

    box is displayed. Each time you change the state of a check

    box, the status display is updated.

  • 7/28/2019 AWT-part2

    26/157

  • 7/28/2019 AWT-part2

    27/157

  • 7/28/2019 AWT-part2

    28/157

    Handling Check Boxes

    Sample output

  • 7/28/2019 AWT-part2

    29/157

    CheckboxGroup

  • 7/28/2019 AWT-part2

    30/157

    CheckboxGroup (radio buttons)

    A set of mutually exclusive check boxes in which one and only one

    check box in the group can be checked at any one time.

    Check box groups are objects of type CheckboxGroup

    Constructor :CheckboxGroup() creates an empty group

    Methods:

    1. Checkbox getSelectedCheckbox( ) get status

    2. void setSelectedCheckbox(Checkbox which) set a check box

    which the check box that you want to be selected

  • 7/28/2019 AWT-part2

    31/157

    CheckboxGroup (radio buttons)

    First define the group to which they will belong and then specify

    that group when you construct the check boxes.

  • 7/28/2019 AWT-part2

    32/157

  • 7/28/2019 AWT-part2

    33/157

  • 7/28/2019 AWT-part2

    34/157

    CheckboxGroup (radio buttons)

    Output generated by the CBGroup applet is shown

  • 7/28/2019 AWT-part2

    35/157

    Choice Controls

  • 7/28/2019 AWT-part2

    36/157

    Choice Controls

    A Choice control is a form of menu.

    The Choice class is used to create a pop-up list of items from

    which the 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

    Constructor (default):

    Choice () creates an empty list.

    Choice Controls

  • 7/28/2019 AWT-part2

    37/157

    Choice Controls

    Methods :

    1. void add(String name) add a selection to the list

    2. String getSelectedItem( ) returns a string containing the

    name of the item

    3. intgetSelectedIndex( )returns the index of the item

    4. intgetItemCount( ) obtain the number of items in the list

    5. void select(int index) select an item with a zero-based

    integer index

    6. void select(String name) select an item with a string

    7. StringgetItem(int index) obtain the name associated with

    the item at the given index.

  • 7/28/2019 AWT-part2

    38/157

    Handling Choice Lists

    Each time a choice is selected, an item event is

    generated.

    Each listener implements theItemListenerinterface

    That interface defines the itemStateChanged( )

    method

    An ItemEvent object is supplied as the argument to

    this method.

    dli h i i

  • 7/28/2019 AWT-part2

    39/157

    Handling Choice Lists

    Here is an example that creates two Choice menus. One selects the

    operating system. The other selects the browser.

  • 7/28/2019 AWT-part2

    40/157

  • 7/28/2019 AWT-part2

    41/157

  • 7/28/2019 AWT-part2

    42/157

  • 7/28/2019 AWT-part2

    43/157

  • 7/28/2019 AWT-part2

    44/157

    Using Lists

  • 7/28/2019 AWT-part2

    45/157

    Using Lists

    The List class provides a compact, multiple-choice, scrolling

    selection list

    Constructors :

    1. List( ) creates a List control that allows only one item to be

    selected at any one time.

    2. List(int numRows) The value of numRows specifies the

    number of entries in the list that will always be visible.

    3. List(int numRows, boolean multipleSelect) If

    multipleSelect is true, then the user may select two or more

    items at a time.

  • 7/28/2019 AWT-part2

    46/157

    Methods :

    1. void add(String name) adds items to the end of

    the list

    2. void add(String name, int index) adds the item

    at the index specified by index

    3. String getSelectedItem( ) returns a string

    containing the name of the item.

    4. int getSelectedIndex() returns the index of the

    item

    Using Lists

    M h d

  • 7/28/2019 AWT-part2

    47/157

    Methods :

    5. String[ ] getSelectedItems( ) returns an array

    containing the names of the currently selected items.

    6. int[ ] getSelectedIndexes( ) returns an array

    containing the indexes of the currently selected items.

    7. int getItemCount( ) obtain the number of items in the

    list

    8. void select(int index)

    set the currently selected item

    9. String getItem(int index) obtain the name associated

    with the item at the index

    Handling Lists

  • 7/28/2019 AWT-part2

    48/157

    you will need to implement the ActionListener interface

    Each time a List item is double-clicked, an ActionEvent object isgenerated

    Its getActionCommand( ) method can be used to retrieve the

    name of the newly selected item

    Each time an item is selected or deselected with a single click,

    an ItemEvent object is generated

    Its getStateChange( ) method can be used to determine

    whether a selection or deselection triggered this event

    getItemSelectable( ) returns a reference to the object that

    triggered this event.

    Handling Lists

  • 7/28/2019 AWT-part2

    49/157

    Here is an example that converts the Choice controls in the preceding

    section into List components, one multiple choice and the other

    single choice:

  • 7/28/2019 AWT-part2

    50/157

  • 7/28/2019 AWT-part2

    51/157

  • 7/28/2019 AWT-part2

    52/157

  • 7/28/2019 AWT-part2

    53/157

    Managing Scroll Bars

  • 7/28/2019 AWT-part2

    54/157

    Managing Scroll Bars

  • 7/28/2019 AWT-part2

    55/157

    g g

    Constructors

    1. Scrollbar() creates a vertical scroll bar.2. Scrollbar(int style)

    style specifies the orientation . style can be

    Scrollbar.VERTICAL

    Scrollbar.HORIZONTAL

    3. Scrollbar(int style, int initialValue, int thumbSize, int min, int max)

    initialValuethe initial value of the scroll bar

    thumbSize The number of units represented by the height of the thumb

    min , max The minimum and maximum values for the scroll bar

    Managing Scroll Bars

  • 7/28/2019 AWT-part2

    56/157

    g g

    Methods :

    1. void setValues(int initialValue, int thumbSize, int min, int max)

    set its parameters

    2. int getValue( ) obtain the current value of the scroll bar

    3. void setValue(int newValue) set the current value

    4. int getMinimum( ) retrieve the minimum values

    5. int getMaximum( ) retrieve the maximum values

    6. void setUnitIncrement(int newIncr)change the increment (default : 1)

    7. void setBlockIncrement(int newIncr) page-up and page-down increments

    (default : 10)

    Handling Scroll Bars

  • 7/28/2019 AWT-part2

    57/157

    Interaction with a scroll bar, generates an Adjustment Event

    you need to implement the AdjustmentListener interface

    This interface defines adjustmentValueChanged() method

    which takes AdjustmentEvent object as the argument

    getAdjustmentType() ofAdjustmentEvent can be used to

    determine the type of the adjustment.

    g

    Handling Scroll Bars

  • 7/28/2019 AWT-part2

    58/157

    g

    The types of adjustment events are as follows:

    Handling Scroll Bars

  • 7/28/2019 AWT-part2

    59/157

    The following example creates both a vertical and a horizontal scroll bar. If you

    drag the mouse while inside the window, the coordinates of each drag event

    are used to update the scroll bars. An asterisk is displayed at the current drag

    position.

    g

    Handling Scroll Bars

  • 7/28/2019 AWT-part2

    60/157

    g

    Handling Scroll Bars

  • 7/28/2019 AWT-part2

    61/157

    Handling Scroll Bars

  • 7/28/2019 AWT-part2

    62/157

    Handling Scroll Bars

  • 7/28/2019 AWT-part2

    63/157

    Handling Scroll Bars

  • 7/28/2019 AWT-part2

    64/157

    Sample output

  • 7/28/2019 AWT-part2

    65/157

    Using a TextField

    Using a TextField

  • 7/28/2019 AWT-part2

    66/157

    The TextField class implements a single-line text-entry area,

    usually called an edit control.

    TextField is a subclass ofTextComponent

    Constructors :

    1. TextField( ) creates a default text field

    2. TextField(int numChars) creates a text field that is

    numChars characters wide

    3. TextField(String str)initializes the text field with the string

    4. TextField(String str, int numChars) initializes a text field

    and sets its width

    Using a TextField

  • 7/28/2019 AWT-part2

    67/157

    Using a TextField

    Methods

    1. String getText( ) obtain the curent string

    2. void setText(String str) To set the text

    3. String getSelectedText( ) select a portion of the text

    4. void select(int startIndex, int endIndex) obtain the

    currently selected text

    Using a TextField

  • 7/28/2019 AWT-part2

    68/157

    Using a TextField

    Methods

    5. void setEditable(boolean canEdit) if canEdit is true, thetext may be changed. If it is false, the text cannot be altered

    6. boolean isEditable( ) determine editability7. void setEchoChar(char ch) disable the echoing of the

    characters

    8. boolean echoCharIsSet( ) check a text field to see if it is inthis mode

    9. char getEchoChar( ) retrieve the echo character

    Handling a TextField

  • 7/28/2019 AWT-part2

    69/157

    Handling a TextField

    Text Field generates Action Event when ENTER key is pressed.

  • 7/28/2019 AWT-part2

    70/157

  • 7/28/2019 AWT-part2

    71/157

  • 7/28/2019 AWT-part2

    72/157

    Sample output

  • 7/28/2019 AWT-part2

    73/157

    Using a TextArea

    Using a TextArea

  • 7/28/2019 AWT-part2

    74/157

    Using a TextArea

    AWT includes a simple multiline editor called TextArea

    TextArea is a subclass ofTextComponent

    Constructors :

    1. TextArea( )

    2. TextArea(int numLines, int numChars)

    3. TextArea(String str)

    4. TextArea(String str, int numLines, int numChars)

    5. TextArea(String str, int numLines, int numChars, int sBars)

    Using a TextArea

  • 7/28/2019 AWT-part2

    75/157

    Using a TextArea

    Constructors :

    numLines specifies the height, in lines,

    numCharsspecifies its width, in characters.

    str Initial text

    sBars must be one of these values :

    SCROLLBARS_BOTH

    SCROLLBARS_NONE

    SCROLLBARS_HORIZONTAL_ONLY

    SCROLLBARS_VERTICAL_ONLY

    Using a TextArea

  • 7/28/2019 AWT-part2

    76/157

    Using a TextArea

    Methods :

    Supports the getText( ), setText( ), getSelectedText( ), select( ), isEditable( ), and

    setEditable( ) methods. In addition to these, TextArea adds the following

    methods:

    1. void append(String str) appends the string specified by str to

    the end of the currenttext.

    2. void insert(String str, int index) inserts the string passed in

    strat the specified index

    3. void replaceRange(String str, int startIndex, int endIndex)

    replaces the characters from startIndex to endIndex1, withthe replacement text passed in str.

    Using a TextArea

  • 7/28/2019 AWT-part2

    77/157

    Using a TextArea

    Text areas only generate got-focus and lost-focus events.

    Normally, your program simply obtains the current text when it is

    needed. The following program creates a TextArea control

    Using a TextArea

  • 7/28/2019 AWT-part2

    78/157

    Using a TextArea

    Using a TextArea

  • 7/28/2019 AWT-part2

    79/157

    g

    sample output

    Understanding Layout Managers

  • 7/28/2019 AWT-part2

    80/157

    Understanding Layout Managers

    Each Container object has a layout manager associated with it.

    The layout manager is set by the setLayout( ) method

    void setLayout(LayoutManager layoutObj)

    layoutObj A reference to the desired layout manager

    pass null for layoutObj If you wish to disable thelayout manager.

  • 7/28/2019 AWT-part2

    81/157

    FlowLayout

  • 7/28/2019 AWT-part2

    82/157

    y

    FlowLayout is the default layout manager.

    Components are laid out from the upper-left corner, left to

    right and top to bottom.

    Constructors for FlowLayout

    1. FlowLayout( )

    2. FlowLayout(int how)

    3. FlowLayout(int how, int horz, int vert)

    FlowLayout

  • 7/28/2019 AWT-part2

    83/157

    y

    Constructors for FlowLayout

    1. The first form creates the default layout, which centerscomponents and leaves five pixels of space between each

    component

    2. The second form lets you specify how each line is aligned.

    Valid values forhow are as follows:

    1. FlowLayout.LEFT

    2. FlowLayout.CENTER

    3. FlowLayout.RIGHT

    4. FlowLayout.LEADING

    5. FlowLayout.TRAILING

    FlowLayout

  • 7/28/2019 AWT-part2

    84/157

    y

    Constructors for FlowLayout

    3. The third form allows you to specify the horizontal and

    vertical space left between components

    Here is a version of the CheckboxDemo applet( the controls are left aligned )

    FlowLayout

  • 7/28/2019 AWT-part2

    85/157

    FlowLayout

  • 7/28/2019 AWT-part2

    86/157

    FlowLayout

  • 7/28/2019 AWT-part2

    87/157

    FlowLayout

  • 7/28/2019 AWT-part2

    88/157

    sample output

    BorderLayout

  • 7/28/2019 AWT-part2

    89/157

    The BorderLayout class implements a common layout

    style for top-level windows.

    It has four narrow, fixed-width components at the

    edges and one large area in the center.

    The four sides are referred to as north, south, east, and

    west. The middle area is called the center.

    BorderLayout

  • 7/28/2019 AWT-part2

    90/157

    Constructors defined by BorderLayout

    1. BorderLayout( ) Creates a default border layout.

    2. BorderLayout(int horz, int vert)

    Allows you to specify the horizontal and vertical

    space left between components in horzand vert

    BorderLayout defines the following constants that specify the

  • 7/28/2019 AWT-part2

    91/157

    BorderLayout defines the following constants that specify the

    regions:

    BorderLayout.CENTER

    BorderLayout.SOUTH

    BorderLayout.EAST

    BorderLayout.WEST

    BorderLayout.NORTH

    Use these constants with the following form of add( ), which is

    defined by Container:

    void add(Component compObj, Object region);

    region specifies where the componentwill be added.

    Here is an example of a BorderLayout with a component in each

  • 7/28/2019 AWT-part2

    92/157

    Here is an example of a BorderLayout with a component in each

    layout area

  • 7/28/2019 AWT-part2

    93/157

  • 7/28/2019 AWT-part2

    94/157

    Sample output

    Using Insets

  • 7/28/2019 AWT-part2

    95/157

    To leave a small amount of space between the container that

    holds your components and the window that contains it. To

    do this :

    Override the getInsets( ) method that is defined by Container

    Insets getInsets( )

    returns an Insets object that containsthe top, bottom, left, and right inset

    Constructor for Insets

    Insets(int top, int left, int bottom, int right)

  • 7/28/2019 AWT-part2

    96/157

  • 7/28/2019 AWT-part2

    97/157

    Using Insets

  • 7/28/2019 AWT-part2

    98/157

    Using Insets

  • 7/28/2019 AWT-part2

    99/157

    Output from the InsetsDemo applet

    GridLayout

  • 7/28/2019 AWT-part2

    100/157

    GridLayout lays out components in a two-dimensional grid.

    The constructors supported

    1. GridLayout( ) creates a single-column grid layout

    2. GridLayout(int numRows, int numColumns )

    3. GridLayout(int numRows, int numColumns, int horz, int vert)

    The third form allows you to specify the horizontal and vertical

    space left between components in horz andvert, respectively.

    numRow , numColumns specified number of rows and columns

    GridLayout

  • 7/28/2019 AWT-part2

    101/157

    Here is a sample program that creates a 44 grid and fills it in with

    15 buttons, each labeled with its index:

    GridLayout

  • 7/28/2019 AWT-part2

    102/157

    GridLayout

  • 7/28/2019 AWT-part2

    103/157

    the output generated by the GridLayoutDemo applet:

    CardLayout

  • 7/28/2019 AWT-part2

    104/157

    It stores several different layouts

    Each layout can be thought of as being on a separate

    index card in a deck

    The deck can be shuffled so that any card is on top at a

    given time

    useful for user interfaces with optional components

    that can be dynamically enabled and disabled upon user

    input

    CardLayout

  • 7/28/2019 AWT-part2

    105/157

    Provides these two constructors

    1. CardLayout( )

    2. CardLayout(int horz, int vert)

    The first form creates a default card layout

    The second form allows you to specify the horizontal

    and vertical space left between components in horz and

    vert, respectively

  • 7/28/2019 AWT-part2

    106/157

  • 7/28/2019 AWT-part2

    107/157

  • 7/28/2019 AWT-part2

    108/157

  • 7/28/2019 AWT-part2

    109/157

  • 7/28/2019 AWT-part2

    110/157

  • 7/28/2019 AWT-part2

    111/157

  • 7/28/2019 AWT-part2

    112/157

  • 7/28/2019 AWT-part2

    113/157

    d

  • 7/28/2019 AWT-part2

    114/157

    Menu Bars and Menus

  • 7/28/2019 AWT-part2

    115/157

    Steps to create Menus

    1 first create an instance of MenuBar

  • 7/28/2019 AWT-part2

    116/157

    1. first create an instance ofMenuBar

    2. Next, create instances of Menu that will define the selections

    displayed on the bar

    constructors for Menu:

    1. Menu( )2. Menu(String optionName)3. Menu(String optionName, boolean removable)

    optionName

    specifies the name of the menu selection.removable If removable is true, the pop-up menu can be

    removed and allowed to float free.

    Steps to create Menus

    To cretate menu items use MenuItems constructors :

  • 7/28/2019 AWT-part2

    117/157

    To cretate menu items use MenuItems constructors :

    1. MenuItem( )

    2. MenuItem(String itemName)

    3. MenuItem(String itemName, MenuShortcut keyAccel)

    itemNamethe name shown in the menu,

    keyAccel is the menu shortcut forthis item.

  • 7/28/2019 AWT-part2

    118/157

    CheckboxMenuItem

  • 7/28/2019 AWT-part2

    119/157

    You can create a checkable menu item by using a subclass

    ofMenuItem called CheckboxMenuItem

    1. CheckboxMenuItem( )

    2. CheckboxMenuItem(String itemName)

    3. CheckboxMenuItem(String itemName, boolean on)

    itemName the name shown in the menu

    on ifon is true, the checkable entry is initially checked.

    CheckboxMenuItem

    You can obtain the status of a checkable item by calling

  • 7/28/2019 AWT-part2

    120/157

    You can obtain the status of a checkable item by calling

    getState( ).

    You can set it to a known state by using setState( ).

    boolean getState( ) returns true if checked. Otherwise, it

    returns false

    void setState(boolean checked) checked must be TRUE

    to check an item

    Adding Menu item and Menu Bar

    Add the item to a Menu object by using add( )

  • 7/28/2019 AWT-part2

    121/157

    Add the item to a Menu object byusing add( ),

    MenuItem add(MenuItem item)

    Item the item being added. The item is returned.

    Once you have added all items to a Menu object, you can add that

    object to the menu bar by using this version of add( ) defined by

    MenuBar:

    Menu add(Menu menu)

    menu the menu being added. The menu is returned

    Handling Menus

  • 7/28/2019 AWT-part2

    122/157

    Menus only generate events when an item of type MenuItem or

    CheckboxMenuItem is selected.

    Each time a menu item is selected, an ActionEvent object is

    generated.

    Each time a check box menu item is checked or unchecked, an

    ItemEvent object is generated

    Thus, you must implement the ActionListener and ItemListener

    interfaces in order to handle these menu events.

  • 7/28/2019 AWT-part2

    123/157

  • 7/28/2019 AWT-part2

    124/157

  • 7/28/2019 AWT-part2

    125/157

  • 7/28/2019 AWT-part2

    126/157

  • 7/28/2019 AWT-part2

    127/157

  • 7/28/2019 AWT-part2

    128/157

  • 7/28/2019 AWT-part2

    129/157

  • 7/28/2019 AWT-part2

    130/157

  • 7/28/2019 AWT-part2

    131/157

  • 7/28/2019 AWT-part2

    132/157

  • 7/28/2019 AWT-part2

    133/157

  • 7/28/2019 AWT-part2

    134/157

    Dialog Boxes

  • 7/28/2019 AWT-part2

    135/157

    Dialog Boxes

  • 7/28/2019 AWT-part2

    136/157

    Dialog boxes are of type Dialog.

    Two commonly used constructors :

    1. Dialog(Frame parentWindow, boolean mode)

    2. Dialog(Frame parentWindow, String title, boolean mode)

    parentWindow the owner of the dialog box.

    mode If mode is true, the dialog box is modal

    title title of the dialog box

    Generally, you will subclass Dialog, adding the functionality

    required by your application

    Dialog Boxes

    Following is a modified version of the preceding menu program

  • 7/28/2019 AWT-part2

    137/157

    Following is a modified version of the preceding menu program

    that displays a modeless dialog box when the New option ischosen.

  • 7/28/2019 AWT-part2

    138/157

  • 7/28/2019 AWT-part2

    139/157

  • 7/28/2019 AWT-part2

    140/157

  • 7/28/2019 AWT-part2

    141/157

  • 7/28/2019 AWT-part2

    142/157

  • 7/28/2019 AWT-part2

    143/157

  • 7/28/2019 AWT-part2

    144/157

  • 7/28/2019 AWT-part2

    145/157

  • 7/28/2019 AWT-part2

    146/157

  • 7/28/2019 AWT-part2

    147/157

  • 7/28/2019 AWT-part2

    148/157

  • 7/28/2019 AWT-part2

    149/157

  • 7/28/2019 AWT-part2

    150/157

    FileDialog

  • 7/28/2019 AWT-part2

    151/157

    To create a file dialog box, instantiate an object of type

    FileDialog.

    This causes a file dialog box to be displayed.

    FileDialog provides these constructors:

    1. FileDialog(Frame parent, String boxName)

    2. FileDialog(Frame parent, String boxName, int how)

    3. FileDialog(Frame parent)

    FileDialog

  • 7/28/2019 AWT-part2

    152/157

    parent the owner of the dialog box,

    boxName the name displayed in the boxs title bar.

    If boxName is omitted, the title of the dialog box is empty.

    how if FileDialog.LOAD, then the box is selecting a file for

    reading.

    If how is FileDialog.SAVE, the box is selecting a file for writing

    FileDialog

    FileDialog( ) id th f ll i th d

  • 7/28/2019 AWT-part2

    153/157

    FileDialog( ) provides the following methods

    1. String getDirectory( )

    2. String getFile( )

    These methods return the directory and the filename, respectively.

  • 7/28/2019 AWT-part2

    154/157

  • 7/28/2019 AWT-part2

    155/157

  • 7/28/2019 AWT-part2

    156/157

    The output generated by this program

  • 7/28/2019 AWT-part2

    157/157

    The output generated by this program