AJP Unit-2
AJP Unit-2
Java AWT and Java Swing are both part of a group of Java class libraries called the Java Foundation
Classes (JFC). Java AWT is older than Java Swing. At first, Java provided Abstract Windowing
Toolkit (AWT) for GUI interface designing. AWT is based on the underlying native system
components.
The Abstract Windowing Toolkit (AWT) is the original GUI toolkit shipped with the Java
Development Kit (JDK) and Java Swing is built on Java AWT. Few benefits of choosing Java Swings
comparedt o Java AWT are:
The look and feel and behavior of Java Swing components is consistent across platforms but AWT
components's provides different look and feel and behavior for different platform.
Event model is more efficient in Java Swing as compared to AWT which means Swing components
can run more quickly than their AWT counterparts.
A drawback of Java Swing as compared to Java AWT is that Java Swing components can take longer
to load than AWT components.
Swing features:
Java Swing has many features which makes it ideal for Graphical User Interface (GUI) development.
Few of those are:
Lightweight: Swing component are independent of native Operating System's API as Swing
API controls are rendered mostly using pure JAVA code instead of underlying operating
system calls.
Built-in controls like image buttons, tabbed panes, sliders, toolbars, color choosers, tables,
text areas
Customizable (change border, text alignment, visual appearance of almost any control and
can change)
Pluggable look and feel
New features like tool tips, tool bars, keyboard accelerators, custom cursors, etc. are also
available
Has arbitrary keyboard event binding
Debugging support is also available
MVC Architecture:
The Model-View-Controller (MVC) architectural pattern is used in software engineering to allow for
the separation of three common features of GUI applications:
the data access (typically via a database)
the business logic (how the data will be used)
user interaction (how the data and actions will be visually presented)
Towards this end one defines three components of such an architecture, the model, view and
controller with the following interactions:
There are many differences between java awt and swing that are given below.
AWT doesn't support pluggable look & feel. Swing supports pluggable look and feel.
AWT provides less components than Swing. Swing provides more powerful componentssuch as
tables, lists, scrollpanes, colorchooser, tabbedpane
etc.
JPanel : JPanel is Swing's version of AWT class Panel and uses the same default layout, FlowLayout.
JPanel is descended directly from JComponent.
JFrame : JFrame is Swing's version of Frame and is descended directly from Frame class. The
component which is added to the Frame, is refered as its Content.
JWindow : This is Swing's version of Window and hass descended directly from Window class.
Like Window it uses BorderLayout by default.
JLabel : JLabel descended from Jcomponent, and is used to create text labels.
JButton : JButton class provides the functioning of push button. JButton allows an icon, string or
both associated with a button.
Creating a JFrame
There are two way to create a JFrame Window.
setTitle("MyWindow");
JLabel lb = new JLabel("Welcome to My Second Window");
add(lb);
setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args)
{
new Second();
}
}
JComboBox class:
The JComboBox class is used to create the combobox (drop-down list). At a time only one item can
be selected from the item list.
JComboBox()
JComboBox(Object[] items)
JComboBox(Vector<?> items)
1) public void addItem(Object anObject): is used to add an item to the item list.
2) public void removeItem(Object anObject): is used to delete an item to the item list.
3) public void removeAllItems(): is used to remove all the items from the list.
4) public void setEditable(boolean b): is used to determine whether the JComboBox is editable.
5) public void addActionListener(ActionListener a): is used to add the ActionListener.
6) public void addItemListener(ItemListener i): is used to add the ItemListener.
Example:
import javax.swing.*;
public class Combo
{
JFrame f;
Combo()
{
f=new JFrame("Combo ex");
String country[]={"India","Aus","U.S.A","England","Newzeland"};
f.setLayout(null);
f.setSize(400,500);
f.setVisible(true);
}
public static void main(String[] args)
{
new Combo();
}
}
JProgressBar class:
The JProgressBar class is used to display the progress of the task. Commonly used Constructors of
JProgressBar class:
1) public void setStringPainted(boolean b): is used to determine whether string should be displayed.
2) public void setString(String s): is used to set value to the progress string.
3) public void setOrientation(int orientation): is used to set the orientation, it may be either vertical or
horizontal by using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants..
4) public void setValue(int value): is used to set the current value on the progress bar.
Example:
import javax.swing.*;
public class MyProgress extends JFrame
{
JProgressBar jb;
int i=0,num=0;
MyProgress()
{
jb=new JProgressBar(0,2000);
jb.setBounds(40,40,200,30);
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(400,400);
setLayout(null);
}
JToolTip:
The Tool Tip is used in conjunction with any Swing component. The Tool Tip is used to give more
in-depth information about a component. An example could be for a JButton. The button may only
display an icon, but a user may not know what that icon means. The user would then place the mouse
over the button in order to view its Tool Tip. The Tool Tip can be a short sentence that explains the
functionality of that button. The Tool Tip can be used to explain functionality or to explain what a
component is displaying to the user.
The easiest way to use a Tool Tip is to utilize the JComponent setToolTipText() method. The
argument for this method is a String which is the text to display. An example using a JButton would
be:
This technique can be applied to any JComponent in the Java libraries. Once a JComponent is placed
into your application the text of its Tool Tip can be set.
import java.awt.Color;
import javax.swing.*;
f.add(b,"North");
b.setToolTipText("this is a button");
f.setSize(300, 200);
f.setVisible(true);
}
jSeperator:
The JSeparator class provides a horizontal or vertical dividing line or empty space. It's most commonly
used in menus and tool bars. In fact, you can use separators without even knowing that
a JSeparator class exists, since menus and tool bars provide convenience methods that create and add
separators customized for their containers. Separators are somewhat similar to borders, except that
they are genuine components and, as such, are drawn inside a container, rather than around the edges
of a particular component.
package ajp;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.*;
JTable class :
The JTable class is used to display the data on two dimensional tables of cells.Commonly used
Constructors of JTable class:
import javax.swing.*;
f=new JFrame();
f.setSize(300,400);
f.setVisible(true);
}
public static void main(String[] args)
{
new JTableDemo();
}
}
JTrees:
JTree class is used to wrap a tree based control in javax.swing. A tree orders data in a hierarchical
fashion. Sub trees can be expanded and collapsed by the user.
10 | A d v a n c e d J a v a P r o g r a m m i n g
Swing
JTreeDemo()
{
f=new JFrame();
f.setLayout(new BorderLayout());
11 | A d v a n c e d J a v a P r o g r a m m i n g
Swing
f.setSize(500,500);
f.setVisible(true);
}
public static void main(String args[])
{
JTreeDemo j1=new JTreeDemo();
}
}
Toggle Button
A toggle button is two-states button that allows user to switch on and off. To create a toggle button in
Swing you use JToggleButton class.An implementation of a two-state button.
The JRadioButton and JCheckBox classes are subclasses of this class.
12 | A d v a n c e d J a v a P r o g r a m m i n g
Swing
Example:
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.*;
public class JToggleDemo
{
public static void main(String[] args)
{
f.setSize(300, 200);
f.setVisible(true);
}
}
13 | A d v a n c e d J a v a P r o g r a m m i n g