awt components - wordpress.comawt components •java awt components are platform-dependent i.e....
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)
• Checkbox(String str, CheckboxGroup cbGroup, boolean on)
Methods available in the Checkbox class:
• 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:
java.awt.BorderLayout
java.awt.FlowLayout
java.awt.GridLayout
java.awt.CardLayout
java.awt.GridBagLayout
javax.swing.BoxLayout
javax.swing.GroupLayout
javax.swing.ScrollPaneLayout
javax.swing.SpringLayout etc.
Defined in the AWT
Defined in Swing
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 35
Use Layout Managers
• Default layout managers
– Windows (Frames & Dialogs)
• BorderLayout
– Panels (Applets)
• FlowLayout
setLayout(new BorderLayout());
setLayout(new CardLayout(());
setLayout(new FlowLayout());
setLayout(new GridLayout(rows,columns,xgap,ygap));
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 36
BorderLayout The BorderLayout is used to arrange the components
in five regions: north, south, east, west and center. Each region (area) may contain one component only. It is the default layout of frame or window.
Five constants for each region:
public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 37
Constructors of BorderLayout class
BorderLayout()
creates a border layout but with no gaps between the components.
JBorderLayout(int hgap, int vgap)
creates a border layout with the given horizontal and vertical gaps between the components.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 38
Example of BorderLayout class import java.awt.*;
import javax.swing.*;
public class Border {
JFrame f;
Border(){
f=new JFrame();
JButton b1=new JButton("NORTH");
JButton b2=new JButton("SOUTH");
JButton b3=new JButton("EAST");
JButton b4=new JButton("WEST");
JButton b5=new JButton("CENTER");
f.add(b1,BorderLayout.NORTH); f.add(b2,BorderLayout.SOUTH); f.add(b3,BorderLayout.EAST); f.add(b4,BorderLayout.WEST); f.add(b5,BorderLayout.CENTER); f.setSize(300,300); f.setVisible(true); } public static void main(String[] args) { new Border(); } }
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 39
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 40
GridLayout
The GridLayout is used to arrange the components in rectangular grid. One component is displayed in each rectangle.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 41
Constructors of GridLayout class
GridLayout()
creates a grid layout with one column per component in a row.
GridLayout(int rows, int columns)
creates a grid layout with the given rows and columns but no gaps between the components.
GridLayout(int rows, int columns, int hgap, int vgap)
creates a grid layout with the given rows and columns alongwith given horizontal and vertical gaps.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 42
import java.awt.*;
import javax.swing.*;
public class MyGridLayout{
JFrame f;
MyGridLayout(){
f=new JFrame();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5); f.add(b6); f.add(b7); f.add(b8); f.add(b9); f.setLayout(new GridLayout(3,3)); f.setSize(300,300); f.setVisible(true); } public static void main(String[] args) { new MyGridLayout(); } }
Example of GridLayout class
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 43
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 44
FlowLayout The FlowLayout is used to arrange the components
in a line, one after another (in a flow). It is the default layout of applet or panel.
Fields of FlowLayout class:
public static final int LEFT
public static final int RIGHT
public static final int CENTER
public static final int LEADING
public static final int TRAILING
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 45
Constructors of FlowLayout class FlowLayout()
creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap.
FlowLayout(int align)
creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap.
FlowLayout(int align, int hgap, int vgap)
creates a flow layout with the given alignment and the given horizontal and vertical gap.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 46
Example of FlowLayout class import java.awt.*;
import javax.swing.*;
public class MyFlowLayout{
JFrame f;
MyFlowLayout(){
f=new JFrame();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
f.add(b1);
f .add(b2);
f.add(b3); f.add(b4); f.add(b5); f.setLayout(new FlowLayout(FlowLayout.RIGHT)); //setting flow layout of right alignment f.setSize(300,300); f.setVisible(true); } public static void main(String[] args) { new MyFlowLayout(); } }
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 47
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 48
CardLayout class The CardLayout class manages the components in such
a manner that only one component is visible at a time. It treats each component as a card that is why it is known as CardLayout.
Constructors of CardLayout class CardLayout()
creates a card layout with zero horizontal and vertical gap.
CardLayout(int hgap, int vgap)
creates a card layout with the given horizontal and vertical gap. Department of Computer
Engineering AJP Unit-I Mrs. Chavan P.P. 49
Commonly used methods CardLayout public void next(Container parent) It is used to flip to the next card of the given container.
public void previous(Container parent) It is used to flip to the previous card of the given container.
public void first(Container parent) It is used to flip to the first card of the given container.
public void last(Container parent)
It is used to flip to the last card of the given container.
public void show(Container parent, String name) It is used to flip to the specified card with the given name.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 50
Example of CardLayout class import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample extends
JFrame implements ActionListener{
CardLayout card;
JButton b1,b2,b3;
Container c;
CardLayoutExample(){
c=getContentPane();
card=new CardLayout(40,30);
c.setLayout(card);
b1=new JButton("Apple");
b2=new JButton("Boy");
b3=new JButton("Cat");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add("a",b1); c.add("b",b2); c.add("c",b3); } public void actionPerformed(ActionEvent e) { card.next(c); } public static void main(String[] args) { CardLayoutExample cl=new CardLayoutExample(); cl.setSize(400,400); cl.setVisible(true); cl.setDefaultCloseOperation(EXIT_ON_CLOSE); } }
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 51
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 52
GridBagLayout class
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.
GridBagConstraints.HORIZONTAL: Component grows horizontally
GridBagConstraints.VERTICAL: Component grows vertically
GridBagConstraints.BOTH: Component grows both ways
GridBagConstraints.NONE: Component does not grow at all
GridBagConstraints
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 56
VARIABLE DESCRIPTION
anchor
This variable specifies the placement (or location) of the component within the
cell when the component does not fill the whole cell. The value can be:
GridBagConstraints.NORTH
GridBagConstraints.NORTHEAST
GridBagConstraints.EAST
GridBagConstraints.SOUTHEAST
GridBagConstraints.SOUTH
GridBagConstraints.SOUTHWEST
GridBagConstraints.WEST
GridBagConstraints.NORTHWEST
GridBagConstraints.CENTER (the default one)
ipadx, ipady ipadx pixels are added to the left and right of the minimum size of the
component and ipady pixels are added to the top and bottom.
insets specifies the empty space between the container border and the components.
weightx,
weighty
distribute the space when the container expands. The GridBagLayout distributes
the space between the columns horizontally or vertically as per the weights of
weightx and weighty. The columns or cells with more weight get more space.
The cells with 0 weight value will not get any extra space. The range of values
must be 0.0 to 1.0. The default value of these fields are 0 and when specified it
must be a non-negative value.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 57
GridBagConstraints The following is a complete list of all of the constraints:
anchor determines position in the display area
fill determines if a component is stretched to fill the area
gridheight and gridwidth determine the number of rows and columns in the component's area
gridx and gridy determine the position of the component's area.
insets determine a border around a component's area.
ipadx and ipady allows the minimum or preferred size of a component to be adjusted.
weightx and weighty determine the sizes of the rows and columns of the grids.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 58
Example of GridBagLayout class import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class GB01 extends Applet
implements ActionListener {
Button B1 = new Button("Button 1");
Button B2 = new Button("Button 2");
GridBagLayout gridbag;
public void init() {
setBackground(Color.yellow);
gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridbag);
c.weightx = 1;
c.weighty = 1;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.SOUTHWEST; gridbag.setConstraints(B1,c); add(B1); c.weightx = 0; c.gridx = 1; c.anchor = GridBagConstraints.NORTH; c.fill = GridBagConstraints.BOTH; gridbag.setConstraints(B2,c); add(B2); B1.addActionListener(this); B2.addActionListener(this); } public void actionPerformed(ActionEvent e) { repaint(); } } /*<applet code=GB01.class width=200 height=200> </applet>*/
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 59
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 60
Layout Manager Heuristics
Left to right, Top to bottom
c
n
s
e w
FlowLayout GridLayout
BorderLayout
none, programmer sets x,y,w,h
null
One at a time
CardLayout GridBagLayout
JButton
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 61
Menu Bars and Menus
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 62
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.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 69
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class exp31 extends Frame
{ public static void main(String args[])
{ exp31 e=new exp31();
e.setVisible(true);
e.setSize(300,200);
MenuBar mbr=new MenuBar();
e.setMenuBar(mbr);
Menu mnuHome=new Menu("Home");
Menu mnuInsert=new Menu("Insert");
mbr.add(mnuHome);
mbr.add(mnuInsert);
CheckboxMenuItem Picture1=new CheckboxMenuItem("Picture");
MenuItem Paste1=new MenuItem("Paste");
mnuHome.add(Paste1);
mnuInsert.add(Picture1);
e.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0); }
});
}
}
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 70
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.
void setMode(int mode)
Sets the mode of the file dialog.
Department of Computer Engineering
AJP Unit-I Mrs. Chavan P.P. 76