Swing: Difference Between AWT and Swing
Swing: Difference Between AWT and Swing
The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
There are many differences between java awt and swing that are given below.
3) AWT provides less components than Swing. Swing provides more powerful components such as
tables, lists, scrollpanes, colorchooser and` etc.
Method Description
setSize(int width,int height) sets the size (width and height) of the component.
SwingExample.java
import javax.swing.*;
public class SwingExample
{
public static void main(String[] args)
{
JFrame f=new JFrame("Simple Swing Example");
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
(OR)
In Constructor()
import javax.swing.*;
class SwingExample extends JFrame
{
SwingExample()
{
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not visible
setTitle("SwingExample ");//Set Title
}
public static void main(String args[]){
SwingExample f=new SwingExample();
}
}
JButton:
The JButton class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed.
Syntax:
JButton b=new JButton(“Text");
(Or)
JButton b1,b2;
b1=new JButton(“Text”);
b.setBounds(50,100,80,30);
JLabel:
The JLabel class is a component for placing text in a container. It is used to display
a single line of read only text. The text can be changed by an application but a user
cannot edit it directly.
Syntax:
JLabel l1=new JLabel(“Text”);
(or)
JLabel l1,l2;
l1=new JLabel(“Text”);
JTextField:
The JTextField class is a text component that allows the editing of a single line
text.
Syntax:
JTextField t1=new JTextField(“Text”);
(or)
JTextField t1,t2;
t1=new JTextField(“Text”);
JTextArea :
The JTextArea class is a multi line region that displays text. It allows the editing of
multiple line text.
Syntax:
JTextArea t1=new JTextArea(“Text”);
(or)
JTextArea t1,t2;
t1=new JTextArea(“Text”);
JRadioButton
The JRadioButton class is used to create a radio button. It is used to choose one
option from multiple options. It is widely used in exam systems or quiz.
• It should be added in ButtonGroup to select one radio button only.
Syntax:
ButtonGroup bg=new ButtonGroup();
JRadioButton r1=new JRadioButton("Male");
JRadioButton r2=new JRadioButton("Female");
bg.add(r1);bg.add(r2);
JTable:
The JTable class is used to display data in tabular form. It is composed of rows
and columns.
Syntax:
String data[][]= { {“521",“Madhu",“43400"}, {“512",“Hari",“54500"},
{“509",“Ganesh","70000"}};
String column[]={"ID","NAME","SALARY"};
JTable jt=new JTable(data,column);
jt.setBounds(30,40,200,300);
JPanel:
The JPanel is a simplest container class. It provides space in which an application
can attach any other component.
Syntax:
JPanel panel=new JPanel();
panel.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
JButton b1=new JButton("Button 1");
b1.setBounds(50,100,80,30);
panel.add(b1);
JDialog:
The JDialog control represents a top level window with a border and a title used
to take some form of input from the user.
• Unlike JFrame, it doesn't have maximize and minimize buttons.
Syntax:
JFrame f= new JFrame();
JDialog d=new JDialog(f , "Dialog", true);
JButton b = new JButton ("OK");
d.add(b);
23 GUI Programming in Java |Madhu T.
Example: An example for JButton Component in swing.
JButtonExample.java
import javax.swing.*;
public class JButtonExample
{
public static void main(String[] args)
{
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Execution:
D:/>javac JButtonExample.java
D:/>java JButtonExample
Advantage of Applet
There are many advantages of applet. They are as follows:
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under many platforms, including Linux,
Windows, Mac Os etc.
Hierarchy of Applet :
As displayed in the diagram, Applet class extends Panel. Panel class extends
Container, which is the subclass of Component. Where Object class is base class for
all the classes in java.
public void init(): is used to initialized the Applet. It is invoked only once.
public void start(): is invoked after the init() method or browser is maximized. It is
used to start the Applet.
public void paint(Graphics g): is invoked immediately after the start() method,
and this method helps to create Applet’s GUI such as a colored background, drawing
and writing.
public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized.
public void destroy(): is used to destroy the Applet. It is invoked only once.
Remember:
java.applet.Applet class provides 4 methods (init,start,stop & destroy) and
java.awt.Graphics class provides 1 method ( paint) to create Applet.
• drawString(String str, int x, int y): is used to draw the specified string.
• drawRect(int x, int y, int width, int height): draws a rectangle with the specified
width and height.
• fillRect(int x, int y, int width, int height): is used to fill rectangle with the default
color and specified width and height.
• drawOval(int x, int y, int width, int height): is used to draw oval with the
specified width and height.
• fillOval(int x, int y, int width, int height): is used to fill oval with the default color
and specified width and height.
• drawLine(int x1, int y1, int x2, int y2): is used to draw line between the
points(x1, y1) and (x2, y2).
• setColor(Color c): is used to set the graphics current color to the specified color.
• setFont(Font font): is used to set the graphics current font to the specified font.
Example: GraphicsDemo.java
import java.applet.Applet;
import java.awt.*;
public class GraphicsDemo extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
}
}
35 GUI Programming in Java |Madhu T.
GraphicsDemo.html
<html>
<body>
<applet code="GraphicsDemo.class" width="300" height="300">
</applet>
</body>
</html>
Execution:
D:\> javac GraphicsDemo.java
D:\> appletviewer GraphicsDemo.html
Components of Applet:
The components of AWT are the components of Applet,i.e we can use AWT
components (Button,TextField,Checkbox, TextArea,Choice & etc.…) in applet.
Execution:
D:\>javac AppletComponents.java
D:\>appletviewer AppletComponents.html