Unit – I-updated
Unit – I-updated
12 Marks
Introduction
• The AWT contains numerous classes and methods that allow
you to create and manage windows.
• It is also the foundation upon which Swing is built.
• Today, most Java programs employ user interfaces based on
Swing.
• Swing provides richer implementations than does the AWT of
some common GUI controls.
AWT classes
• The AWT classes are contained in the java.awt package.
• It is one of Java’s largest packages.
• It is logically organized in a top-down, hierarchical fashion.
Component, Container, Window,
Frame, Panel
Component:
-Abstract class
-All user interfaces are subclasses of Component
-Defines over a hundred public methods that are responsible for
managing events
-A Component object is responsible for remembering the current
foreground and background colors and the currently selected
text font.
Container:
-Subclass of Component
-Has additional methods that allow other Component objects to
be nested within it.
-Other Container objects can be stored inside of a Container
-A container is responsible for laying out (that is, positioning) any
components that it contains. It does this through the use of
various layout managers
Panel:
-Concrete subclass of Container
-It doesn’t add any new methods; it simply implements
Container
-May be thought of as a recursively nestable, concrete screen
component
-Panel is the superclass for Applet
-Is a window that does not contain a title bar, menu bar, or
border
-Other components can be added to a Panel object by its add( )
method (inherited from Container).
Window:
-Creates a top-level window. A top-level window is not contained
within any other object; it sits directly on the desktop
- A subclass of Window called Frame is generally used.
Frame:
-Subclass of Window and has a title bar, menu bar, borders, and
resizing corners
-If you create a Frame object from within an applet, it will
contain a warning message, such as “Java Applet Window,” to the
user that an applet window has been created.
Creating windowed programs and applets:
Frame class:
Constructors:
-Frame()
Constructs a new instance of Frame that is initially invisible.
-Frame(String title)
Constructs a new, initially invisible Frame object with the
specified title
Methods:
-public void setTitle(String title)
Sets the title for this frame to the specified string
-public void setSize(Dimension d)
Resizes this component so that it has width d.width and height
d.height.
-public void setSize(int width,int height)
Resizes this component so that it has width width and height
height.
-public void setVisible(boolean b)
Shows or hides this Window depending on the value of
parameter b.
(Note: Above three methods are inherited from Window class)
Methods:
-void setText(String str)
-String getText( )
-void setAlignment(int how)
-int getAlignment( )
Button class:
-the most widely used control is the push button.
-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:
-Button( ) throws HeadlessException
creates an empty button
-Button(String str) throws HeadlessException
creates a button that contains str as a label
Methods:
-void setLabel(String str)
-String getLabel( )
Checkbox class:
Methods:
-boolean getState( )
-void setState(boolean on)
-String getLabel( )
-void setLabel(String str)
Scrollbars:
-Scroll bars are used to select continuous values between a
specified minimum and maximum.
-Scroll bars may be oriented horizontally or vertically.
-A scroll bar is actually a composite of several individual parts.
Each end has an arrow that you can click to move the current
value of the scroll bar one unit in the direction of the arrow.
-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.
-The slider box can be dragged by the user to a new position. The
scroll bar will then reflect this value.
-In the background space on either side of the thumb, the user
can click to cause the thumb to jump in that direction by some
increment larger than 1. Typically, this action translates into
some form of page up and page down
Constructors:
-Scrollbar( ) throws HeadlessException
creates a vertical scroll bar
-Scrollbar(int style) throws HeadlessException
-Scrollbar(int style, int initialValue, int thumbSize, int min, int
max) throws HeadlessException
allow you to specify the orientation of the scroll bar
-If style is Scrollbar.VERTICAL, a vertical scroll bar is created.
-If style is Scrollbar.HORIZONTAL, the scroll bar is horizontal.
-In the third form of the constructor, the initial value of the scroll
bar is passed in initialValue.
-The number of units represented by the height of the thumb is
passed in thumbSize.
-The minimum and maximum values for the scroll bar are
specified by min and max
Methods:
-void setValues(int initialValue, int thumbSize, int min, int max)
-int getValue( )
-void setValue(int newValue)
-int getMinimum( )
-int getMaximum( )
-void setUnitIncrement(int newIncr) - by default it is 1
-void setBlockIncrement(int newIncr) - by default it is 10
Choice control:
-The Choice class is used to create a pop-up list of items from
which the user may choose.
-Choice control is a form of menu. When inactive, a Choice component
takes up only enough space to show the currently selected item.
-When the user clicks on it, the whole list of choices pops up,
and a new selection can be made
-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.
-Choice only defines the default constructor, which creates an
empty list.
Methods:
-void add(String name)
-String getSelectedItem( )
-int getSelectedIndex( )
-int getItemCount( )
-void select(int index)
-void select(String name)
-String getItem(int index)
List control:
-The List class provides a compact, multiple-choice, scrolling
selection list
-Unlike the Choice object, which shows only the single selected
item in the menu, a List object can be constructed to show any
number of choices in the visible window
-It can also be created to allow multiple selections.
Constructors:
-List( ) throws HeadlessException
-List(int numRows) throws HeadlessException
-List(int numRows, boolean multipleSelect) throws
HeadlessException
Methods:
-void add(String name)
-void add(String name, int index)
-String getSelectedItem( )
-int getSelectedIndex( )
-String[ ] getSelectedItems( )
-int[ ] getSelectedIndexes( )
-int getItemCount( )
-void select(int index)
-String getItem(int index)
TextField:
-The TextField class implements a single-line text-entry area,
usually called an edit control
-Text fields allow the user to enter strings and to edit the text
using the arrow keys, cut and paste keys, and mouse selections
-TextField is a subclass of TextComponent
Constructors:
-TextField( ) throws HeadlessException
-TextField(int numChars) throws HeadlessException
-TextField(String str) throws HeadlessException
-TextField(String str, int numChars) throws HeadlessException
Methods:
-String getText( )
-void setText(String str)
-String getSelectedText( )
-void select(int startIndex, int endIndex)
-boolean isEditable( )
-void setEditable(boolean canEdit)
-void setEchoChar(char ch)
-boolean echoCharIsSet( )
-char getEchoChar( )
TextArea:
-Sometimes a single line of text input is not enough for a given
task. To handle these situations, the AWT includes a simple
multiline editor called TextArea
Constructors:
-TextArea( ) throws HeadlessException
-TextArea(int numLines, int numChars) throws HeadlessException
-TextArea(String str) throws HeadlessException
-TextArea(String str, int numLines, int numChars) throws
HeadlessException
-TextArea(String str, int numLines, int numChars, int sBars)
throws HeadlessException
-sBars must be one of these values
SCROLLBARS_BOTH
SCROLLBARS_NONE
SCROLLBARS_HORIZONTAL_ONLY
SCROLLBARS_VERTICAL_ONLY
Methods:
-TextArea is a subclass of TextComponent. Therefore, it supports
the getText( ), setText( ), getSelectedText( ), select( ), isEditable( ),
and setEditable( ) methods
-void append(String str)
-void insert(String str, int index)
-void replaceRange(String str, int startIndex, int endIndex)
Use of Layout Managers:
-a layout manager automatically arranges your controls within a
window by using some type of algorithm
-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 the setLayout( ) method.
-If no call to setLayout( ) is made, then the default layout
manager is used.
-The setLayout( ) method has the following general form:
void setLayout(LayoutManager layoutObj)
FlowLayout
-FlowLayout is the default layout manager of Panel.
-FlowLayout implements a simple layout style, which is similar to
how words flow in a text editor.
-The direction of the layout is governed by the container’s
component orientation property, which, by default, is left to
right, top to bottom.
-Therefore, by default, components are laid out line-by-line
beginning at the upper-left corner.
-In all cases, when a line is filled, layout advances to the next line.
-A small space is left between each component, above and below,
as well as left and right.
Constructors
-FlowLayout( )
-FlowLayout(int how)
-FlowLayout(int how, int horz, int vert)
-The first form creates the default layout, which centers
components and leaves five pixels of space between each
component.
-The second form lets you specify how each line is aligned.
Valid values for how are as follows:
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
FlowLayout.LEADING
FlowLayout.TRAILING
BorderLayout
-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
Constructors:
BorderLayout( )
BorderLayout(int horz, int vert)
Method (of Container class):
void add(Component compObj, Object region)
-Here, compObj is the component to be added, and region
specifies where the component will be added
-region can take following values
BorderLayout.CENTER
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.NORTH
GridLayout
-GridLayout lays out components in a two-dimensional grid. -
-When you instantiate a GridLayout, you define the number of
rows and columns
Constructors:
GridLayout( )
GridLayout(int numRows, int numColumns)
GridLayout(int numRows, int numColumns, int horz, int vert)
-The first form creates a single-column grid layout.
-The second form creates a grid layout with the specified number
of rows and columns.
-The third form allows you to specify the horizontal and vertical
space left between components in horz and vert, respectively
-Either numRows or numColumns can be zero.
Specifying numRows as zero allows for unlimited-length columns.
Specifying numColumns as zero allows for unlimited-length rows
CardLayout
-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( )
CardLayout(int horz, int vert)
-The second form allows you to specify the horizontal and vertical
space left between components in horz and vert, respectively
Steps
-Create a Panel (container for cards) and set its layout manager as
CardLayout
-Create separate panels for each card – these panel can have any
layout manager.
-Add required components to cards
-Add these panels to the panel created in step 1
-Add the panel created in step 1 to window
-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
Method used to add panels to main panel (deck)
void add(Component panelObj, Object name)
Here, name is a string that specifies the name of the card
-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)
void last(Container deck)
void next(Container deck)
void previous(Container deck)
void show(Container deck, String cardName)
GridBagLayout
-You can specify the relative placement of components by
specifying their positions within cells inside a grid
-The key to the grid bag is that each component can be a
different size, and each row in the grid can have a different
number of columns. This is why the layout is called a grid
bag. It’s a collection of small grids joined together
-The location and size of each component in a grid bag are
determined by a set of constraints linked to it
-The constraints are contained in an object of type
GridBagConstraints
-Constraints include the height and width of a cell, and the
placement of a component, its alignment, and its anchor
point within the cell
-The general procedure for using a grid bag is to first
create a new GridBagLayout object and to make it the current
layout manager. Then, set the constraints that apply to each
component that will be added to the grid bag. Finally, add the
components to the window.
Constructors:
GridBagLayout( )
Methods:
void setConstraints(Component comp, GridBagConstraints
cons)
-GridBagConstraints defines several fields that you can set
to govern the size, placement, and spacing of a component
-The general procedure for using a grid bag is to first
create a new GridBagLayout object and to make it the current
layout manager. Then, set the constraints that apply to each
component that will be added to the grid bag. Finally, add the
components to the window.
Constructors:
GridBagLayout( )
Methods:
void setConstraints(Component comp, GridBagConstraints
cons)
-GridBagConstraints defines several fields that you can set
to govern the size, placement, and spacing of a component
-When a component is smaller than its cell, you can use the anchor
field to specify where within the cell the component’s top-left
corner will be located
GridBagConstraints.CENTER
GridBagConstraints.SOUTH
GridBagConstraints.EAST
GridBagConstraints.SOUTHEAST
GridBagConstraints.NORTH
GridBagConstraints.SOUTHWEST
GridBagConstraints.NORTHEAST
GridBagConstraints.WEST
GridBagConstraints.NORTHWEST
-The weightx and weighty, by default, both these values are
zero. When all values within a row or a column are zero,
extra space is distributed evenly between the edges of
the window
-The gridwidth variable lets you specify the width of a cell
in terms of cell units. The default is 1.
To specify that a component use the remaining space in a
row, use GridBagConstraints.REMAINDER.
To specify that a component use the next-to-last cell in a
row, use GridBagConstraints.RELATIVE.
-The gridheight constraint works the same way, but in the
vertical direction.
-You can specify a padding value that will be used to increase the
minimum size of a cell.
To pad horizontally, assign a value to ipadx.
To pad vertically, assign a value to ipady.
FileDialog
-Java provides a built-in dialog box that lets the user specify a
file.
-To create a file dialog box, instantiate an object of type
FileDialog. This causes a file dialog box to be displayed.
-Usually, this is the standard file dialog box provided by the
operating system.
-Here are three FileDialog constructors:
FileDialog(Frame parent)
-FileDialog(Frame parent, String boxName)
-FileDialog(Frame parent, String boxName, int how)