AWT Components • Java AWT components are platform- dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e. its components are using the resources of OS. • The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc. Department of Computer Engineering AJP Unit-I Mrs. Chavan P.P. 1
76
Embed
AWT Components - WordPress.comAWT Components •Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
AWT Components
• Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e. its components are using the resources of OS.
• The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 1
Java AWT Hierarchy • The hierarchy of Java AWT classes are given
below.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 2
Container
• The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 3
Window
• The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 4
Panel
• The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 5
Frame
• The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 6
Useful Methods of Component class
Method Description
public void add(Component c) inserts a component on this component.
public void setSize(int width,int height) sets the size (width and height) of the component.
public void setLayout(LayoutManager m) defines the layout manager for the component.
public void setVisible(boolean status) changes the visibility of the component, by default false.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 7
Working with Frame Windows
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 8
Constructors :
• Frame( )
• Frame(String title)
Setting the Window’s Dimensions: The setSize( ) method is used to set the dimensions of the window. void setSize(int newWidth, int newHeight)
The getSize( ) method is used to obtain the current size of a window.
Dimension getSize( ) Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 9
Hiding and Showing a Window
void setVisible(boolean visibleFlag)
Setting a Window’s Title
void setTitle(String newTitle)
Closing a Frame Window When using a frame window, your program must remove that window from the screen when it is closed, by calling setVisible(false).
To intercept a window-close event, you must implement the windowClosing( ) method of the WindowListener interface. Inside windowClosing( ), you must remove the window from the screen.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 10
Example:
import java.awt.*; public class TestFrame extends Frame { public TestFrame(String title){ super(title); } public static void main(String[] args){ Frame f = new TestFrame("TestFrame"); f.setSize(400,400); f.setLocation(100,100); f.show(); } } Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 11
AWT Component Classes
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 12
java.awt.Label
Constructors
• public Label(String strLabel, int alignment);
• public Label(String strLabel);
• public Label()
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 13
Alignment Constants (final static fields)
• public static final LEFT; // Label.LEFTpublic static final RIGHT; // Label.RIGHT
• public static final CENTER; // Label.CENTER
Public Methods
• public String getText();
• public void setText(String strLabel);
• public int getAlignment();
• public void setAlignment(int alignment);
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 14
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame
{
Label myLabel;
MyFrame()
{
setSize(400, 200);
setTitle("My Application");
setLayout(new FlowLayout());
setVisible(true);
myLabel = new Label("This is a label!");
add(myLabel);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
} );
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 15
java.awt.Button
Constructors
• public Button(String btnLabel);
• public Button();
Public Methods
• public String getLabel();
• public void setLabel(String btnLabel);
• public void setEnable(boolean enable)
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 16
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame
{ Button r,g,b;
MyFrame()
{
setSize(400, 200);
setTitle("My Application");
setLayout(new FlowLayout());
setVisible(true);
r = new Button("Red");
g = new Button("Green");
b = new Button("Blue");
add(r); add(g); add(b);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0); }
});
}
public static void main(String[] args)
{ MyFrame mf = new MyFrame();
}
} Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 17
Constructors
• public TextField(String initialText, int columns); • public TextField(String initialText); • public TextField(int columns); Public Methods
• public String getText(); • public void setText(String strText); • public void setEditable(boolean editable);
java.awt.TextField
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 18
Methods available in TextField class • String getText() – Retrieves the text in the text field.
• void setText(String str) – Assigns or sets text in the text field.
• String getSelectedText() – Retrieves the selected text in the text field.
• void select(int startindex, int endindex) – To select the text in text field from startindex to endindex – 1.
• boolean isEditable() – To check whether the text field is editable or not.
• void setEditable(boolean canEdit) – To make a text field editable or non-editable.
• void setEchoChar(char ch) – To set the echo character of a text field. This is generally used for password fields.
• boolean echoCharIsSet() – To check whether the echo character for the text field is set or not.
• char getEchoChar() – To retrieve the current echo character. Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 19
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame
{
Label myLabel;
TextField tf;
MyFrame()
{
setSize(400, 200);
setTitle("My Application");
setLayout(new FlowLayout());
myLabel = new Label("Enter name: ");
tf = new TextField(20);
add(myLabel);
add(tf);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0); }
} );
setVisible(true);
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 20
java.awt.TextArea Constructors • TextArea()
• TextArea(int numLines, int numChars)
• TextArea(String str)
• TextArea(String str, int numLines, int numChars)
• TextArea(String str, int numLines, int numChars, int sBars)
Valid values of sBars
• SCROLLBARS_BOTH
• SCROLLBARS_NONE
• SCROLLBARS_HORIZONTAL_ONLY
• SCROLLBARS_VERTICAL_ONLY Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 21
Methods available in TextArea class • String getText() – To retrieve the text in the text area.
• void setText(String str) – To assign or set the text in a text area.
• String getSelectedText() – To retrieve the selected text in a text area.
• void select(int startindex, int endindex) – To select the text in text field from startindex to endindex – 1.
• boolean isEditable() – To check whether the text field is editable or not.
• void setEditable(boolean canEdit) – To make a text field editable or non-editable.
• void append(String str) – To append the given string to the text in the text area.
• void insert(String str, int index) – To insert the given string at the specified index.
• void replaceRange(String str, int stastartIndex, int endIndex) – To replace the text from startIndex to endIndex – 1 with the given string. Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 22
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame
{
TextArea ta;
MyFrame()
{
setSize(400, 200);
setTitle("My Application");
setLayout(new FlowLayout());
ta = new TextArea(3, 20);
add(ta);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0); }
} );
setVisible(true);
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 23
java.awt.List
Constructors
• List()
• List(int numRows)
• List(int numRows, boolean multipleSelect)
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 24
Methods available in the List class
• void add(String name) – To add an item to the list box. • void add(String name, int index) – To add an item at the specified
index in the list box. • String getSelectedItem() – To get the item name which is selected
by the user. • int getSelectedIndex() – To get the item index which is selected by
the user. • String[] getSelectedItems() – To retrieve the selected item names
by the user. • int[] getSelectedIndexes() – To retrieve the selected item indexes
by the user. • int getItemCount() – To retrieve the number of items in the list
box. • void select(int index) – To select an item based on the given index. • String getItem(int index) – To retrieve the item at the given index. Department of Computer
Engineering AJP Unit-I Mrs. Chavan P.P. 25
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame
{ List myList;
MyFrame()
{
setSize(400, 200);
setTitle("My Application");
setLayout(new FlowLayout());
myList = new List();
myList.add("CSE");
myList.add("ECE");
myList.add("EEE");
myList.add("IT");
add(myList);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0); }
} );
setVisible(true);
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 26
java.awt.Choice (Dropdown Boxes) Constructor
• Choice()
Methods available in Choice class:
• void add(String name) – To add an item to the drop down list.
• String getSelectedItem() – To retrieve the item selected by the user.
• int getSelectedIndex() – To retrieve the index of the item selected by the user.
• int getItemCount() – To retrieve the number of items in the drop down list.
• void select(int index) – To select an item based on the given index.
• void select(String name) – To select an item based on the given item name.
• void getItem(int index) – To retrieve an item at the given index.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 27
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame
{ Choice myList;
MyFrame()
{ setSize(400, 200);
setTitle("My Application");
setLayout(new FlowLayout());
setVisible(true);
myList = new Choice();
myList.add("CSE");
myList.add("ECE");
myList.add("EEE");
myList.add("IT");
add(myList);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0); }
} );
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 28
java.awt.Checkbox
Constructors
• Checkbox()
• Checkbox(String str)
• Checkbox(String str, boolean on)
• Checkbox(String str, boolean on, CheckboxGroup cbGroup)
• boolean getState() – To retrieve the state of a checkbox.
• void setState(boolean on) – To set the state of a checkbox.
• String getLabel() – To retrieve the text of a checkbox.
• void setLabel(String str) – To set the text of a checkbox Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 29
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame
{ Checkbox c1, c2;
MyFrame()
{ setSize(400, 200);
setTitle("My Application");
setLayout(new FlowLayout());
setVisible(true);
c1 = new Checkbox("Male");
c2 = new Checkbox("Female");
add(c1);
add(c2);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0); }
} );
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 30
CheckBox Group
Radio buttons can be create by using Checkbox class and CheckboxGroup class
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 31
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame
{ Checkbox c1, c2;
CheckboxGroup cbg;
MyFrame()
{ setSize(400, 200);
setTitle("My Application");
setLayout(new FlowLayout());
setVisible(true);
cbg = new CheckboxGroup();
c1 = new Checkbox("Male", cbg, false);
c2 = new Checkbox("Female", cbg, false);
add(c1);
add(c2);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0); }
} );
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 32
Scroll Bar
Constructors • Scrollbar( ) • Scrollbar(int style) • Scrollbar(int style, int iValue, int tSize, int min, int
max) Methods • int getValue( ) • void setValue(int newValue) • int getMinimum( ) • int getMaximum( ) • void setUnitIncrement(int newIncr) • void setBlockIncrement(int newIncr)
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 33
import java.awt.*; import java.applet.*; /*<applet code="SBDemo" width=300 height=200></applet> */ public class SBDemo extends Applet { String msg = ""; Scrollbar vertSB, horzSB; public void init() { int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); vertSB = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, height); horzSB = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, width); add(vertSB); add(horzSB); } public void paint(Graphics g) { msg = "Vertical: " + vertSB.getValue(); msg += ", Horizontal: " + horzSB.getValue(); g.drawString(msg, 6, 160); } }
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 34
LayoutManagers The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes of layout managers.
There are following classes that represents the layout managers:
The GridBagLayout class is a flexible layout manager that aligns components vertically and horizontally, without requiring that the components be of the same size. Each GridBagLayout object maintains a dynamic rectangular grid of cells, with each component occupying one or more cells, called its display area.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 53
Constructor of GridBagLayout class
GridBagLayout()
Creates a grid bag layout manager.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 54
Some of the information that the GridBagLayout needs to know about an object are: – row and column – number of cells spanned – placement within its space – stretch and shrink values
This information is stored in an object of type GridBagContstraints and is associated with a component using
setContraints(Component, GridBagContraints)
This causes the layout manager to make a copy of the constraints and associate them with the object. Therefore you only need one of these GridBagContraints objects.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 55
VARIABLE DESCRIPTION
gridx, gridy
These variables are used to specify the location where the component is to be
placed. For example, the top-left cell location is specified as gridx = 0 and gridy =
0. If no values for these variables are specified, the layout manager assumes
them to be as GridBagConstraints.RELATIVE which means the component is to
be placed next to the existing one (it is relative placement: either to the right or
below the existing one).
gridwidth,
gridheight
These variables are used to give the size of the component in terms of cells. The
default value is 1, if no value is specified. Assigning
GridBagConstraints.REMAINDER to these variables means, the component
getting added is the last one in its row.
fill
This field is used when the component’s display area is larger than the
component’s size. It tells whether to resize the component or not when the
frame is resized. The value can be one of the following.
A top-level window can have a menu bar associated with it. This is implemented by the following classes: MenuBar, Menu, and MenuItem.
• 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.
• It is also possible to include checkable menu items. These are menu options of type CheckboxMenuItem and will have a check mark next to them when they are selected.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 63
Creating a menu on Frame / Applet
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 64
Constructors for Menu Menu( )
-Creates an empty menu.
Menu(String label)
-Constructs a new menu with the specified label.
Menu(String label, boolean removable)
-Constructs a new menu with the specified label. If removable is true, the pop-up menu can be removed and allowed to float free. Otherwise, it will remain attached to the menu bar.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 65
MenuItem( )
-Constructs a new MenuItem with an empty label and no keyboard shortcut.
MenuItem(String label)
-Constructs a new MenuItem with the specified label and no keyboard shortcut.
MenuItem(String label, MenuShortcut s)
-Create a menu item with an associated keyboard shortcut.
Constructors for MenuItem
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 66
void setEnabled(boolean enabledFlag)
-enables or disables a menu item. If the argument enabledFlag is true, the menu item is enabled. If false, the menu item is disabled.
boolean isEnabled( )
-determines an item’s status. Returns true if the menu item on which it is called is enabled. Otherwise, it returns false.
void setLabel(String newName)
- changes the name of a menu item.
String getLabel( ) -retrieve the current name of menu item.
Methods of MenuItem
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 67
Checkable MenuItem
• The CheckboxMenuItem class represents a check box which can be included in a menu. Selecting the check box in the menu changes control's state from on to off or from off to on.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 68
Constructors for Checkable MenuItem
CheckboxMenuItem()
-Create a check box menu item with an empty label.
CheckboxMenuItem(String label)
-Create a check box menu item with the specified label.
CheckboxMenuItem(String label, boolean state)
-Create a check box menu item with the specified label and state.
Dialog Boxes Dialog control represents a top-level window
with a title and a border used to take some form of input from the user.
Dialog boxes may be modal or modeless.
When a modal dialog box is active, all input is directed to it until it is closed. i.e. you cannot access other parts of 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.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 71
Constructors for Dialog Box Dialog(Frame parentWindow) Constructs an initially invisible, modeless Dialog with the specified parentWindow Frame and an empty title.
Dialog(Frame parentWindow, boolean modal) Constructs an initially invisible Dialog with the specified parentWindow Frame and modality and an empty title.
Dialog(Frame parentWindow, String title) Constructs an initially invisible, modeless Dialog with the specified parentWindow Frame and title.
Dialog(Frame parentWindow, String title, boolean modal) Constructs an initially invisible Dialog with the specified parentWindow Frame, title and modality.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 72
Methods for Dialog Box
String getTitle()
-Gets the title of the dialog.
void setTitle(String title)
-Sets the title of the Dialog.
boolean isModal()
-Indicates whether the dialog is modal.
void setModal(boolean modal)
-Specifies whether this dialog should be modal.
boolean isResizable()
-Indicates whether this dialog is resizable by the user.
void setResizable(boolean resizable)
-Sets whether this dialog is resizable by the user.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 73
FileDialog
FileDialog control represents a dialog window from which the user can select a file. To display a file dialog, you must instantiate an object of type FileDialog.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 74
Constructors for FileDialog
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. If mode is FileDialog.LOAD then selecting a file for reading. If mode is FileDialog.SAVE, then selecting a file for writing.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 75
Methods for FileDialog
String getDirectory()
Gets the directory of this file dialog.
String getFile()
Gets the selected file of this file dialog.
int getMode()
Indicates whether this file dialog box is for loading from a file or for saving to a file.