Unit 1 Part1
Unit 1 Part1
1. AWT
Explanation -
• Java AWT (Abstract Window Toolkit) package is a library used for designing graphical
user interfaces.
• It contains classes for placing various user intractable components and graphics.
• The AWT package has classes using which we can create Text Boxes, Check Boxes,
Radio Button, List, etc.
• The following diagram shows the hierarchy of AWT Components.
22517 Advance Java Programming
• Object: All classes in Java are inherited from the object class.
Component:
o public void setHeight (int height)- Sets the height of the component in pixel
o public void setSize (int width, int height)-Sets the width and height of the component
in pixel
o public int getWidth ( )-Returns the width of the component in pixel
o public int getHeight ( )-Returns the height of the component in pixel
o public void setX (int x)-Sets the x position of the component in pixel
o public Dimension getSize ( )-Returns x and y point of the component
o public void setY (int y) Sets the y position of the component in pixel
o public void setLocation (int x, int y) Sets the location of the component to specified
pixels
o public int getX() Returns the x position of the component.
o public int getY() Returns the y position of the component.
o public Point getLocation()- Returns X and Y pf the component
o public void setBounds(int x, int y, int width, int height)-Sets the Location and Size of
component.
o public Rectangle getBounds()- Returns location and size.
o public void setBackground(Color c)- Set background color
o public Color getBackground() Returns the current background color of the component
o public void setForeground(Color c) Sets the foreground color of the component
o public Color getForeground() Returns the current foreground color of the component
o public void setFont(Font f) Sets the font of the component
o public Font getFont() Returns the current font of the component
o public void paint(Graphics g) It paints the component with specified graphics
o public void update(Graphics g) It updates the component with specified graphics
o public void repaint() It repaints the component
o public void setVisible(boolean value) Shows or hides the component based on
specified Boolean value(true or false)
o public boolean isVisible() It returns true if component is visible else returns
o public void setEnabled(boolean value) Enable or disable the component based on
22517 Advance Java Programming
3.Container:
• A Container class is used to contain other components in it.
• It also arranges the components according to the different layouts (eg. BorderLayout,
GridLayout, FlowLayout etc) set to it.
• It provides method for adding and removing component in it.
• Methods:
o public Component add(Component obj) It adds the specified component in
container
o public void remove(Component obj) It removes the specified component from
the container
o public void setLayout(LayoutManager obj) Sets the specified layout to the
container
4.Window:
5.Panel:
• Panel is a concrete subclass of Container.
• It doesn’t add any new methods which simply implements Container.
• Panel is the immediate superclass of Applet
• Panel is one type of container that does not contain a title bar, menu bar, or border.
• When you run an applet using an applet viewer, the applet viewer provides the title
and border.
• Uses add() to add component defined by container
6.Applet:
• Applet is a public class which is predefined by java.applet.Applet.
• There is no main() method in Applet like Application program.
• The main() method is defined by browser or Appletviewer for Applet.
• Life cycle methods: init, start, paint, stop, destroy
• Applet is one type of container and subclass of Panel. Applet is superclass of JApplet
7.Frame:
• A Frame class is used to create a top-level window which can contain other
components in it.
• A Frame can have menu bar, title bar and resizing borders.
• Constructor:
o public Frame() It creates a frame which does not have any title
o public Frame(String title) It creates a frame with specified title
• Methods:
o public void setMenuBar(MenuBar obj) Sets the specified menu bar to the
frame
o public MenuBar getMenuBar()Returns the MenuBar currently set to the
frame
8.AWT Controlls:
• Label
• Button
22517 Advance Java Programming
• Checkbox
• Checkbox group
• Text field
• Text Area
8.1. Label
• Constructor:
o public Label()-It creates a label which does not have any text on it
o public Label(String text, int alignment)- It creates a label having a specified text
on it with specified alignment. Different alignment constants are Label.LEFT,
Label.RIGHT, Label.CENTER
• Methods:
o public void setLabel(String text) Sets the specified text on the label component
o public String getLabel() Returns the current text set on the label component.
o public void setAlignment(int alignment) It sets the specified alignment for the
label. Different alignment constants are Label.LEFT, Label.RIGHT, Label.CENTER
o public int getAlignment() Returns the alignment constant current set to the
Label.
8.2 Button:
• Constructors:
• Methods:
8.3 Checkbox:
• A check box is a graphical component that can be in either an "on" (true) or "off"
(false) state.
• Clicking on a check box changes its state from "on" to "off," or from "off" to "on."
• Constructor:
• Methods:
o public void setLabel(String text) Sets the specified text on the Checkbox
o public String getLabel() Returns the text current present on the checkbox.
o public void setState(Boolean value) Sets the state the checkbox either on or off
based on the specified boolean value (true or false)
8.4 CheckboxGroup:
• It is possible to create 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. These check
boxes are often called radio buttons
• Constructors:
• Methods:
8.5 TextField:
• Constructor:
o public TextField(String text) Create a single line text box filled with
specified text
o public TextField(int columns) Creates an empty single line text box having
a width of specified columns
o public TextField(String text, int columns) Creates a single line text box
filled with specified text and having a width of specified columns
• Methods:
o public void setColumns(int columns) Sets the width of text box to specified
columns
o public char getEchoChar() Returns the echo character set for the text box.
8.6 TextArea:
• Constructor:
o public TextArea(int rows, int columns) Creates an empty multi line text
box having a height of specified rows and width of specified columns.
22517 Advance Java Programming
o public TextArea(String text, int rows, int columns) Creates a multiline text
box filled with specified text and having a height of specified rows and
width of specified columns.
o public TextArea(String text, int rows, int columns, int scrollbars) Creates a
multiline text box filled with specified text and having a height of specified
rows and width of specified columns. The scrollbars represents the
visibility of horizontal and vertical scrollbars for the text area. Different
scrollbar constants are –
• TextArea.SCROLLBARS_BOTH
• TextArea.SCROLLBARS_VERTICAL_ONLY
• TextArea.SCROLLBARS_HORIZONTAL_ONLY
• TextArea.SCROLLBARS_NONE
• Methods:
public void setColumns(int columns) Sets the width of text area to specified
columns
o public void setRows(int rows) Sets the height of text area to specified
rows
o public void insert(String text, int position) Inserts the given text at a
specified position in the text area
o public void append(String text) Appends the given text at the end of the
current text
o public void replaceRange(String text, int start, int end) in the text area
Replaces text between the given start and end positions with the specified
22517 Advance Java Programming
text.
▪ Coice:
• The Choice class represents a pop-up menu of string items out of which user can
select only one at a time.
• Constructor:
o public Choice() Creates a new choice menu. The menu initially has no
items in it.
• Methods:
o public void add(String item) Adds a specified items at the end of the
popup menu
o public void insert(String item, int index) Inserts a given string item at the
specified index in the popup menu
o public void remove(String item) Removes the specified item from the
popup menu
o public void remove(int index) Removes the item from specified index
from the popup menu
o public void removeAll() Removes all the items from the popup menu
o public String getSelectedItem() Returns the selected item from the popup
menu
o public String getItem(int index) Returns the item from the specified index
from the popup menu
o public void select(int index) Sets the item at specified index as selected
item in popup menu
o public void select(String item) Sets the specified item as selected item in
popup menu
▪ List:
• The List class represents a scrolling list of string items out of which user can
select one or more items at a time.
• List object can be constructed to show any number of choices in the visible
window
• Constructor:
o public List() Create a scrolling list of string items out of which user can
select only one at a time.
o public List(int rows) Create a scrolling list of string items out of which
user can select only one at a time. The list has a height of specified
rows.
• Methods:
o public void add(String item) Adds a specified items at the end of the
popup menu
o public void add(String item, int index) Inserts a given string item at the
specified index in the popup menu
o public void remove(String item) Removes the specified item from the
popup menu
o public void remove(int index) Removes the item from specified index
from the popup menu
o public void removeAll() Removes all the items from the popup menu
o public String getItem(int index) Returns the item from the specified
index from the popup menu
o public void select(int index) Sets the item at specified index as selected
item in popup menu
22517 Advance Java Programming
o public void select(String item) Sets the specified item as selected item
in popup menu
• Top-level window can have a menu bar associated with it. A menu bar displays a
list of top-level menuchoices. Each choice is associated with a drop-down
menu.
• Classes:
o MenuBar
o Menu
o MenuItem
1 MenuBar:
• Constructor:
• Methods:
o public void setHelpMenu(Menu m)- Sets the specified menu as the menubar’s help
menu.
o public void remove(Menu m) -Removes the specified menu from menu bar
o public void remove(index n)-Removes the menu from the specified index on menubar
22517 Advance Java Programming
o public Menu getMenu(int index)- Returns the menu available at specified index
on menubar
2.Menu:
• The Menu class is used to create pull down menu which is a list of menu items. It
can be displayed on the menubar
• Constructor:
• Method:
o public MenuItem add(MenuItem m) - Adds the specified menu item in the menu
o public void insert(MenuItem m, int index) -Inserts the specified menu item at
specified index in menu
o public void remove(MenuItem m)- Removes the specified menu item from menu
22517 Advance Java Programming
o public void remove(index n)-Removes the menu item from the specified index in
menu.
o public void removeAll() -Removes all the menu items from the menu
o public int geItemCount()- Returns the number of menu items present in the menu
3.MenuItem:
• The MenuItem class is used to create a menu item which can be clicked using
mouse. It can be displayed on the menu.
• Constructor:
o public MenuItem(String label, MenuShortcut shortcut) -Create a menu item with specified
label on it. A menu shortcut represent the keyboard shortcut for accessing this menu item.
• Method:
o public void setShortcut(MenuShortcut shortcut)-Sets the specified shortcut for accessing this
menu item
o public MenuShortcut getShortcut()- Returns the shortcut currently set this menu item
▪ MenuShortCut:
• The MenuShortcut class is used to create keyboard shortcut for accessing menu
items
22517 Advance Java Programming
• Constructor:
o public MenuShortcut(int key)- Creates a menu shortcut for the specified virtual key
o public MenuShortcut(int key, boolean use_shift_key) -Creates a menu shortcut for the
specified virtual key. use of shift key is decided by true or false value.
▪ Layout Managers
▪ The LayoutManagers are used to arrange components in a particular manner. The Java
LayoutManagers facilitate us to control the positioning and size of the components in GUI forms.
LayoutManager is an interface that is implemented by all the classes of layout managers.
FlowLayout (java.awt.FlowLayout) - The Java FlowLayout class is used to arrange the components in a
line, one after another (in a flow). It is the default layout of the applet or panel.
Fields of FlowLayout
Constructors
• FlowLayout() - creates a flow layout with centered alignment and a default 5 unit horizontal and
vertical gap.
• FlowLayout(int align) - creates a flow layout with the given alignment and a default 5 unit horizontal
and vertical gap.
• FlowLayout(int align, int hgap, int vgap) - creates a flow layout with the given alignment and the
given horizontal and vertical gap.
22517 Advance Java Programming
Fields of BorderLayout
Constructors
• BorderLayout() - creates a border layout but with no gaps between the components.
• BorderLayout(int hgap, int vgap) - creates a border layout with the given horizontal and vertical gaps
between the components.
GridLayout (java.awt.GridLayout) - The Java GridLayout class is used to arrange the components in a
rectangular grid. One component is displayed in each rectangle.
22517 Advance Java Programming
Constructors
• GridLayout(): creates a grid layout with one column per component in a row.
• GridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps
between the components.
• GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and
columns along with given horizontal and vertical gaps.
GridBagLayout - The Java GridBagLayout class is used to align components vertically, horizontally or along
their baseline.
Constructor
GridBagLayout(): The parameterless constructor is used to create a grid bag layout manager.
CardLayout - The Java CardLayout class manages the components in such a manner that only one
component is visible at a time. It treats each component as a card that is why it is known as CardLayout.
Constructor
• CardLayout(): creates a card layout with zero horizontal and vertical gap.
• CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and vertical gap.
22517 Advance Java Programming
• public void next(Container parent): is used to flip to the next card of the given container.
• public void previous(Container parent): is used to flip to the previous card of the given container.
• public void first(Container parent): is used to flip to the first card of the given container.
• public void last(Container parent): is used to flip to the last card of the given container.
• public void show(Container parent, String name): is used to flip to the specified card with the given
name.
Dialog
To create Dialog Box: Create Frame or Applet Create another class which extends Dialog
class. Call this new class from Frame/Applet class. In constructor of Extended Dialog class,
use super method and pass vales to constructor of Dialog
Constructor
Methos
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
Constructor
Methods
• Void setFile(String file)-method sets the selected file for this dialog window to be the
specified file.
• Void setFMode(int mode)-method sets the mode of the file dialog
• String getDirectory()- method gets the file directory of this file dialog.