Awt Components Unit 1
Awt Components Unit 1
The Panel is the container that doesn't contain title bar and
menu bars. It can have other components like button,
textfield etc.
The Frame is the container that contain title bar and can have
menu bars. It can have other components like button,
textfield etc.
public void setSize(int width,int height) sets the size (width and height) of
the component.
Constructors
public Label(String strLabel, int alignment);
public Label(String strLabel);
public Label()
Public Methods
public String getText();
public void setText(String strLabel);
public int getAlignment();
public void setAlignment(int alignment);
Public Methods
public String getLabel();
public void setLabel(String btnLabel);
public void setEnable(boolean enable)
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);
AJP Unit-I Mrs. Chavan P.P. Department of Computer
20 Engineering
Methods available in TextField class
String getText()
Retrieves the text in the text field.
String getSelectedText()
Retrieves the selected text in the text field.
boolean isEditable()
To check whether the text field is editable or not.
AJP Unit-I Mrs. Chavan P.P. Department of Computer
21 Engineering
Methods available in TextField class
void setEditable(boolean canEdit)
To make a text field editable or non-editable.
boolean echoCharIsSet()
To check whether the echo character for the text field is set
or not.
char getEchoChar()
To retrieve the current echo character.
AJP Unit-I Mrs. Chavan P.P. Department of Computer
22 Engineering
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();
}
} AJP Unit-I Mrs. Chavan P.P. Department of Computer
23 Engineering
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)
String getSelectedText()
To retrieve the selected text in a text area.
boolean isEditable()
To check whether the text field is editable or not.
AJP Unit-I Mrs. Chavan P.P. Department of Computer
25 Engineering
Methods available in TextArea class
void setEditable(boolean canEdit)
To make a text field editable or non-editable.
Constructors
List()
List(int numRows)
List(int numRows, boolean multipleSelect)
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.
int[] getSelectedIndexes()
To retrieve the selected item indexes by the user.
int getItemCount()
To retrieve the number of items in the list box.
Methods
int getValue( )
void setValue(int newValue)
int getMinimum( )
int getMaximum( )
void setUnitIncrement(int newIncr)
void setBlockIncrement(int newIncr)
AJP Unit-I Mrs. Chavan P.P. Department of Computer
39 Engineering
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);
}
}
AJP Unit-I Mrs. Chavan P.P. Department of Computer
40 Engineering
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 Defined in the AWT
java.awt.CardLayout
java.awt.GridBagLayout
javax.swing.BoxLayout
javax.swing.GroupLayout Defined in Swing
javax.swing.ScrollPaneLayout
javax.swing.SpringLayout etc.
AJP Unit-I Mrs. Chavan P.P. Department of Computer
41 Engineering
Use Layout Managers
• setLayout(new BorderLayout());
• setLayout(new CardLayout(());
• setLayout(new FlowLayout());
• setLayout(new GridLayout(rows,columns,xgap,ygap));
w e
c
FlowLayout(int align)
creates a flow layout with the given alignment and a default 5 unit
horizontal and vertical gap.
f.add(b1);
f .add(b2);
CardLayout()
creates a card layout with zero horizontal and vertical gap.
GridBagLayout()
Creates a grid bag layout manager.
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.
AJP Unit-I Mrs. Chavan P.P. Department of Computer
61 Engineering
GridBagConstraints
VARIABLE DESCRIPTION
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
gridx, gridy 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).
These variables are used to give the size of the component in terms of cells. The default
gridwidth,
value is 1, if no value is specified. Assigning GridBagConstraints.REMAINDER to these
gridheight
variables means, the component getting added is the last one in its row.
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.
fill GridBagConstraints.HORIZONTAL: Component grows horizontally
GridBagConstraints.VERTICAL: Component grows vertically
GridBagConstraints.BOTH: Component grows both ways
GridBagConstraints.NONE: Component does not grow at all
AJP Unit-I Mrs. Chavan P.P. Department of Computer
62 Engineering
VARIABLE DESCRIPTION
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
anchor GridBagConstraints.EAST GridBagConstraints.SOUTHEAST
GridBagConstraints.SOUTH GridBagConstraints.SOUTHWEST
GridBagConstraints.WEST GridBagConstraints.NORTHWEST
GridBagConstraints.CENTER (the default one)
ipadx pixels are added to the left and right of the minimum size of the component and
ipadx, ipady
ipady pixels are added to the top and bottom.
insets specifies the empty space between the container border and the components.
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.
weightx,
The columns or cells with more weight get more space. The cells with 0 weight value will
weighty
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.
none,
Left to right,
programmer
Top to bottom
sets x,y,w,h
e JButton
c One at a time
w s
Menu(String label)
-Constructs a new menu with the specified label.
MenuItem(String label)
-Constructs a new MenuItem with the specified label and no
keyboard shortcut.
boolean isEnabled( )
-determines an item’s status. Returns true if the menu item on which
it is called is enabled. Otherwise, it returns false.
String getLabel( )
-retrieve the current name of menu item. Department of Computer
72 AJP Unit-I Mrs. Chavan P.P.
Engineering
Checkable MenuItem
CheckboxMenuItem(String label)
-Create a check box menu item with the specified label.
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.