Swing Componenets New
Swing Componenets New
Swing has been included as part of the Java Standard Edition 1.2.
1
Class Hierarchy:
2
Class Hierarchy:
3
Component:
It is an object having a graphical representation that can be
displayed on the screen.
Examples:
Buttons, Checkboxes, Scrollbars, etc.
4
Methods of Component Class:
Methods Meaning
Graphics getGraphics (): It returns the graphics context for this
component.
And more…
5
Container:
Container is the sub class of the Component.
Example:
Panel.
6
Methods of Container:
Methods Meaning
add (Component): It adds the specified component to
the end of this container.
7
Panel:
The Panel class is concrete sub class of Container.
8
AWT Component classes Swing Component Classes
Button JButton
Frame JFrame
Panel JPanel
Label JLable
TextField JTextField
CheckBox JCheckBox
Choice JComboBox
List JList
JTree
JTable
9
JLabel: -
JLabel (String s)
JLabel (Icon ic)
JLabel (String s, Icon ic, int align)
Where,
String s : It is the caption string of label.
Icon ic : It is the image displayed on label.
int align : It is alignment of an image on label.
LEFT, RIGHT, CENTER, LEADING,
TRAILING
Methods: -
Icon getIcon () : It returns the image of a label.
10
JButton: -
It provides the functionality of push button.
It allows an icon, a string, or both to be associated on button.
JButton (String s)
JButton (Icon ic)
JButton (String s, Icon ic)
e.g.
ImageIcon icn = new ImageIcon (“file.gif”);
JButton b1 = new JButton (icn);
JCheckBox: -
JCheckBox (String s)
JCheckBox (Icon ic)
JCheckBox (Icon ic, boolean state)
JCheckBox (String s, boolean state)
JCheckBox (String s, Icon ic)
JCheckBox (String s, Icon ic, boolean state)
11
JRadioButton: -
Radio buttons must be configured into a group.
Only one of the button from group can be selected at any time.
JRadioButton (String s)
JRadioButton (Icon ic)
JRadioButton (String s, boolean state)
JRadioButton (Icon ic, boolean state)
JRadioButton (String s, Icon ic)
JRadioButton (String s, Icon ic, boolean state)
JComboBox: -
It displays a drop-down list that allows list that allows a user to
select a different entry.
JComboBox ()
JComboBox (Vector V)
Where,
V is vector that initializes the combo box.
It may be of any type.
12
JTextArea: -
Text area is multiline text area which is used to display plain text.
JTextArea();
JTextArea(int row, int col);
JTextArea(String text);
JTextArea(String text, int row, int col);
Methods of JTextArea:
void append(String str)
void insert(String str, int pos)
void setFont(font)
String getText(); 13
Menus: -
Menus consist of Menu Bar on the top of the window having names of
pull down menus.
Steps: -
Create menu bar by using JMenuBar class of swing.
JMenuBar mb = new JMenuBar ();
Create object of Menu class and add it in to the menu bar as follows.
JMenu file = new JMenu (“File”);
Create Menu item by using JMenuItem class and add these menu items
in specific menu.
15
Icons in Menu Item: -
We can also add image as icon in as menu item along with text.
We can specify it in constructor as
By default the menu item text is placed to the right of the icon.
We can also add check box and radio button as menu item by
using following constructor.
16
Keyboard Mnemonics and Accelerators: -
We can specify the Keyboard Mnemonics for menu, items follows.
JMenuItem cut = new JMenuItem (“Cut”, ‘T’)
So when the menu displayed, the user just need to press the T key or C key
to select Cut or Copy menu item form it.
Mnemonics are used to select the menu item only form the currently opened
menus.
17
Accelerators: -
These are the keyboard shortcuts that let us select menu item
without ever opening a menu.
e.g.
If we want to set accelerator for the menu item save from the file menu
i.e. CTRL+S
18
Pop – Up Menus: -
A pop up menu is a menu that is not attached to a menu bar
that floats somewhere.
We can also set the position of the pop up menu on the frame by using following
method.
pop.show (panel, X, Y);
Where,
panel is parent component on which the pop up menu get displays
X is x-coordinate on panel
Y is y-coordinate on panel
19
Graphics Class:-
The Graphics class is the abstract base class for all
graphics contexts.
21
The methods of Graphics class are as follows:
void drawLine (int x1, int y1, int x2, int y2)
Where,
x1 & y1 the coordinate of first point.
x2 & y2 the coordinate of second point.
class GraphicsDemo
{
public static void main(String arg [] )
{
GraphicsFrame gf = new GraphicsFrame();
gf.setVisible(true);
}
}
25
Working with Color:
The AWT color system allows us to specify any color that we want.
Thus our program does not need to be concerned with the difference on
the way color is supported by various hardware devices.
26
We can also create your own colors by using constructor of Color
class as follows.
27
e. g.
1. Color clr = new Color (255,100,100);
The fonts are encapsulated by the Font class with several methods.
Variable Meaning
String name Name of the font
float size Size of the font in points
int style Font styles
29
The methods defined in Font class:
Method Meaning
Font getFont (String property) : It returns the font associated with the
system property specified by property. It
returns null if property does not exist.
30
Method Meaning
boolean isItalic () :It returns true if the font includes
ITALIC style value. Otherwise false.
32
Format:
void setLayout (LayoutManager obj)
Where,
obj is reference to desired layout manger.
Then,
We determine the shape and position of component manually
using the setBounds ().
33
Layout Managers:
FlowLayout, BorderLayout, GridLayout, CardLayout.
FlowLayout Manager:
It is default Layout manager.
34
Constructor:
FlowLayout fl = new FlowLayout ()
FlowLayout fl = new FlowLayout (int align)
FlowLayout fl = new FlowLayout (int align, int hspace, int vspace)
35
BorderLayout:
It has four narrow, fixed-width components at the edges
and one large area in the center.
The four sides are referred as north, south, east, and west.
36
BorderLayout defines the constants that specify the regions.
BorderLayout.CENTER
BorderLayout.SOUTH
BorderLayout.NORTH
BorderLayout.EAST
BorderLayout.WEST
37
GridLayout:
It lays component in a two-dimensional grid.
38
CardLayout:
CardLayout is unique layout managers which stores several
different layouts.
This Layout manages two or more cards i.e. containers (usually Panel)
that share the same display space.
Constructors:
CardLayout cl = new CardLayout ()
CardLayout cl =new CardLayout (int hspace, int vspace)
39
CardLayout:
Steps:
1. First create number of different cards i.e. Panels.
2. Add all these cards Panels in one deck i.e. Panel.
3. This main Panel must have CardLayout as Layout Manager.
4. Finally placed this mail Panel in Frame or Window.
5. We must provide some way to select between cards.
i.e. push button or Comb Box.
When card panels are added to a panel, they are given a name.
The format of add() methods is as follows.
Where,
panelObj is the object of panel of cart.
name is the string that specifies the name of the card.
40
Methods to Activate Cards:
Methods Meaning
void first (Container deck) : It shows the first card in the deck.
void last (Container deck) : It shows the last card in the deck.
void next (Container deck) : It shows the next card in the deck.
void previous (Container deck): It shows the previous card in the deck.
41
42
Scrollbar:
Scrollbar is used to select the continuous values between the specified
ranges.
It is collection of several individual parts.
Clicking on arrow it changes the current value of scrollbar one unit and
moves the thumb in the direction of arrow.
43
Constructor:
Scrollbar sb = new Scrollbar ()
Scrollbar sb = new Scrollbar(int style, int init, int thumbsize, int min,
int max)
Where,
style: It is orientation of scrollbar having following values.
Scorllbar.VERTICAL : Displays Vertical Scrollbar
Scrollbar.HORIZONTAL : Displays Horizontal Scrollbar
void setValue (int val): It set the current value of the scrollbar.
void setUnitIncrement (int value): It set the unit value of scrollbar when we
drag the thumb.
void setBlockIncrement (int value): It set the block value of scrollbar when we
click on the space of scrollbar.
45