0% found this document useful (0 votes)
5 views17 pages

Java CardLayout and Dialog Box Guide

The document provides an overview of the CardLayout class, which allows for multiple layouts to be managed like a deck of cards, enabling dynamic user interfaces. It also discusses adapter classes for event handling, dialog boxes for user input, and the structure of menu bars and menus in Java, detailing their constructors and functionalities. Key concepts include how to create and manage these components, as well as the events they generate.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

Java CardLayout and Dialog Box Guide

The document provides an overview of the CardLayout class, which allows for multiple layouts to be managed like a deck of cards, enabling dynamic user interfaces. It also discusses adapter classes for event handling, dialog boxes for user input, and the structure of menu bars and menus in Java, detailing their constructors and functionalities. Key concepts include how to create and manage these components, as well as the events they generate.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

CardLayout

• The CardLayout class is unique among the other layout managers in


that it stores several different layouts.
• Each layout can be thought of as being on a separate index card in a
deck that can be shuffled so that any card is on top at a given time.
• This can be useful for user interfaces with optional components that
can be dynamically enabled and disabled upon user input.
• You can prepare the other layouts and have them hidden, ready to be
activated when needed.
• Constructors:
CardLayout( )
– creates a default card layout
CardLayout(int horz, int vert)
– Allows you to specify the horizontal and vertical space left between
components in horz and vert respectively.
• The cards are typically held in an object of type Panel.
• This panel must have CardLayout selected as its layout manager.
• The cards that form the deck are also typically objects of type
Panel.
• Thus, you must create a panel that contains the deck and a panel
for each card in the deck.
• you add to the appropriate panel the components that form each
card.
• Then add these panels to the panel for which CardLayout is the
layout manager.
• Finally, you add this panel to the main applet panel.
• Once these steps are complete, you must provide some way for
the user to select between cards.
• One common approach is to include one push button for each
card in the deck.
• When card panels are added to a panel, they are usually
given a name.
• Thus, most of the time, you will use this form of add( )
when adding cards to a panel:
void add(Component panelObj, Object name);
– Here, name is a string that specifies the name of the card whose panel is
specified by panelObj.
• After you have created a deck, your program activates a
card by calling one of the following methods defined by
CardLayout:
void first(Container deck)
– causes the first card in the deck to be shown.

void last(Container deck)


– To show the last card
void next(Container deck)
– To show the next card
void previous(Container deck)
– To show the previous card
– Both next( ) and previous( ) automatically cycle back to the top or
bottom of the deck, respectively.
void show(Container deck, String cardName)
– displays the card whose name is passed in cardName
• Here, deck is a reference to the container (usually a
panel) that holds the cards
• cardName is the name of a card.
Adapter classes
• An adapter class provides an empty implementation of all methods
in an event listener interface.
• Adapter classes are useful when you want to receive and process
only some of the events that are handled by a particular event
listener interface.
• You can define a new class to act as an event listener by extending
one of the adapter classes and implementing only those events in
which you are interested.
• If any event listener interface is having only one method, there is
no corresponding adapter class.
– ActionListener
– ItemListener
– AdjustmentListener
– TextListener
• Adapter classes are concrete classes which must be
extended with the main class
• As the main class already extends Frame, we can’t
extend any other class
• We make helper classes that extend the adapter class
and we call helper class in the constructor.
DialogBox
• Sometimes we want to use a dialog box to hold a set of
related controls.
• Dialog boxes are primarily used to obtain user input.
• They are similar to frame windows, except that dialog boxes
are always child windows of a top-level window.
• Also, dialog boxes don’t have menu bars.
• In other respects, dialog boxes function like frame windows.
(You can add controls to them, for example, in the same
way that you add controls to a frame window.)
• Dialog boxes may be modal or modeless.
• When a modal dialog box is active, all input is directed to it until
it is closed.
• This means that you cannot access other parts of your 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.
• Thus, other parts of your program remain active and accessible.
• Constructors:
Dialog(Frame parentWindow, boolean mode)
Dialog(Frame parentWindow, String title, boolean mode)
• Here, parentWindow is the owner of the dialog box. If mode is
true, the dialog box is modal. Otherwise, it is modeless.
• The title of the dialog box can be passed in title.
• Generally, you will subclass Dialog, adding the functionality
required by your application.
Menubars and Menus
• A top-level window can have a menu bar associated with it.
• A menu bar displays a list of top-level menu choices.
• Each choice is associated with a drop-down menu.
• This concept is implemented in Java by the following classes:
MenuBar, Menu, and MenuItem.
• In general, 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.
• To create a menu bar, first create an instance of MenuBar.
• This class only defines the default constructor.
• Next, create instances of Menu that will define the
selections displayed on the bar.
• constructors for Menu:
Menu( )
Menu(String optionName)
• Here, optionName specifies the name of the menu selection.
• The first form creates an empty menu.
• Individual menu items are of type MenuItem.
• constructors:
MenuItem( )
MenuItem(String itemName)
MenuItem(String itemName, MenuShortcut
keyAccel)
• Here, itemName is the name shown in the menu, and
keyAccel is the menu shortcut for this item.
void setEnabled(boolean enabledFlag)
– You can disable or enable a menu item
– If the argument enabledFlag is true, the menu item is
enabled. If false, the menu item is disabled.
void setLabel(String newName)
– You can change the name of a menu item
– newName becomes the new name of the invoking menu
item
String getLabel( )
– To get the current name of a menu item
• You can create a checkable menu item by using a subclass
of MenuItem called CheckboxMenuItem.
• constructors:
CheckboxMenuItem( )
CheckboxMenuItem(String itemName)
CheckboxMenuItem(String itemName, boolean on)
• Here, itemName is the name shown in the menu.
• Checkable items operate as toggles.
• Each time one is selected, its state changes.
• In the first two forms, the checkable entry is unchecked.
• In the third form, if on is true, the checkable entry is
initially checked. Otherwise, it is cleared.
boolean getState( )
– To obtain the status of a checkable item
– If the item is checked, getState( ) returns true. Otherwise, it returns
false.
void setState(boolean checked)
– set it to a known state
– To check an item, pass true
– To clear an item, pass false.
MenuItem add(MenuItem item)
– Once you have created a menu item, you must add the item to a
Menu object
– Here, item is the item being added.
– Items are added to a menu in the order in which the calls
to add( ) take place.
– 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)
• Here, menu is the menu being added. The menu is returned.
• Menus only generate events when an item of type
MenuItem or CheckboxMenuItem is selected.
• They do not generate events when a menu bar is accessed to
display a drop-down menu
• For example. 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.
Object getItem( )
– This is the method of ItemEvent returns a reference to the item that
generated this event.

You might also like