Unit-2 Ajava
Unit-2 Ajava
Unit-2 Ajava
Introduction to
Abstract Window
Toolkit:(AWT)
INDEX
2.1 Working with Windows and AWT :
AWT classes hierarchy
Windows Fundamentals
2.2 Working with frame windows :
creating a frame window in applet,
Canvas,
Creating windowed program
2.3 Working with graphics –
AWT Controls: Labels, TextField, Push buttons
2.4 Layout Managers:
Flow Layout,
Border Layout,
Grid Layout,
Card Layout)
2.5 GUI with Swing using :
JApplet, JLabel, JTextField, JButton, JCheckBox,
JRadioButton, JComboBox, Menus
Checkbox
Component Panel Applet
Choice
FileDialog
Container
Java.lang Dialog
List window
Objcet Frame
Scrollbar
TextArea
TextComponent
TextField
MenuBar
MenuComponent Menu
MenuItem
CheckboxMenuItem
WINDOWS FUNDAMENTAL
A window provides a top level window on the screen ,with no
borders or menu bar.
Constructor:
public Window(Frame parent)
When parent is minimized ,Window is also
minimized. So we must create Frame before we create a
Window.
Container
Window Panel
There are two basic types of container:
1) Window class : this class creates popup windows
seperate from the main program.
It has two subclasses
1) Frame(window that have a border and a menu bar)
2) Dialog (a special window used in applications to select a file)
2) Panel class :
A container that represents a series of an existing
window.
the applet class is a container that is a subclasss of the
panel class.
you can place components directly on applet panel or
use additional panel objects to subdivide the applet into
smalller sections.
PANEL CLASS
Panel is the basic building block of an applet. It provides a
container with no special features.
Methods:
1.getText()
2.setText(String)
3.GetAlignment()
4.SetAlignment(int)
TEXTFIELDS
TextFields is used for inserting a single line of text.
It creates an empty text field with an unspecified number
of columns.
TextField t1=new TextField (8);
add(t);
TextFields Constuctors:
1) TextField():creates an empty TextField is 0 character
wide.
2) TextField(int):creates empty text field.int eger
argument defines no. Of characters to display.
3) TextField(String):creates a text fields initialized with
given string.
4) TextField(String ,int):creates a text field some
number of characters wide.
Methods Description
1. getText() Returns text contained in textfield
2. setText (String) Puts the text in text fields
3. getColumns() Returns width of this text field
4. select(int,int) Selects text between two integer
positions
5. selectAll() Selects all text in the field
6. isEditale() Returns true or false based on
whether text is editable.
True enables text to be edited, false
7. setEditable(boolea
freezes the text
n)
TextArea constructor:
1. TextArea():creates an empty text area 0 rows long
and 0 characters wide.
2. TextArea(int,int):creates an empty text area with
given number of rows and columns
3. TextArea (String):creates a text area displaying the
given string.
4. TextArea(String,int,int):creates a text area displaying
the given string and with given dimensions
Methods:
1. getColumns()
2. getRows()
Syntax:
Checkbox ch1=new Checkbox("pen");
add(ch1);
Methods:
getLabel() setLabel(String) getItem( )
getState() setState(boolean) getItemSelectable( )
getstateChange( )
Constructor :
Checkbox ( )
Another methods
void addItemListener(ItemListener il)
List constructor:
1) List() :
getOrientation() :
0 for Horizontal
1 for vertical
getValue()
setValue()
setLineIncrement()
setLineIncrement()
Object
Menu Component
type title her
Manubar MenuItem
CheckboxMenuItem Menu
PopupMenu
A MenuBar component is a horizontal menu in which
contains on more Menu objects.
It can only be added to Frame object.
It forms the root of all menu trees.
A Frame can display only one MenuBar at a time.
The Menu Bar does not support any listener.
MenuBar Constructor:
- MenuBar ( ): to create default MenuBar.
Menu:
A menu component provides a basic pull down menu.
It can be added either to a menu bar or to another
Menu.
Menus are used to display and control Menu items.
Menu Constructor:
Menu () : to create default menu.
Menu (String str): str specifies name of the menu
selection
Menu(String str, Boolean flag):flag shows popup
menu if set true it can be removed and allowed to
float free.
MenuItem
MenuItem component are text leaf nodes of a menu
tree.
MenuItem are added to a Menu.
MenuItem constructor:
MenuItem () :create s a default constructor
CheckboxMenuItem constructor:
CheckboxMenuItem() :
CheckboxMenuItem(String str):
BorderLayout
CardLayout
FlowLayout
Java.lang
GridBagLAyout
Objcet
GridBagConstarints
GridLayout
FLOW LAYOUT
It is default layout for applets and Panels.
setLayout(f1)
Buttons and Flow Layout
width=400 height=50
width=100 height=120
GRID LAYOUT
The Grid layout class puts each component into a place on a
grid that is equal in size to all the other places.
#1 #2
#3 #4
#5 #6
row=1 col=0
BORDER LAYOUT
Border layout is the default layout manager for all window,
dialog and frame class.
In Border layout ,components are added to the edges of the
container ,the center area is allotted all the space that’s left
over.
North
east
west
center
south
add(bt1,BorderLayout.NORTH);
add(bt2,BorderLayout.SOUTH);
CARD LAYOUT
Card layout class is a special type of layout organizer.
Component
Container
JComponent
JLabel
JTextField
JButton
JCheckBox
JRadioButton
JComboBox
JtabbedPane (Menus)
1. JApplet :
Fundamental of Swing is JApplet class, which extends
Applet.
Applets that uses Swing must be subclasses of JApplet.
JApplet is rich with functionality .It supports various
“panes”,such as content pane, glass pane and root pane.
When adding a component to an instance of JApplet,do not
invoke the add( ) method of the applet. But call add( )for
content pane of the JApplet object.
We can obtain content pane using getContentPane( )
method.
The add( ) method of container can be used to add a
component to content pane.
void add (component comp)
//component which you want to add
2. JLabel:
Swing labels are instances of JLabel class. which extends
JComponent.
It can display text and/or icon.
Constructor of JLabel
JLabel( Icon i)
JLabel(String s)
JLabel (String s , Icon I , int align)
//Align is either
LEFT,RIGHT,CENTER,LEADING or TRAILING.
Methods of JLabel:
Icon getIcon( )
String getText( )
void setIcon( )
void setText( )
// Swing application Program using JFrame
import java.awt.*;
import javax.swing.*;
public class JLabelDemo extends JFrame
{
JLabel lb;
JLabelDemo()
{
super("JLabel Demo");
lb = new JLabel("Welcome TO ADVANCE JAVA");
add(lb);
setVisible(true);
setSize(300,300);
}
public static void main(String args[])
{ new JLabelDemo(); }
}
// applet Program using JApplet
import java.awt.*;
import javax.swing.*;
/*<applet code="JLabelDemo.class" width=400 height=500>
</applet>*/
public class JLabelDemo extends JApplet
{
JLabel jlb1=new JLabel("welcome........");
Constructor
JTextField( )
JTextField(int cols)
JTextField(String s,int cols)
JTextField(String s)
Swing buttons provide features that are not found in the
Button class defined by the AWT.
Swing buttons are subclasses of AbstractButton class which
extends JComponent.
AbstractButton is a super class for push buttons, checkboxes
and radio buttons. It contains many methods that allow you
to control the behaviour of buttons, checkboxes and radio
buttons.
void setDisabledIcon(Icon)
void setPressedIcon (Icon)
void setSelectedIcon (Icon)
void setRolloverIcon(Icon)
Concrete subclasses of AbstractButton generate action
events when they are pressed. Method used for even
handling are,
void addActionListener( ActionListener l)
void removeActionListener(ActionListener al)
JButton:
Methods for Button
1. getText( )
2. setText(String s)
1. JButton(Icon i)
2. JButton(String s)
3. JButton(String s,Icon i) //s and i are the string
and icon used for the button.
JCHECKBOX
JCheckBox class which provides the functionality of a check
box, is a subclass of AbstractButton class. Its immediate super
class is JToggleButton.
It supports two states true or false. We can associate an icon,
string, or the state with the checkbox.
JCheckBox constructors:
1. JCheckBox(Icon c)
2. JCheckBox(Icon c,boolean state)
3. JCheckBox(String s)
4. JCheckBox (String s,boolean state)
5. JCheckBox (String s,Icon i)
6. JCheckBox (String s,Icon c,boolean state)
Here, i is the icon for the button.s is text to be displayed.
The getText( ) method gets the text for that check box and
text inside the text field.
JRadioButton (String s)
Constructor of JComboBox
JComboBox( )
JComboBox(Vector v) //v is vector that initializes the
combo box.
Items are added to the list of choices via
addItem()method,
void addItem( Object obj) //obj is object to be added to
the combo box
JTabbedPane
A tabbed pane is a component that appears as a group of
folders in a file cabinet. Each folder has a title. When a user
selects a folder, its content becomes visible. Only one of the
folders may be selected at a time.
Tabbed panes are commonly used for setting configuration
options.
Tabbed panes are encapsulated by the JTabbedPane class,
extends JComponent.
We will use its default constructor. Tabs are defined via the
following method.
void addTab(String str, Component comp)
str is title for the tab
comp is the component that should added to the tab.
Typically, a JPanel or a subclass of it is added.
The general procedure to use a tabbed pane in an applet
is outlined here.
Class constructors
ActionEvent (Object src, int id, String cmd): Constructs an
ActionEvent object.
ActionEvent (Object source, int id, String cmd, int
modifiers): Constructs an ActionEvent object with modifier
keys.
ActionEvent (Object source, int id, String cmd,long when,
int modifiers): Constructs an ActionEvent object with the
specified modifier keys and timestamp.
Class methods :
String getActionCommand ( ): Returns the command
string associated with this action. For example, when a
button is pressed, an action event is generated that has
a command name equal to the label on that button.
Fields
Following are the fields for java.awt.event.MouseEvent class:
Class declaration
Following is the declaration for java.awt.event.WindowEvent
class:
public class WindowEvent extends ComponentEvent
There are ten types of window events:
static int WINDOW_ACTIVATED --The window-activated
event type.
static int WINDOW_CLOSED -- The window closed event.
Interface methods
void keyPressed(KeyEvent e):Invoked when a key has been
pressed.
void keyReleased(KeyEvent e):Invoked when a key has been
released.
void keyTyped(KeyEvent e) :Invoked when a key has been
typed.
EVENT TYPES AND CORRESPONDING EVENT SOURCE &
EVENTLISTENER
Event Type Event Source Event Listener interface
ActionEvent Button, List, MenuItem, ActionListener
TextField
AdjustmentEvent Scrollbar AdjustmentListener
ItemEvent Choice, Checkbox, ItemListener
CheckboxMenuItem, List
TextEvent TextArea, TextField TextListener
ComponentEvent Component ComponentListener
ContainerEvent Container ContainerListener
FocusEvent Component FocusListener
KeyEvent Component KeyListener
MouseEvent Component MouseListener,
MouseMotionListener
WindowEvent Window WindowListener
ADAPTER CLASSES
Event adapters facilitate implementing listener interfaces.
Many event listener interfaces have more than one event
listener methods. For such interfaces, Java technology defines
adapter classes.
These have empty implementation (stubs) of all the event
listener methods defined in the interface they implement.
A listener can subclass the adapter and override only stub
methods for handling events of interest.
The table below lists the low level event listener interfaces and
their adapters.
Event Listener interface Event Listener Adapter
ComponentListener ComponentAdapter
ContainerListener ContainerAdapter
FocusListener FocusAdapter
KeyListener KeyAdapter
MouseListener MouseAdapter
MouseMotionListener MouseMotionAdapter
WindowListener WindowAdapter
QUESTION
1. Give full name :AWT ,GUI ,URL
2. Compare Label and JLabel.
3. List out layout managers. Explain any one with
example.
4. Explain Frame class in brief.
5. Explain Panel class in brief.
6. Explain event classes.
7. Explain event listener interfaces.
8. Explain ActionListener interface.
9. Compare : swing(JFC) and AWT controls
Thank you