0% found this document useful (0 votes)
32 views

Unit 1 Part1

Tha hi nahi

Uploaded by

landgeom90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Unit 1 Part1

Tha hi nahi

Uploaded by

landgeom90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

22517 Advance Java Programming

Unit I – Abstract Window Toolkit (AWT)

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.

• However, the components of this class are platform dependent.

• They are heavy and rely on the OS for their functioning.

• 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.

• Components: Elements like buttons, TextBox, RadioButton, etc. are called


components in Java. We have separate classes for all components. To display
elements on the screen, we need to place them in containers.
• Container: It consists of classes that are used to hold other objects. All components
are placed in containers, like Windows and Panels. Containers control the placement
and layout of the components placed in them. A container in itself is a component.
• Label: As the name suggests, it is used as a label, or a method to display text on the
screen. The user cannot change the content of a label. It represents a read-only text.
• Button: A button is like a push button. It consists of a label (name of the button)
and generates an event when clicked.
• Checkbox: It is used to provide options. When clicked, it changes the state of the
checkbox from 'on' to 'off' or vice-versa.
• TextField: It is used to take single-line text input from the user. It can also be used
to display editable text. Unlike a label, which provides read-only text, the text in
the textfield can be edited by the user.
• TextArea: It allows the display of multiple lines of text. It provides 'an area' where
text can be displayed and also edited by the user.

Component:

A component is an object having a graphical representation that can be displayed on the


screen and that can interact with the user.
• Examples of components are the buttons, checkboxes, and scrollbars of a typical graphical
user interface.
• The Component class is the abstract superclass of all Abstract Window Toolkit
components.
• methods of Component class-
o public void setWidth (int width)-Sets the width of the component in pixel
22517 Advance Java Programming

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

specified boolean value(true or false)


o public boolean isEnabled() false It returns true if component is enabled else returns
false

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:

• A Window is a Container class which is used to contain other components in it.


• Window class is used to create a top-level window which can appear directly on
screen.
• Window has a title bar but does not have menu bar.
• Default layout for window is BorderLayout.
• Methods:
o public void setTitle(String text) Sets the specified title to the window’s title bar
o public String getTitle()Returns the current title set to the window’s title bar
22517 Advance Java Programming

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

• Used to just display string on window.

• Label is a Passive Component(It does not generate any event)

• Constructor:

o public Label()-It creates a label which does not have any text on it

o public Label(int alignment)- It creates an empty label with specified alignment

o public Label(String text)- It creates a label having a specified text on it Default


alignment for this label is LEFT.

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:

• It contains a label and that generates an event when it is pressed.


22517 Advance Java Programming

• Button is an Active Components

• Constructors:

o Public Button() Create a button without label

o Public Button(String str) Create a button with label

• Methods:

o void setLabel(String str)-set a specified text on the label

o String getLabel( )-returns the current text present on button

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."

• It is used to select one or all options from the group.

• Constructor:

o public Checkbox() Creates a checkbox without any label on it.

o public Checkbox(String label) Creates a checkbox with a specified label on it.

o public Checkbox(String label, boolean state) Creates a checkbox with a


specified label on it. It can be selected or deselected based on the specified
boolean value (true or false).

o public Checkbox(String label, boolean state, CheckboxGroup group) Creates a


radio button with a specified label on it. It can be selected or deselected based
on the specified boolean value (true or false).

o public Checkbox(String label, CheckboxGroup group, boolean state)- Creates a


radio button with a specified label on it. It can be selected or deselected based
on the specified boolean value (true or false). Only one button from the
22517 Advance Java Programming

specified CheckboxGroup can be selected at a time.

• 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)

o public boolean getState() Returns true if the checkbox is selected otherwise


returns 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:

o public void setCheckboxGroup (CheckboxGroup group) Sets the checkbox


group for this checkbox.

o public CheckboxGroup getCheckboxGroup() Returns the checkbox group


currently set for this checkbox

• Methods:

o Checkbox getSelectedCheckbox( )- Returns selected Checkbox

o void setSelectedCheckbox(Checkbox which)-Turn on Passed Checkbox

8.5 TextField:

• The TextField class is an immediate subclass of TextComponent class.

• It is used to create single line text box as well as password box.


22517 Advance Java Programming

• Constructor:

o public TextField() Create an empty single line text box.

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 int getColumns() Returns the width of text box in columns

o public void setEchoChar(char c) Set the echo character (password character)


for text box

o public char getEchoChar() Returns the echo character set for the text box.

8.6 TextArea:

• The TextArea class is an immediate subclass of TextComponent class.

• It is used to create multi line text box.

• Constructor:

o public TextArea() Create an empty multi line text box.

o public TextArea(String text) Create a multiline text box filled with


specified text

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 int getColumns() Returns the width of text area in columns

o public void setRows(int rows) Sets the height of text area to specified
rows

o public int getRows() Returns the height of text area in 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.

• The current choice is displayed as the title of the menu.

• Used to create a pop-up list items.

• Default constructor Choice( ) create emptylist.

• 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 int getSelectedIndex() Returns the position of the selected item


from the popup menu
22517 Advance Java Programming

o public String getItem(int index) Returns the item from the specified index
from the popup menu

o public int getItemCount() Returns the total number of items present in


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 class provides a compact, multiple-choice, scrolling selection list.

• 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.

o public List(int rows, boolean multiselect) Create a scrolling list of string


items out of which user can select one or more items at a time based
on the specified boolean value (true or false). The list has a height of
specified rows.
22517 Advance Java Programming

• 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 getSelectedItem() Returns the selected item from the


popup menu

o public String[] getSelectedItems() Returns an array of all selected items


from the popup menu

o public int getSelectedIndex() Returns the position of the selected item


from the popup menu

o public int[] getSelectedIndexes() Returns an array of index of all


selected items from the popup menu

o public String getItem(int index) Returns the item from the specified
index from the popup menu

o public int getItemCount() Returns the total number of items present in


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

o public void setMultipleMode(boolean v) Determines whether this list


allows multiple selection or not based on the boolean value (true or
false)

▪ Working with Menu and MenuBar:

• 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:

• The MenuBar class is used to implement menubar to display different menus on


it. It can be displayed on the Frame, just below the titlebar

• Constructor:

o MenuBar() -Creates a menubar.

• Methods:

o public Menu add(Menu m)-Adds the specified menu on the menubar

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

o public int getMenuCount()- Returns the number of menus 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:

o public Menu() - Creates a menu without any label on it

o public Menu(String label) Creates a menu with specified label on it

o public Menu(String label, boolean tearoff)- Create a menu with specified


label on it. A tear-off property can be set as true or false. A tear-off menu
can be opened and dragged away from its parent menu bar or menu

o public Menu(String label, boolean tearoff)- Create a menu with specified


label on it. A tear-off property can be set as true or false. A tear-off menu
can be opened and dragged away from its parent menu bar or menu

• Method:

o public MenuItem add(MenuItem m) - Adds the specified menu item in the menu

o public void addSeparator()-Adds a separator line on this menu

o public void insert(MenuItem m, int index) -Inserts the specified menu item at
specified index in menu

o public void insertSeparator(index n) -Inserts a separator line at specified position on


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

o public MenuItem getItem(int index)-Returns the menu item available at specified


index on 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()-Creates a menu item without any label on it

o public MenuItem(String label) -Creates a menu item with specified label on it

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 String getLabel()-Returns the current label from menu item

o public void setLabel(String label)-Sets the specified label on menu item

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

• public static final int LEFT


• public static final int RIGHT
• public static final int CENTER
• public static final int LEADING
• public static final int TRAILING

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

BorderLayout (java.awt.BorderLayout) - The BorderLayout is used to arrange the components in five


regions: north, south, east, west, and center. Each region (area) may contain one component only. It is the
default layout of a frame or window.

Fields of BorderLayout

• public static final int NORTH


• public static final int SOUTH
• public static final int EAST
• public static final int WEST
• public static final int CENTER

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

Methods of CardLayout Class

• 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

public class Dialog extends Window

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

• Dialog(Dialog owner) - Constructs an initially invisible, modeless Dialog with the


specified owner Dialog and an empty title.
• Dialog(Dialog owner, String title) - Constructs an initially invisible, modeless Dialog
with the specified owner Dialog and title.
• Dialog(Dialog owner, String title, boolean modal) - Constructs an initially invisible
Dialog with the specified owner Dialog, title, and modality.
• Dialog(Frame owner)- Constructs an initially invisible, modeless Dialog with the
specified owner Frame and an empty title.
• Dialog(Frame owner, boolean modal)- Constructs an initially invisible Dialog with the
specified owner Frame and modality and an empty title.
• Dialog(Frame owner, String title) - Constructs an initially invisible, modeless Dialog
with the specified owner Frame and title.
• Dialog(Frame owner, String title, boolean modal) - Constructs an initially invisible
Dialog with the specified owner Frame, title and modality.
22517 Advance Java Programming

Methos

• Void setTitle(String title)


• Void setSize(int width, int heigh)
• Vois setVisible(boolean visible)
• Void dispose()

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

• FileDialog(Dialog parent) - Creates a file dialog for loading a file.


• FileDialog(Dialog parent, String title) - Creates a file dialog window with the specified
title for loading a file.
• FileDialog(Dialog parent, String title, int mode) - Creates a file dialog window with the
specified title for loading or saving a file.
• FileDialog(Frame parent) - Creates a file dialog for loading a file.
• FileDialog(Frame parent, String title) - Creates a file dialog window with the specified
title for loading a file.
• FileDialog(Frame parent, String title, int mode) - Creates a file dialog window with the
specified title for loading or saving a file.

Methods

• String getfile()-Method gets the selected file of this file dialog


• Int getMode()-method indicates whether this file dialog box is for loading from a file or
for saving to a file.
• Void setDirectory(String dir)-method sets the directory of this file dialog window to be
the specified directory.
22517 Advance Java Programming

• 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.

You might also like