0% found this document useful (0 votes)
31 views41 pages

AWT & Swing: Anas Dange

This document provides an overview of the AWT and Swing GUI toolkits in Java. It describes the basic components available in each toolkit such as containers, buttons, text fields, menus and layout managers. It also compares the heavyweight and lightweight components of AWT and Swing. Key classes and methods are defined for each GUI component.

Uploaded by

Pooja Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views41 pages

AWT & Swing: Anas Dange

This document provides an overview of the AWT and Swing GUI toolkits in Java. It describes the basic components available in each toolkit such as containers, buttons, text fields, menus and layout managers. It also compares the heavyweight and lightweight components of AWT and Swing. Key classes and methods are defined for each GUI component.

Uploaded by

Pooja Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

AWT & Swing

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)

 public void setSize(int width, int height)

 public void setLayout(LayoutManager m)

 public void setVisible(boolean status)


Frame
 Cons:
 Frame( )
 Frame(String title)
 Two ways to program:
1) By extending Frame class
Example - Ext_Frame.java
2) By creating the instance of Frame class
Example - Inst_Frame.java
Panel
 Displayed within the window and do not have
borders
 A panel cannot be seen directly. Hence, we
need to add it to a frame
 Cons:
 Panel( )
 Method:
 public void addNotify()
 Example:
 Panel_Demo.java
Dialog
 Cons:
 Dialog(Frame parentWindow, boolean mode)
 Dialog(Frame parentWindow, String title, boolean
mode)
 Example:
 Dialog_Demo.java
FileDialog
 Cons:
 FileDialog(Frame parent, String boxName)
 FileDialog(Frame parent, String boxName, int how)
 FileDialog(Frame parent)
 Method:
 String getDirectory( )
 String getFile( )
 Example:
 FD_Demo.java
Label
 Is used to display the String
 Passive controls that do not support any
interaction with the user
 Constructors
 Label( )
 Label(String labeltext)
 Label(String labeltext, int alignment)
 Methods
 setFont(Font f)
 setText(String s)
 getText( )
 void setAlignment(int how)
 int getAlignment( )
 Example:
 Label_Demo.java
Button
 Constructors
 Button( )
 Button(String str)
 Methods
 void setLabel(String str)
 String getLabel( )
 Example:
 ButtonList.java
Checkbox
 used for multi-option user input
 can be used individually or part of a group
 Cons:
 Checkbox( )
 Checkbox(String str)
 Checkbox(String str, boolean on)
 Checkbox(String str, boolean on, CheckboxGroup
cbGroup)
 Methods:
 boolean getState( )
 void setState(boolean on)
 String getLabel( )
 void setLabel(String str)
CheckboxGroup
 often called radio buttons, because they act
like the station selector—only one station can
be selected.
 Example:
 CBGroup.java
Choice
 To create a pop-up list of items from which
the user may choose.
 Is a form of menu.
 Cons:
 Choice( )
 Methods:
 void add(String name)
 String getSelectedItem( )
 int getSelectedIndex( )
 int getItemCount( )
 void select(int index)
 void select(String name)
 Example:
 ChoiceDemo.java
List
 provides a compact, multiple-choice, scrolling
selection list
 Cons:
 List( ) List(int numRows)
 List(int numRows, boolean multipleSelect)
 Methods:
 void add(String name)
 void add(String name, int index)
 String getSelectedItem( )
 int getSelectedIndex( )
 String[ ] getSelectedItems( )
 int[ ] getSelectedIndexes( )
 int getItemCount( )
 void select(int index)
 String getItem(int index)
Scrollbar
 Cons:
 Scrollbar( )
 Scrollbar(int style)
 Scrollbar(int style, int initialValue, int thumbSize,
int min, int max)
 Methods:
 void setValues(int initialValue, int thumbSize, int
min, int max)
 int getValue( )
 void setValue(int newValue)
 void setUnitIncrement(int newIncr)
 void setBlockIncrement(int newIncr)
 By default, page-up and page-down
increments are 10
TextField
 single-line text-entry area, called edit control
 Component – TextComponent - TextField
 Cons:
 TextField( )
 TextField(int numChars)
 TextField(String str)
 TextField(String str, int numChars)
 Methods:
 String getText( )
 void setText(String str)
 String getSelectedText( )
 void select(int startIndex, int endIndex)
 boolean isEditable( )
 void setEditable(boolean canEdit)
 Example:
 TextFieldDemo.java
TextArea
 multiline editor
 Component – TextComponent - TextArea
 Cons:
 TextArea( ) TextArea(String str)
 TextArea(int numLines, int numChars)
 TextArea(String str, int numLines, int numChars)
 TextArea(String str, int nLines,int nChars, int sBars)
 SCROLLBARS_BOTH SCROLLBARS_NONE
 SCROLLBARS_HORIZONTAL_ONLY
 SCROLLBARS_VERTICAL_ONLY
 Methods: same as textfield
 void append(String str)
 void insert(String str, int index)
 void replaceRange(String str, int startIndex, int
endIndex)
 Example: TextAreaDemo.java
Layout Manager
 arranges your controls within a window by
using some type of algorithm.

 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”

You might also like