AWT & Swing: Anas Dange
AWT & Swing: Anas Dange
Anas Dange
M.E(CMPN), B.E(I.T)
Lecturer,
Dept. of Computer Engineering,
ARKP, Panvel
Intro
API to develop GUI or window-based
application.
Components are displayed according to the
view of OS.
Heavyweight
java.awt package
Hierarchy
Components
Container is like screen wherein the
components are placed.
Container contains and controls the layout of
components.
Window: no border and no title
Frame: has title, border and menu bars. most
widely used container.
Dialog: has border and title. An instance of
the Dialog class cannot exist without an
associated instance of the Frame class.
Panel: does not contain title bar, menu bar or
border. Region internal to a Frame. Used for
grouping components together. Not bounded
by a visible border.
Methods of Component class
public void add(Component c)
Methods:
void setLayout(LayoutManager layoutObj)
To disable the layout manager, pass null for layoutObj.
By default - FlowLayout
FlowLayout
Components are laid out from the upper-left
corner, left to right and top to bottom.
Cons:
FlowLayout( )
FlowLayout(int how)
FlowLayout(int how, int horz, int vert)
Note: By default, which centers components
and leaves five pixels of space between each
component.
Example:
FlowLayoutDemo.java
BorderLayout
four narrow, fixed-width components at the
edges & one large area in center, which are
referred to as north, south, east, and west
Cons:
BorderLayout( )
BorderLayout(int horz, int vert)
Note: When adding components, use following
constants:
BorderLayout.CENTER BorderLayout.SOUTH
BorderLayout.EAST BorderLayout.WEST
BorderLayout.NORTH
Example:
BorderLayoutDemo.java
GridLayout
two-dimensional grid
Cons:
GridLayout( )
GridLayout(int numRows, int numColumns )
GridLayout(int numRows, int numColumns, int horz,
int vert)
Example:
GridLayoutDemo.java
GridBagLayout
two-dimensional grid not necessary to be of
same size
Cons:
GridBagLayout( )
CardLayout
only one component is visible at a time. It
treats each component as a card
Cons:
CardLayout( )
CardLayout(int horz, int vert)
Methods:
public void next(Container parent)
public void previous(Container parent)
public void first(Container parent)
public void last(Container parent)
Menus in Java
A menu bar displays a list of top-level menu
choices and each choice is associated with a
drop-down menu.
Implemented by following classes:
MenuBar, Menu, and MenuItem.
1. MenuBar:
Cons:
MenuBar()
Methods:
Menu add(Menu m)
Menu getMenu(int i)
void remove(int index)
Menus in Java
2. Menu:
Cons:
Menu( )
Menu(String optionName)
Menu(String optionName, boolean removable)
Methods:
MenuItem add(MenuItem mi)
void add(String label)
MenuItem getItem(int index)
int getItemCount()
void insert(MenuItem menuitem, int index)
void remove(int index)
void removeAll()
Menus in Java
3. MenuItem:
Cons:
MenuItem( )
MenuItem(String itemName)
Methods:
void setEnabled(boolean enabledFlag)
boolean isEnabled( )
void setLabel(String newName)
String getLabel( )
Menus in Java
4. CheckboxMenuItem:
Cons:
CheckboxMenuItem( )
CheckboxMenuItem(String itemName)
CheckboxMenuItem(String itemName, boolean on)
Methods:
boolean getState( )
void setState(boolean state)
Example:
MenuDemo.java
Swing
Swing is a set of classes that provides more
powerful and flexible components than AWT
and several exciting additions, including
tabbed panes, trees, and tables.
The look and feel of a component can be
separated form the logic of the component.
Thus, it is possible to "plug in" a new look
and feel for any given component without
affecting the rest of the code.
ImageIcon
paints an icon from an image.
Constructors:
ImageIcon(String filename)
ImageIcon(URL url)
ImageIcon class implements the Icon
interface that declares the methods:
int getIconHeight( )
int getIconWidth( )
JLabel
Constructors
JLabel(Icon i)
Label(String s)
JLabel(String s, Icon i, int align)
Methods
Icon getIcon( )
String getText( )
void setIcon(Icon i)
void setText(String s)
JButton
subclass of AbstractButton class, which
extends JComponent
Constructors
JButton(Icon i)
JButton(String s)
JButton(String s, Icon i)
Methods:
void setPressedIcon(Icon pi)
void setRolloverIcon(Icon ri)
Example:
JButtonDemo.java
JCheckbox
superclass is JToggleButton
Cons:
JCheckBox(Icon i)
JCheckBox(Icon i, boolean state)
JCheckBox(String s)
JCheckBox(String s, boolean state)
JCheckBox(String s, Icon i)
JCheckBox(String s, Icon i, boolean state)
Methods:
void setSelected(boolean state)
String getText()
JRadioButton
superclass is JToggleButton
ButtonGroup class is instantiated to create a
button group
Cons:
JRadioButton(Icon i)
JRadioButton(Icon i, boolean state)
JRadioButton(String s)
JRadioButton(String s, boolean state)
JRadioButton(String s, Icon i)
JRadioButton(String s, Icon i, boolean state)
Methods:
void setSelected(boolean state)
String getText()
Example:
JRadioButton.java
JComboBox
extends JComponent.
Cons:
JComboBox( )
JComboBox(Vector v)
Methods:
void addItem(Object obj)
Tabbed Panes
A tabbed pane is a component that appears
as a group of folders in a file cabinet
Extends JComponent.
Cons:
JTabbedPane()
Methods:
void addTab(String str, Component comp)
Example:
JTabbedPaneDemo.java
JScrollPane
Cons:
JScrollPane(Component comp)
JScrollPane(int vsb, int hsb)
JScrollPane(Component comp, int vsb, int hsb)
ScrollPaneConstants.
HORIZONTAL_SCROLLBAR_ALWAYS
HORIZONTAL_SCROLLBAR_AS_NEEDED
VERTICAL_SCROLLBAR_ALWAYS
VERTICAL_SCROLLBAR_AS_NEEDED
JTable
A table is a component that displays rows and
columns of data.
JTable class extends Jcomponent
Cons:
JTable(Object data[ ][ ], Object colHeads[ ])
Example:
JTableDemo.java
JTree
presents a hierarchical view of data
Extends JComponent.
Cons:
JTree(Hashtable ht)
JTree(Object obj[ ])
JTree(TreeNode tn)
JTree(Vector v)
TreeNode interface declares methods that obtain
information about a tree node.
MutableTreeNode interface extends TreeNode. It
declares methods that can insert and remove child
nodes or change the parent node.
DefaultMutableTreeNode class implements the
MutableTreeNode interface. It represents a node in a
tree.
Example:
JTreeDemo.java
Progress Bar
to display the progress of the task.
Extends JComponent.
Cons:
JProgressBar()
JProgressBar(int min, int max)
JProgressBar(int orient)
JProgressBar(int orient, int min, int max)
Methods:
double getPercentComplete()
void setStringPainted(boolean b)
Example:
ProgressBarExample.java
“Nothing pushes students to do their
best work like a professor who takes
pride not in his or her own
accomplishments, but in helping others
realize their potential”