際際滷

際際滷Share a Scribd company logo
UNIT-1
AWT & SWING
Introduction to AWT
AWT Classes
 The AWT classes are contained in the java.awt package.
Window Fundamentals
 AWT defines windows according to a class hierarchy that adds
functionality & specificity in each level.
 The two most common windows are
 those derived from Panel
 those derived from Frame
 The following fig. shows the class hierarchy for Panel and Frame
Component
Container
Window Panel
Frame Applet
Component
 All user interface elements that are displayed on the screen and that
interact with the user are subclasses of Component.
 Responsible for managing events, such as mouse and keyboard input,
positioning and sizing the window, and repainting.
Container
 subclass of Component
 Responsible for laying out(i.e. positioning) any components that it
contain.
 It does this through the use of various layout managers.
Panel
 Concrete subclass of Container.
 No new methods; it simply implements Container.
 Panel is the superclass for Applet.
 Panel is a window that does not contain a title bar, menu bar or
border. (AppletViewer provides this; browser wont)
 Components can be added to a Panel object by its add( )
method.
 Once added, they can be positioned and resized manually using the
setLocation( ), setSize( ), or setBounds( ) methods defined by
Component.
Window
 creates top-level window.
 Window objects generally are not created directly; instead we will use a
subclass of Window called Frame.
Frame
 subclass of Window and has a title bar, menu bar, border and resizing
corners.
 Frame object can be created from within an applet window
 it will contain a message Java Applet Window
 When a Frame window is created by a program ; a normal window is
created.
Canvas
 not part of hierarchy.
 Canvas encapsulates a blank window upon which you can draw.
Commonly Used Methods of Component Class
 setBackground(): used to change the background color of a component.
public void setBackground ( Color c)
Note: color is a helper class.
 setForeground(): used to change the foreground color of a
component.
public void setForeground ( Font obj)
 Font is represented by java.awt.Font.
public Font (String FontName, int Style, int Size)
e.g. : Font f = (Times New Roman, Font.BOLD, 20);
 setBounds(): used to specify size and position of component in a container.
public void setBounds (int left, int top, int width, int height)
Commonly used methods of Container
Class
 add(): used to add components to a container.
public void add (Component c)
 setSize(): used to specify the size of a container.
public void setSize(int width, int height)
 setLayout(): used to specify the layout manager for a container.
public void setLayout (LayoutManager mgr)
 setVisible(boolean visibility): used to set the visibility of
container.
public void setVisible (boolean visibility)
Working with Frame
Windows
Frames Constructors
 Frame( )
 creates a standard window that does not contain a title.
 Frame(String title)
 creates a window with the title specified by title.
Setting the Windows Dimensions
 void setSize(int newWidth, int newHeight)
 void setSize(Dimension newSize)
 set the dimensions of the window
 Dimension getSize( )
 obtain the current size of a window
Hiding and Showing a Window
 After a frame window has been created, it will not be visible until you
call setVisible( )
void setVisible(boolean visibleFlag)
-> visible if true otherwise hidden
Setting a Windows Title
 void setTitle(String newTitle)
Closing a Frame Window
 Remove window from the screen by calling setVisible(false), when it is
closed.
 If Frame window is a child window, to intercept window-close event,
 implement windowClosing( ) method of the WindowListener interface.
 Inside windowClosing( ), remove the window from the screen.
import java.awt.Frame;
public class SampleFrame extends Frame {
SampleFrame(String title) {
super( );
this.setTitle(title);
this.setSize(300,200);
this.setVisible(true);
}
public static void main(String args[
]) {
SampleFrame sf = new
SampleFrame(My
Window);
Layout managers
 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 form
void setLayout(LayoutManager layoutObj)
layoutObj -> reference to the
desired layout manager
 Java has several predefined LayoutManager
classes.
FlowLayout
 FlowLayout is the default layout manager.
 FlowLayout implements a simple layout style, which is similar to how
words flow in a text editor.
 direction -> by default it is left to right, top to bottom
 By default, components are laid out line-by-line beginning at the upper-left
corner.
 A small space is left between each component, above and below, as well as
left and right.
 Constructors
FlowLayout( ) -> creates the default layout, which centers components
and leaves 5 pixels of space between each component.
FlowLayout(int how)
-> valid values for how are as follows:
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
FlowLayout.LEADING
FlowLayout.TRAILING
FlowLayout(int how, int horz, int vert)
-> horz and vert allows to specify the horizontal and vertical
space between components
BorderLayout
 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( ) -> default border layout
BorderLayout(int horz, int vert)
 When adding components to border layout, call the following add( )
method
void add(Component compObj, Object region)
compObj  component to be added
region  specifies where the component will
be added
 BorderLayout defines the following constants that specify the regions
BorderLayout.CENTER
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.NORTH
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
GridLayout
 GridLayout lays out components in a two-dimensional grid.
 Constructors
GridLayout( )  creates a single-column grid layout
GridLayout(int numRows, int numColumns)
-> creates a grid layout with the specified number of rows and
columns
GridLayout(int numRows, int numColumns, int horz, int vert)
 Specifying numRows as zero allows for unlimited-length columns
 Specifying numColumns as zero allows for unlimited-length rows
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
CardLayout
 It is used to arrange containers in the form of deck of cards.
Methods:
first() / last()/ next()/ previous(): is used to make the first/
last/ next/ previous card visible.
show(): is used to make a specified card visible.
public void show ( Container deck, String CardName)
 To give a name to the container while it is added to the
deck:
public void add ( Container card, String CardName)
AWT
Controls
 The AWT supports the following types of controls
 Labels
 Push Buttons
 Check boxes
 Choice lists
 Lists
 Scroll bars
 Text editing
-> subclasses of Component
Adding and Removing Controls
 To add a control to a window
 first create an instance of the desired control.
 then add it to a window by calling add(
) Component add(Component
compObj)
compObj -> instance of the
control to add
 To remove a control
void remove(Component obj)
obj -> control to be removed
removeAll( ) -> to remove all controls
 Except for labels, which are passive controls, all controls generate
events when they are accessed by user.
 Example
Label
s
 A label is an object of type Label, and it contains a string which it
displays.
 Passive controls  do not support any interaction with the user.
 Label defines the following constructors
Label( )
Label(String str)  string is left-justified
Label(String str, int how)
- alignment specified by how
- value of how must be one of
the 3 constants
- Label.LEFT, Label.RIGHT,
Label.CENTER
 To set or change the text in a label, use the setText( ) method.
void setText(String str)
 To obtain the current label, call getText( )
String getText( )
 To set the alignment of the string within the label, call setAlignment( )
void setAlignment(int how)
 To obtain the current alignment, call getAlignment( )
int getAlignment( )
 Example
Button
s
 A 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.
 Button defines these two constructors
Button( )
Button(String str)  contains str
as a label
 To set the label of a button, call
setLabel( ) void setLabel(String str)
 To retrieve the label of a button, call
getLabel( ) String getLabel( )
Check
Boxes
 A check box is a control that is used to turn an option on or off. It
consists of a small box that can either contain a check mark or not.
 There is a label associated with each check box that describes what
option the box represents.
 Check boxes can be used individually or as part of a group.
 Check boxes are objects of the Checkbox class.
 Checkbox supports these constructors
Checkbox( )  label is initially blank; state of the checkbox is
unchecked.
Checkbox(String str)  label is specified by str; state of the
checkbox is unchecked.
Checkbox(String str, boolean on)
- if on is true, the checkbox is initially checked;
otherwise cleared.
Checkbox(String str, boolean on, CheckboxGroup cbGroup)
- group is specified by cbGroup; if this checkbox is not
part of a group, then cbGroup must be null.
Checkbox(String str, CheckboxGroup cbGroup, boolean on)
 To retrieve the current state of a checkbox, call getState( )
boolean getState( )
 To set the state of a checkbox, call setState( ):
void setState(Boolean on)
 To obtain the current label associated with a checkbox, call getLabel( )
String getLabel( )
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.
 To create a set of mutually excusive check boxes, first define the
group to which they will belong and then specify that group when
constructing the checkboxes.
 Check box groups are objects of type CheckboxGroup
 Only default constructor  CheckboxGroup( ) -> creates an empty
group.
 To determine which checkbox in a group is currently selected, call
getSelectedCheckbox( )
Checkbox getSelectedCheckbox( )
 To set a check box, call setSelectedCheckbox( )
void setSelectedCheckbox(Checkbox which)
which -> checkbox that has to be selected; previously
selected check box will be turned off
 Example
Choice Controls
 The Choice class is used to create a pop-up list of items from which the
user may choose.
 Thus, a 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.
 Choice only defines the default constructor, which creates an empty
list.
 To add a selection to the list, call add( ). Items are added to the list in the
order in which calls to add( ) occur.
void add(String name)
 To determine which item is currently selected, call getSelectedItem( ) or
getSelectedIndex( )
String getSelectedItem( )
-> returns a string containing the name of the item
int getSelectedIndex( )
-> returns the index of the item. The first item is at index 0
 To obtain the number of items in the list, call getItemCount( )
int getItemCount( )
 To set the currently selected item with either a zero-based index or a string that will
match a name in the list, call select( )
void select(int index)
void select(String name)
 To obtain the name
associated with the item at
an index, call getItem( )
Lists
 The List class provides a compact, multiple-choice, scrolling selection list.
 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.
 List provides 3 constructors
List( )  creates a List control that allows only one item to be
selected at any one time.
List(int numRows)  numRows specifies the number of entries in
the list that will always be visible.
List(int numRows, boolean multipleSelect)  if multipleSelect is true,
then the user may select two or more items at a
time. If it is false, only one item may be
selected.
 To add a selection to the list, call add( )
void add(String name)  name is the name of the item added to the list.
void add(String name, int index)  adds the item at the index specified
by index.
 For lists that allow only single selection, to determine which item is currently
selected, call getSelectedItem( ) or getSelectedIndex( )
 For lists that allow multiple selection, call getSelectedItems( ) or
getSelectedIndexes( )
String[ ] getSelectedItems( )
int[ ] getSelectedIndexes( )
TextFields
 The TextField class implements a single-line text-entry area, usually called
an edit control.
 TextField is a subclass of TextComponent.
 TextField defines the following constructors
TextField( )  creates a default text field
TextField(int numChars)  creates a text
field that is numChars wide.
TextField(String str)  initializes the text
field with the string contained
in str.
TextField(String str, int numChars)
 To obtain the string currently contained in the text field, call getText( )
String getText( )
 To set the text, call setText( )
void setText(String str)  str is the new string
 To select a portion of the text in a text field, call
select( )
void select(int startIndex, int endIndex)
- selects the characters beginning at
startIndex and ending at
endIndex  1
 To obtain the currently selected text, call getSelectedText( )
String getSelectedText( )
 To control whether the contents of a text field may be modified by the
user or not, call setEditable( )
void setEditable(boolean canEdit)
- if canEdit is true, the text may be changed. If
it is false, the text cannot be altered.
 To determine the editability, call isEditable( )
boolean isEditable( )
- returns true if the text may be changed and false if not.
 To disable the echoing of characters as they are typed(eg.while typing
passwords), call setEchoChar( ) method.
void setEchoChar(char ch)
- ch specifies the character to be echoed.
 To check a text field to see if it is in this mode, call echoCharIsSet( )
method.
boolean echoCharIsSet( )
 To retrieve the echo character, call getEchoChar( ) method.
char getEchoChar( )
 Example
TextAre
a
 Sometimes, a single line of text input is not enough for a given task. to
handle these situations, the AWT includes a multiline editor called
TextArea.
 Constructors
TextArea( )
TextArea(int numLines, int numChars)
-> numLines specifies the height, in lines, of the text area
-> numChars specifies its width, in characters.
TextArea(String str)
-> initial text can be specified by str
TextArea(String str, int numLines, int numChars)
TextArea(String str, int numLines, int numChars, int sBars)
-> sBars specify the scroll bars. sBars must be one of these
values
SCROLLBARS_BOTH
SCROLLBARS_NONE
SCROLLBARS_HORIZONTAL_ONLY
SCROLLBARS_VERTICAL_ONLY
 TextArea is a subclass of TextComponent.
 It supports the getText( ), setText( ), getSelectedText( ), select,
isEditable( ), and setEditable( )
 TextArea adds the following methods
void append(String str)
-> appends the string specified by str to the end of the
current text.
void insert(String str, int index)
-> inserts the string passed in str at the specified index.
void replaceRange(String str, int startIndex, int endIndex)
-> replaces the characters from startIndex to
endIndex -1,
with the replacement text passed in str.
Scroll Bar
Scroll bars are used to select continuous values between
a specified minimum and maximum.
Scroll bars may be oriented horizontally or vertically.
Scrollbar defines the following constructors:
Scrollbar( ) throws HeadlessException
Scrollbar(int style) throws HeadlessException
Scrollbar(int style, int initialValue, int thumbSize, int min,
int max) throws HeadlessException
Continue
If style is Scrollbar.VERTICAL, a vertical scroll bar is
created.
If style is Scrollbar.HORIZONTAL the scroll bar is
horizontal.
the height of the thumb is passed in thumbSize.
The minimum and maximum values for the scroll bar are
specified by min and max.
Method
void setValues(int initialValue, int thumbSize, int min, int
max)
int getValue( )
void setValue(int newValue)
int getMinimum( )
int getMaximum( )
void setUnitIncrement(int newIncr) //scroll line up and
down
void setBlockIncrement(int newIncr) //scroll page-up and
down
Menu Bars and Menus
 Menu is implemented in AWT by the following classes: MenuBar,
Menu, and MenuItem.
 A menu bar displays a list of top-level menu choices. Each choice is
associated with a drop-down menu.
 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.
 Checkable menu items are menu options of type
CheckboxMenuItem and will have a check mark next to them when
they are selected
Creating Menu
 To create a menu bar, first create an instance of MenuBar.
This class only defines the default constructor.
public MenuBar()
 setMenuBar() method is used to add menubar on window.
 Next, create instances of Menu that will define the selections
displayed on the bar. Following are the constructors for
Menu:
Menu( ) throws HeadlessException
Menu(String optionName) throws HeadlessException
Menu(String optionName, boolean removable) throws
HeadlessException
Individual menu items are of type MenuItem. It defines these
constructors:
MenuItem( ) throws HeadlessException
MenuItem(String itemName) throws HeadlessException
MenuItem(String itemName, MenuShortcut keyAccel) throws
HeadlessException
Methods
 We can disable or enable a menu item by using the setEnabled( )
method.
void setEnabled (boolean enabledFlag)
 If the argument enabledFlag is true, the menu item is enabled. If false,
the menu item is disabled.
 We can determine an items status by calling isEnabled( ).
boolean isEnabled( )
 isEnabled( ) returns true if the menu item on which it is called is
enabled. Otherwise, it returns false.
 We can change the name of a menu item by calling setLabel( ).
We can retrieve the current name by using getLabel( ).
void setLabel(String newName)
String getLabel( )
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
Example
item2 = new MenuItem("Open...");
item3 = new MenuItem("Close");
item4 = new MenuItem("-");
item5 = new MenuItem("Quit...");
// add menu items on menu
file.add(item1); file.add(item2);
file.add(item3); file.add(item4);
file.add(item5);
// add menu on menubar mbar.add(file);
}
Public static void main(String arg[]) {
MenuDemo md=new MenuDemo();
md.setVisible(true);
md.setTitle(Menu);
md.setSize(400,400);
}
Import java.awt.*;
Class MenuDemo extends
Frame
{
MenuDemo()
{
MenuBar mbar = new
MenuBar();
setMenuBar(mbar);
// create the menu and menu
items
Menu file = new Menu("File");
MenuItem item1, item2, item3,
item4, item5;
item1 = new MenuItem
("New...");
Dialog Boxes
 Dialog boxes are primarily used to obtain user input and are
often child windows of a top-level window.
 Dialog boxes dont have menu bars, but in other respects,
they 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 are of type Dialog
 Dialog(Frame parentWindow, boolean mode)
 Dialog(Frame parentWindow, String title, boolean mode)
 parentWindow is the owner of the dialog box.
Continue..
 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.
Example
Create subclass of Dialog
class
Class DemoDialog extends
Dialog{
DemoDialog(Frame par,
String title)
{
Button b1= new Button
(ok); add(b1);
setSize(300,100);
setVisible(true);
}
}
Class Fdemo extends Frame{
Fdemo()
{
setSize(300,400);
}
Public static void main (String
arg[])
{
Fdemo fd=new Fdemo();
Fd.setVisible(true);
DemoDialog dd=new
DemoDialog(fd,First Dialog);
}
File Dialog
 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.
FileDialog constructors:
FileDialog(Frame parent)
FileDialog(Frame parent, String boxName)
FileDialog(Frame parent, String boxName, int how)
If how is FileDialog.LOAD, then the box is selecting a file for
reading.
If how is FileDialog.SAVE, the box is selecting a file for writing. If
how is omitted, the box is selecting a file for reading.
 FileDialog provides methods that allow you to determine the
name of the file and its path as selected by the user. Here are
two examples:
String getDirectory( )
String getFile( )

More Related Content

Similar to UNIT-I.pptx awt advance java abstract windowing toolkit and swing (20)

17625-1.pptx
17625-1.pptx17625-1.pptx
17625-1.pptx
BhaskarDharmadhikari
AWT stands for Abstract Window Toolkit. AWT is collection of classes and int...
AWT stands for Abstract Window Toolkit.  AWT is collection of classes and int...AWT stands for Abstract Window Toolkit.  AWT is collection of classes and int...
AWT stands for Abstract Window Toolkit. AWT is collection of classes and int...
Prashant416351
13457272.ppt
13457272.ppt13457272.ppt
13457272.ppt
aptechaligarh
Unit-1 awt advanced java programming
Unit-1 awt advanced java programmingUnit-1 awt advanced java programming
Unit-1 awt advanced java programming
Amol Gaikwad
Creating GUI.pptx Gui graphical user interface
Creating GUI.pptx Gui graphical user interfaceCreating GUI.pptx Gui graphical user interface
Creating GUI.pptx Gui graphical user interface
pikachu02434
Abstract Window Toolkit_Event Handling_python
Abstract Window Toolkit_Event Handling_pythonAbstract Window Toolkit_Event Handling_python
Abstract Window Toolkit_Event Handling_python
jasminebeulahg
Online birth certificate system and computer engineering
Online birth certificate system and computer engineeringOnline birth certificate system and computer engineering
Online birth certificate system and computer engineering
sayedshaad02
Unit I-AWT-updated.pptx
Unit  I-AWT-updated.pptxUnit  I-AWT-updated.pptx
Unit I-AWT-updated.pptx
ssuser10ef65
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
Abdii Rashid
Abstract Window Toolkit
Abstract Window ToolkitAbstract Window Toolkit
Abstract Window Toolkit
RutvaThakkar1
unit3.3.pptx
unit3.3.pptxunit3.3.pptx
unit3.3.pptx
VeenaNaik23
AWT.pptx
AWT.pptxAWT.pptx
AWT.pptx
FAHMIDAASEEZ1
Understanding layout managers
Understanding layout managersUnderstanding layout managers
Understanding layout managers
Nuha Noor
GUI.pdf
GUI.pdfGUI.pdf
GUI.pdf
Poornima E.G.
Labels and buttons
Labels and buttonsLabels and buttons
Labels and buttons
myrajendra
Awt controls ppt
Awt controls pptAwt controls ppt
Awt controls ppt
soumyaharitha
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
MODULE 5.pptx gui programming and applets
MODULE 5.pptx gui programming and appletsMODULE 5.pptx gui programming and applets
MODULE 5.pptx gui programming and applets
LIKITHLIKITH7
9awt Components
9awt Components9awt Components
9awt Components
Adil Jafri
swinSAXSXZZCXXZXCXCXCVCVCBVCBVBNBNBNHg.pdf
swinSAXSXZZCXXZXCXCXCVCVCBVCBVBNBNBNHg.pdfswinSAXSXZZCXXZXCXCXCVCVCBVCBVBNBNBNHg.pdf
swinSAXSXZZCXXZXCXCXCVCVCBVCBVBNBNBNHg.pdf
KarishmaTamboli4
AWT stands for Abstract Window Toolkit. AWT is collection of classes and int...
AWT stands for Abstract Window Toolkit.  AWT is collection of classes and int...AWT stands for Abstract Window Toolkit.  AWT is collection of classes and int...
AWT stands for Abstract Window Toolkit. AWT is collection of classes and int...
Prashant416351
Unit-1 awt advanced java programming
Unit-1 awt advanced java programmingUnit-1 awt advanced java programming
Unit-1 awt advanced java programming
Amol Gaikwad
Creating GUI.pptx Gui graphical user interface
Creating GUI.pptx Gui graphical user interfaceCreating GUI.pptx Gui graphical user interface
Creating GUI.pptx Gui graphical user interface
pikachu02434
Abstract Window Toolkit_Event Handling_python
Abstract Window Toolkit_Event Handling_pythonAbstract Window Toolkit_Event Handling_python
Abstract Window Toolkit_Event Handling_python
jasminebeulahg
Online birth certificate system and computer engineering
Online birth certificate system and computer engineeringOnline birth certificate system and computer engineering
Online birth certificate system and computer engineering
sayedshaad02
Unit I-AWT-updated.pptx
Unit  I-AWT-updated.pptxUnit  I-AWT-updated.pptx
Unit I-AWT-updated.pptx
ssuser10ef65
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
Abdii Rashid
Abstract Window Toolkit
Abstract Window ToolkitAbstract Window Toolkit
Abstract Window Toolkit
RutvaThakkar1
Understanding layout managers
Understanding layout managersUnderstanding layout managers
Understanding layout managers
Nuha Noor
Labels and buttons
Labels and buttonsLabels and buttons
Labels and buttons
myrajendra
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
MODULE 5.pptx gui programming and applets
MODULE 5.pptx gui programming and appletsMODULE 5.pptx gui programming and applets
MODULE 5.pptx gui programming and applets
LIKITHLIKITH7
9awt Components
9awt Components9awt Components
9awt Components
Adil Jafri
swinSAXSXZZCXXZXCXCXCVCVCBVCBVBNBNBNHg.pdf
swinSAXSXZZCXXZXCXCXCVCVCBVCBVBNBNBNHg.pdfswinSAXSXZZCXXZXCXCXCVCVCBVCBVBNBNBNHg.pdf
swinSAXSXZZCXXZXCXCXCVCVCBVCBVBNBNBNHg.pdf
KarishmaTamboli4

Recently uploaded (20)

brightonSEO - Metehan Yesilyurt - Generative AI & GEO: the new SEO race and h...
brightonSEO - Metehan Yesilyurt - Generative AI & GEO: the new SEO race and h...brightonSEO - Metehan Yesilyurt - Generative AI & GEO: the new SEO race and h...
brightonSEO - Metehan Yesilyurt - Generative AI & GEO: the new SEO race and h...
Metehan Yeilyurt
FOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdh
FOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdhFOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdh
FOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdh
cshdhdhvfsbzdb
A Simple Introduction to data Science- what is it and what does it do
A Simple Introduction to data Science- what is it and what does it doA Simple Introduction to data Science- what is it and what does it do
A Simple Introduction to data Science- what is it and what does it do
sarah mabrouk
BoSEU25 | Diego de J坦dar | Why User Activation is the Key to Sustainable Growth
BoSEU25 | Diego de J坦dar | Why User Activation is the Key to Sustainable GrowthBoSEU25 | Diego de J坦dar | Why User Activation is the Key to Sustainable Growth
BoSEU25 | Diego de J坦dar | Why User Activation is the Key to Sustainable Growth
Business of Software Conference
Data-Ethics-and-Privacy-What-Every-Analyst-Should-Know
Data-Ethics-and-Privacy-What-Every-Analyst-Should-KnowData-Ethics-and-Privacy-What-Every-Analyst-Should-Know
Data-Ethics-and-Privacy-What-Every-Analyst-Should-Know
Ozias Rondon
Turinton Insights - Enterprise Agentic AI Platform
Turinton Insights - Enterprise Agentic AI PlatformTurinton Insights - Enterprise Agentic AI Platform
Turinton Insights - Enterprise Agentic AI Platform
vikrant530668
Indian Smm Panel.docxIndian Smm Panel.docx
Indian Smm Panel.docxIndian Smm Panel.docxIndian Smm Panel.docxIndian Smm Panel.docx
Indian Smm Panel.docxIndian Smm Panel.docx
wasifkhan196986
Hadoop-and-R-Programming-Powering-Big-Data-Analytics.pptx
Hadoop-and-R-Programming-Powering-Big-Data-Analytics.pptxHadoop-and-R-Programming-Powering-Big-Data-Analytics.pptx
Hadoop-and-R-Programming-Powering-Big-Data-Analytics.pptx
MdTahammulNoor
This presentation detail concepts of cryptocurrency
This presentation detail concepts of cryptocurrencyThis presentation detail concepts of cryptocurrency
This presentation detail concepts of cryptocurrency
Aslbtr
Ch 1 Basic SQL Statement for the data.pptx
Ch 1 Basic SQL Statement for the data.pptxCh 1 Basic SQL Statement for the data.pptx
Ch 1 Basic SQL Statement for the data.pptx
ehzazulhaq
Risk Based Supervision Model: Introduction
Risk Based Supervision Model: IntroductionRisk Based Supervision Model: Introduction
Risk Based Supervision Model: Introduction
ShohanurRahman76
SQL-for-Data-Analytics-Top-10-Queries-Every-Analyst-Should-Know
SQL-for-Data-Analytics-Top-10-Queries-Every-Analyst-Should-KnowSQL-for-Data-Analytics-Top-10-Queries-Every-Analyst-Should-Know
SQL-for-Data-Analytics-Top-10-Queries-Every-Analyst-Should-Know
Ozias Rondon
The rise of AI Agents - Beyond Automation_ The Rise of AI Agents in Service ...
The rise of AI Agents -  Beyond Automation_ The Rise of AI Agents in Service ...The rise of AI Agents -  Beyond Automation_ The Rise of AI Agents in Service ...
The rise of AI Agents - Beyond Automation_ The Rise of AI Agents in Service ...
Yasen Lilov
PPTjhjhghhhghghghggvgfggffgftftftftftft.ppt
PPTjhjhghhhghghghggvgfggffgftftftftftft.pptPPTjhjhghhhghghghggvgfggffgftftftftftft.ppt
PPTjhjhghhhghghghggvgfggffgftftftftftft.ppt
vmanjusundertamil21
Advice vs Criticism which one is good and not.pptx
Advice vs Criticism which one is good and not.pptxAdvice vs Criticism which one is good and not.pptx
Advice vs Criticism which one is good and not.pptx
thecorneredtigers
20230109_NLDL_Tutorial_Tan.pdf data analysis
20230109_NLDL_Tutorial_Tan.pdf data analysis20230109_NLDL_Tutorial_Tan.pdf data analysis
20230109_NLDL_Tutorial_Tan.pdf data analysis
aitaghavi
Social Media Trends in Bangladesh - A Data-Driven Analysis for 2025.pdf
Social Media Trends in Bangladesh - A Data-Driven Analysis for 2025.pdfSocial Media Trends in Bangladesh - A Data-Driven Analysis for 2025.pdf
Social Media Trends in Bangladesh - A Data-Driven Analysis for 2025.pdf
Ngital
20-NoSQLMongoDbiig data analytics hB.pdf
20-NoSQLMongoDbiig data analytics hB.pdf20-NoSQLMongoDbiig data analytics hB.pdf
20-NoSQLMongoDbiig data analytics hB.pdf
ssuser2d043c
Statistics for Management - standard deviation.pptx
Statistics for Management - standard deviation.pptxStatistics for Management - standard deviation.pptx
Statistics for Management - standard deviation.pptx
Jeya Sree
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
berihun18
brightonSEO - Metehan Yesilyurt - Generative AI & GEO: the new SEO race and h...
brightonSEO - Metehan Yesilyurt - Generative AI & GEO: the new SEO race and h...brightonSEO - Metehan Yesilyurt - Generative AI & GEO: the new SEO race and h...
brightonSEO - Metehan Yesilyurt - Generative AI & GEO: the new SEO race and h...
Metehan Yeilyurt
FOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdh
FOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdhFOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdh
FOOD LAWS.pptxbshdhdhdhdhdhhdhdhdhdhdhhdh
cshdhdhvfsbzdb
A Simple Introduction to data Science- what is it and what does it do
A Simple Introduction to data Science- what is it and what does it doA Simple Introduction to data Science- what is it and what does it do
A Simple Introduction to data Science- what is it and what does it do
sarah mabrouk
BoSEU25 | Diego de J坦dar | Why User Activation is the Key to Sustainable Growth
BoSEU25 | Diego de J坦dar | Why User Activation is the Key to Sustainable GrowthBoSEU25 | Diego de J坦dar | Why User Activation is the Key to Sustainable Growth
BoSEU25 | Diego de J坦dar | Why User Activation is the Key to Sustainable Growth
Business of Software Conference
Data-Ethics-and-Privacy-What-Every-Analyst-Should-Know
Data-Ethics-and-Privacy-What-Every-Analyst-Should-KnowData-Ethics-and-Privacy-What-Every-Analyst-Should-Know
Data-Ethics-and-Privacy-What-Every-Analyst-Should-Know
Ozias Rondon
Turinton Insights - Enterprise Agentic AI Platform
Turinton Insights - Enterprise Agentic AI PlatformTurinton Insights - Enterprise Agentic AI Platform
Turinton Insights - Enterprise Agentic AI Platform
vikrant530668
Indian Smm Panel.docxIndian Smm Panel.docx
Indian Smm Panel.docxIndian Smm Panel.docxIndian Smm Panel.docxIndian Smm Panel.docx
Indian Smm Panel.docxIndian Smm Panel.docx
wasifkhan196986
Hadoop-and-R-Programming-Powering-Big-Data-Analytics.pptx
Hadoop-and-R-Programming-Powering-Big-Data-Analytics.pptxHadoop-and-R-Programming-Powering-Big-Data-Analytics.pptx
Hadoop-and-R-Programming-Powering-Big-Data-Analytics.pptx
MdTahammulNoor
This presentation detail concepts of cryptocurrency
This presentation detail concepts of cryptocurrencyThis presentation detail concepts of cryptocurrency
This presentation detail concepts of cryptocurrency
Aslbtr
Ch 1 Basic SQL Statement for the data.pptx
Ch 1 Basic SQL Statement for the data.pptxCh 1 Basic SQL Statement for the data.pptx
Ch 1 Basic SQL Statement for the data.pptx
ehzazulhaq
Risk Based Supervision Model: Introduction
Risk Based Supervision Model: IntroductionRisk Based Supervision Model: Introduction
Risk Based Supervision Model: Introduction
ShohanurRahman76
SQL-for-Data-Analytics-Top-10-Queries-Every-Analyst-Should-Know
SQL-for-Data-Analytics-Top-10-Queries-Every-Analyst-Should-KnowSQL-for-Data-Analytics-Top-10-Queries-Every-Analyst-Should-Know
SQL-for-Data-Analytics-Top-10-Queries-Every-Analyst-Should-Know
Ozias Rondon
The rise of AI Agents - Beyond Automation_ The Rise of AI Agents in Service ...
The rise of AI Agents -  Beyond Automation_ The Rise of AI Agents in Service ...The rise of AI Agents -  Beyond Automation_ The Rise of AI Agents in Service ...
The rise of AI Agents - Beyond Automation_ The Rise of AI Agents in Service ...
Yasen Lilov
PPTjhjhghhhghghghggvgfggffgftftftftftft.ppt
PPTjhjhghhhghghghggvgfggffgftftftftftft.pptPPTjhjhghhhghghghggvgfggffgftftftftftft.ppt
PPTjhjhghhhghghghggvgfggffgftftftftftft.ppt
vmanjusundertamil21
Advice vs Criticism which one is good and not.pptx
Advice vs Criticism which one is good and not.pptxAdvice vs Criticism which one is good and not.pptx
Advice vs Criticism which one is good and not.pptx
thecorneredtigers
20230109_NLDL_Tutorial_Tan.pdf data analysis
20230109_NLDL_Tutorial_Tan.pdf data analysis20230109_NLDL_Tutorial_Tan.pdf data analysis
20230109_NLDL_Tutorial_Tan.pdf data analysis
aitaghavi
Social Media Trends in Bangladesh - A Data-Driven Analysis for 2025.pdf
Social Media Trends in Bangladesh - A Data-Driven Analysis for 2025.pdfSocial Media Trends in Bangladesh - A Data-Driven Analysis for 2025.pdf
Social Media Trends in Bangladesh - A Data-Driven Analysis for 2025.pdf
Ngital
20-NoSQLMongoDbiig data analytics hB.pdf
20-NoSQLMongoDbiig data analytics hB.pdf20-NoSQLMongoDbiig data analytics hB.pdf
20-NoSQLMongoDbiig data analytics hB.pdf
ssuser2d043c
Statistics for Management - standard deviation.pptx
Statistics for Management - standard deviation.pptxStatistics for Management - standard deviation.pptx
Statistics for Management - standard deviation.pptx
Jeya Sree
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
berihun18

UNIT-I.pptx awt advance java abstract windowing toolkit and swing

  • 2. Introduction to AWT AWT Classes The AWT classes are contained in the java.awt package. Window Fundamentals AWT defines windows according to a class hierarchy that adds functionality & specificity in each level. The two most common windows are those derived from Panel those derived from Frame
  • 3. The following fig. shows the class hierarchy for Panel and Frame Component Container Window Panel Frame Applet
  • 4. Component All user interface elements that are displayed on the screen and that interact with the user are subclasses of Component. Responsible for managing events, such as mouse and keyboard input, positioning and sizing the window, and repainting. Container subclass of Component Responsible for laying out(i.e. positioning) any components that it contain. It does this through the use of various layout managers.
  • 5. Panel Concrete subclass of Container. No new methods; it simply implements Container. Panel is the superclass for Applet. Panel is a window that does not contain a title bar, menu bar or border. (AppletViewer provides this; browser wont) Components can be added to a Panel object by its add( ) method. Once added, they can be positioned and resized manually using the setLocation( ), setSize( ), or setBounds( ) methods defined by Component.
  • 6. Window creates top-level window. Window objects generally are not created directly; instead we will use a subclass of Window called Frame. Frame subclass of Window and has a title bar, menu bar, border and resizing corners. Frame object can be created from within an applet window it will contain a message Java Applet Window When a Frame window is created by a program ; a normal window is created. Canvas not part of hierarchy. Canvas encapsulates a blank window upon which you can draw.
  • 7. Commonly Used Methods of Component Class setBackground(): used to change the background color of a component. public void setBackground ( Color c) Note: color is a helper class. setForeground(): used to change the foreground color of a component. public void setForeground ( Font obj) Font is represented by java.awt.Font. public Font (String FontName, int Style, int Size) e.g. : Font f = (Times New Roman, Font.BOLD, 20); setBounds(): used to specify size and position of component in a container. public void setBounds (int left, int top, int width, int height)
  • 8. Commonly used methods of Container Class add(): used to add components to a container. public void add (Component c) setSize(): used to specify the size of a container. public void setSize(int width, int height) setLayout(): used to specify the layout manager for a container. public void setLayout (LayoutManager mgr) setVisible(boolean visibility): used to set the visibility of container. public void setVisible (boolean visibility)
  • 9. Working with Frame Windows Frames Constructors Frame( ) creates a standard window that does not contain a title. Frame(String title) creates a window with the title specified by title. Setting the Windows Dimensions void setSize(int newWidth, int newHeight) void setSize(Dimension newSize) set the dimensions of the window Dimension getSize( ) obtain the current size of a window
  • 10. Hiding and Showing a Window After a frame window has been created, it will not be visible until you call setVisible( ) void setVisible(boolean visibleFlag) -> visible if true otherwise hidden Setting a Windows Title void setTitle(String newTitle) Closing a Frame Window Remove window from the screen by calling setVisible(false), when it is closed. If Frame window is a child window, to intercept window-close event, implement windowClosing( ) method of the WindowListener interface. Inside windowClosing( ), remove the window from the screen.
  • 11. import java.awt.Frame; public class SampleFrame extends Frame { SampleFrame(String title) { super( ); this.setTitle(title); this.setSize(300,200); this.setVisible(true); } public static void main(String args[ ]) { SampleFrame sf = new SampleFrame(My Window);
  • 12. Layout managers 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 form void setLayout(LayoutManager layoutObj) layoutObj -> reference to the desired layout manager Java has several predefined LayoutManager classes.
  • 13. FlowLayout FlowLayout is the default layout manager. FlowLayout implements a simple layout style, which is similar to how words flow in a text editor. direction -> by default it is left to right, top to bottom By default, components are laid out line-by-line beginning at the upper-left corner. A small space is left between each component, above and below, as well as left and right. Constructors FlowLayout( ) -> creates the default layout, which centers components and leaves 5 pixels of space between each component.
  • 14. FlowLayout(int how) -> valid values for how are as follows: FlowLayout.LEFT FlowLayout.CENTER FlowLayout.RIGHT FlowLayout.LEADING FlowLayout.TRAILING FlowLayout(int how, int horz, int vert) -> horz and vert allows to specify the horizontal and vertical space between components
  • 15. BorderLayout 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( ) -> default border layout BorderLayout(int horz, int vert)
  • 16. When adding components to border layout, call the following add( ) method void add(Component compObj, Object region) compObj component to be added region specifies where the component will be added BorderLayout defines the following constants that specify the regions BorderLayout.CENTER BorderLayout.SOUTH BorderLayout.EAST BorderLayout.WEST BorderLayout.NORTH
  • 18. GridLayout GridLayout lays out components in a two-dimensional grid. Constructors GridLayout( ) creates a single-column grid layout GridLayout(int numRows, int numColumns) -> creates a grid layout with the specified number of rows and columns GridLayout(int numRows, int numColumns, int horz, int vert) Specifying numRows as zero allows for unlimited-length columns Specifying numColumns as zero allows for unlimited-length rows
  • 20. CardLayout It is used to arrange containers in the form of deck of cards. Methods: first() / last()/ next()/ previous(): is used to make the first/ last/ next/ previous card visible. show(): is used to make a specified card visible. public void show ( Container deck, String CardName) To give a name to the container while it is added to the deck: public void add ( Container card, String CardName)
  • 21. AWT Controls The AWT supports the following types of controls Labels Push Buttons Check boxes Choice lists Lists Scroll bars Text editing -> subclasses of Component
  • 22. Adding and Removing Controls To add a control to a window first create an instance of the desired control. then add it to a window by calling add( ) Component add(Component compObj) compObj -> instance of the control to add To remove a control void remove(Component obj) obj -> control to be removed removeAll( ) -> to remove all controls Except for labels, which are passive controls, all controls generate events when they are accessed by user. Example
  • 23. Label s A label is an object of type Label, and it contains a string which it displays. Passive controls do not support any interaction with the user. Label defines the following constructors Label( ) Label(String str) string is left-justified Label(String str, int how) - alignment specified by how - value of how must be one of the 3 constants - Label.LEFT, Label.RIGHT, Label.CENTER
  • 24. To set or change the text in a label, use the setText( ) method. void setText(String str) To obtain the current label, call getText( ) String getText( ) To set the alignment of the string within the label, call setAlignment( ) void setAlignment(int how) To obtain the current alignment, call getAlignment( ) int getAlignment( ) Example
  • 25. Button s A 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. Button defines these two constructors Button( ) Button(String str) contains str as a label To set the label of a button, call setLabel( ) void setLabel(String str) To retrieve the label of a button, call getLabel( ) String getLabel( )
  • 26. Check Boxes A check box is a control that is used to turn an option on or off. It consists of a small box that can either contain a check mark or not. There is a label associated with each check box that describes what option the box represents. Check boxes can be used individually or as part of a group. Check boxes are objects of the Checkbox class. Checkbox supports these constructors Checkbox( ) label is initially blank; state of the checkbox is unchecked. Checkbox(String str) label is specified by str; state of the checkbox is unchecked.
  • 27. Checkbox(String str, boolean on) - if on is true, the checkbox is initially checked; otherwise cleared. Checkbox(String str, boolean on, CheckboxGroup cbGroup) - group is specified by cbGroup; if this checkbox is not part of a group, then cbGroup must be null. Checkbox(String str, CheckboxGroup cbGroup, boolean on) To retrieve the current state of a checkbox, call getState( ) boolean getState( ) To set the state of a checkbox, call setState( ): void setState(Boolean on) To obtain the current label associated with a checkbox, call getLabel( ) String getLabel( )
  • 28. 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. To create a set of mutually excusive check boxes, first define the group to which they will belong and then specify that group when constructing the checkboxes. Check box groups are objects of type CheckboxGroup Only default constructor CheckboxGroup( ) -> creates an empty group.
  • 29. To determine which checkbox in a group is currently selected, call getSelectedCheckbox( ) Checkbox getSelectedCheckbox( ) To set a check box, call setSelectedCheckbox( ) void setSelectedCheckbox(Checkbox which) which -> checkbox that has to be selected; previously selected check box will be turned off Example
  • 30. Choice Controls The Choice class is used to create a pop-up list of items from which the user may choose. Thus, a 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. Choice only defines the default constructor, which creates an empty list. To add a selection to the list, call add( ). Items are added to the list in the order in which calls to add( ) occur. void add(String name)
  • 31. To determine which item is currently selected, call getSelectedItem( ) or getSelectedIndex( ) String getSelectedItem( ) -> returns a string containing the name of the item int getSelectedIndex( ) -> returns the index of the item. The first item is at index 0 To obtain the number of items in the list, call getItemCount( ) int getItemCount( ) To set the currently selected item with either a zero-based index or a string that will match a name in the list, call select( ) void select(int index) void select(String name) To obtain the name associated with the item at an index, call getItem( )
  • 32. Lists The List class provides a compact, multiple-choice, scrolling selection list. 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. List provides 3 constructors List( ) creates a List control that allows only one item to be selected at any one time. List(int numRows) numRows specifies the number of entries in the list that will always be visible. List(int numRows, boolean multipleSelect) if multipleSelect is true, then the user may select two or more items at a time. If it is false, only one item may be selected.
  • 33. To add a selection to the list, call add( ) void add(String name) name is the name of the item added to the list. void add(String name, int index) adds the item at the index specified by index. For lists that allow only single selection, to determine which item is currently selected, call getSelectedItem( ) or getSelectedIndex( ) For lists that allow multiple selection, call getSelectedItems( ) or getSelectedIndexes( ) String[ ] getSelectedItems( ) int[ ] getSelectedIndexes( )
  • 34. TextFields The TextField class implements a single-line text-entry area, usually called an edit control. TextField is a subclass of TextComponent. TextField defines the following constructors TextField( ) creates a default text field TextField(int numChars) creates a text field that is numChars wide. TextField(String str) initializes the text field with the string contained in str. TextField(String str, int numChars)
  • 35. To obtain the string currently contained in the text field, call getText( ) String getText( ) To set the text, call setText( ) void setText(String str) str is the new string To select a portion of the text in a text field, call select( ) void select(int startIndex, int endIndex) - selects the characters beginning at startIndex and ending at endIndex 1 To obtain the currently selected text, call getSelectedText( ) String getSelectedText( )
  • 36. To control whether the contents of a text field may be modified by the user or not, call setEditable( ) void setEditable(boolean canEdit) - if canEdit is true, the text may be changed. If it is false, the text cannot be altered. To determine the editability, call isEditable( ) boolean isEditable( ) - returns true if the text may be changed and false if not.
  • 37. To disable the echoing of characters as they are typed(eg.while typing passwords), call setEchoChar( ) method. void setEchoChar(char ch) - ch specifies the character to be echoed. To check a text field to see if it is in this mode, call echoCharIsSet( ) method. boolean echoCharIsSet( ) To retrieve the echo character, call getEchoChar( ) method. char getEchoChar( ) Example
  • 38. TextAre a Sometimes, a single line of text input is not enough for a given task. to handle these situations, the AWT includes a multiline editor called TextArea. Constructors TextArea( ) TextArea(int numLines, int numChars) -> numLines specifies the height, in lines, of the text area -> numChars specifies its width, in characters. TextArea(String str) -> initial text can be specified by str
  • 39. TextArea(String str, int numLines, int numChars) TextArea(String str, int numLines, int numChars, int sBars) -> sBars specify the scroll bars. sBars must be one of these values SCROLLBARS_BOTH SCROLLBARS_NONE SCROLLBARS_HORIZONTAL_ONLY SCROLLBARS_VERTICAL_ONLY TextArea is a subclass of TextComponent. It supports the getText( ), setText( ), getSelectedText( ), select, isEditable( ), and setEditable( )
  • 40. TextArea adds the following methods void append(String str) -> appends the string specified by str to the end of the current text. void insert(String str, int index) -> inserts the string passed in str at the specified index. void replaceRange(String str, int startIndex, int endIndex) -> replaces the characters from startIndex to endIndex -1, with the replacement text passed in str.
  • 41. Scroll Bar Scroll bars are used to select continuous values between a specified minimum and maximum. Scroll bars may be oriented horizontally or vertically. Scrollbar defines the following constructors: Scrollbar( ) throws HeadlessException Scrollbar(int style) throws HeadlessException Scrollbar(int style, int initialValue, int thumbSize, int min, int max) throws HeadlessException
  • 42. Continue If style is Scrollbar.VERTICAL, a vertical scroll bar is created. If style is Scrollbar.HORIZONTAL the scroll bar is horizontal. the height of the thumb is passed in thumbSize. The minimum and maximum values for the scroll bar are specified by min and max.
  • 43. Method void setValues(int initialValue, int thumbSize, int min, int max) int getValue( ) void setValue(int newValue) int getMinimum( ) int getMaximum( ) void setUnitIncrement(int newIncr) //scroll line up and down void setBlockIncrement(int newIncr) //scroll page-up and down
  • 44. Menu Bars and Menus Menu is implemented in AWT by the following classes: MenuBar, Menu, and MenuItem. A menu bar displays a list of top-level menu choices. Each choice is associated with a drop-down menu. 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. Checkable menu items are menu options of type CheckboxMenuItem and will have a check mark next to them when they are selected
  • 45. Creating Menu To create a menu bar, first create an instance of MenuBar. This class only defines the default constructor. public MenuBar() setMenuBar() method is used to add menubar on window. Next, create instances of Menu that will define the selections displayed on the bar. Following are the constructors for Menu: Menu( ) throws HeadlessException Menu(String optionName) throws HeadlessException
  • 46. Menu(String optionName, boolean removable) throws HeadlessException Individual menu items are of type MenuItem. It defines these constructors: MenuItem( ) throws HeadlessException MenuItem(String itemName) throws HeadlessException MenuItem(String itemName, MenuShortcut keyAccel) throws HeadlessException
  • 47. Methods We can disable or enable a menu item by using the setEnabled( ) method. void setEnabled (boolean enabledFlag) If the argument enabledFlag is true, the menu item is enabled. If false, the menu item is disabled. We can determine an items status by calling isEnabled( ). boolean isEnabled( ) isEnabled( ) returns true if the menu item on which it is called is enabled. Otherwise, it returns false. We can change the name of a menu item by calling setLabel( ). We can retrieve the current name by using getLabel( ). void setLabel(String newName) String getLabel( )
  • 49. Example item2 = new MenuItem("Open..."); item3 = new MenuItem("Close"); item4 = new MenuItem("-"); item5 = new MenuItem("Quit..."); // add menu items on menu file.add(item1); file.add(item2); file.add(item3); file.add(item4); file.add(item5); // add menu on menubar mbar.add(file); } Public static void main(String arg[]) { MenuDemo md=new MenuDemo(); md.setVisible(true); md.setTitle(Menu); md.setSize(400,400); } Import java.awt.*; Class MenuDemo extends Frame { MenuDemo() { MenuBar mbar = new MenuBar(); setMenuBar(mbar); // create the menu and menu items Menu file = new Menu("File"); MenuItem item1, item2, item3, item4, item5; item1 = new MenuItem ("New...");
  • 50. Dialog Boxes Dialog boxes are primarily used to obtain user input and are often child windows of a top-level window. Dialog boxes dont have menu bars, but in other respects, they 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 are of type Dialog Dialog(Frame parentWindow, boolean mode) Dialog(Frame parentWindow, String title, boolean mode) parentWindow is the owner of the dialog box.
  • 51. Continue.. 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.
  • 52. Example Create subclass of Dialog class Class DemoDialog extends Dialog{ DemoDialog(Frame par, String title) { Button b1= new Button (ok); add(b1); setSize(300,100); setVisible(true); } } Class Fdemo extends Frame{ Fdemo() { setSize(300,400); } Public static void main (String arg[]) { Fdemo fd=new Fdemo(); Fd.setVisible(true); DemoDialog dd=new DemoDialog(fd,First Dialog); }
  • 53. File Dialog 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. FileDialog constructors: FileDialog(Frame parent) FileDialog(Frame parent, String boxName) FileDialog(Frame parent, String boxName, int how)
  • 54. If how is FileDialog.LOAD, then the box is selecting a file for reading. If how is FileDialog.SAVE, the box is selecting a file for writing. If how is omitted, the box is selecting a file for reading. FileDialog provides methods that allow you to determine the name of the file and its path as selected by the user. Here are two examples: String getDirectory( ) String getFile( )