Advantages of Swing Over Applets

Both Applet and Swing provides Graphical user interface to Standalone Java Programs. These days Swing has almost replaced Applet, due to below Advantages.
1. Swing is light weight, compared to Applet.
2. Swing has rich set of UI Components, like Image Button, Tabbed Pane,etc…
3. Swing provide common look and feel, across various Platforms, as Swing has built in UI Tool kit. But Applet uses different local UI Tool Kit for each Platform, hence for Applets Look and feel differs across platforms.

Swing UI Component classes are available in javax.swing package.

Below is list of commonly used Swing UI Component classes

UI Component Description
AbstractButton
ButtonGroup
JApplet Swing version of Applet
JButton Swing Button class
JCheckBox Swing Check box class
JComboBox Swing Combo box class
JFrame extended version of java.awt.Frame, which adds support for the JFC/Swing component architecture
JLabel Swing Label class
JOptionPane displays pop up a standard dialog box that prompts users for a value or informs something
JRadioButton Swing radio button class
JScrollPane used to display scroll bars
JTabbedPane Displays Tabbed based windows
JTable To display data in rows and columns
JTextArea editable multiline text
JTextField editable one line text field
JTree Displays Tree hierarchy, For eg. can be used to display Folder structure,etc…

Below is example Login form, using Swing

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class LoginForm implements ActionListener {

	JFrame jf;
	JPanel p1, p2;
	JLabel l1, l2;
	JTextField t1, t2;
	JButton b1, b2;
	public Font f2 = new Font("Calibrie", Font.BOLD, 13);
	JLabel name = new JLabel("Login Form");

	public Font f1 = new Font("Bell MT", Font.BOLD, 14);

	LoginForm() {

	jf = new JFrame("Login-Form");
	p1 = new JPanel();
	p2 = new JPanel();
	p1.setLayout(null);
	jf.setLayout(null);

	name.setBounds(80, 0, 300, 50);
	name.setFont(f2);
	name.setForeground(Color.blue);
	p1.add(name);

	l1 = new JLabel("Username:");
	l1.setBackground(Color.LIGHT_GRAY);
	l2 = new JLabel("Password:");
	l2.setBackground(Color.LIGHT_GRAY);

	t1 = new JTextField(15);

	t2 = new JTextField(15);

	b1 = new JButton("SUBMIT");
	b2 = new JButton("CANCEL");

	l1.setBounds(50, 50, 100, 30);
	l2.setBounds(50, 100, 100, 30);

	t1.setBounds(150, 50, 150, 30);
	t2.setBounds(150, 100, 150, 30);

	b1.setBounds(70, 200, 100, 30);
	b1.setForeground(Color.RED);
	b1.setBackground(Color.black);
	b1.setFont(f1);
	b2.setBounds(190, 200, 100, 30);
	b2.setForeground(Color.RED);
	b2.setBackground(Color.black);
	b2.setFont(f1);
	p1.setBounds(50, 50, 350, 300);
	p1.setBackground(Color.LIGHT_GRAY);

	p2.setBounds(1, 1, 460, 400);
	p2.setBackground(Color.DARK_GRAY);

	p1.add(l1);
	p1.add(l2);
	p1.add(t1);
	p1.add(t2);
	p1.add(b1);
	p1.add(b2);

	jf.add(p1);
	jf.add(p2);

	jf.setBounds(100, 50, 471, 437);
	jf.setVisible(true);

	b1.addActionListener(this);
	b2.addActionListener(this);
	}

	public static void main(String[] args) {
	new LoginForm();
	}

	public void actionPerformed(ActionEvent e) {

	if (e.getSource() == b1) {

	String uname = t1.getText();
	String pwd = t2.getText();

	if (name.equals("")) {
	JOptionPane.showMessageDialog(null, "Enter UserName");
	} else if (pwd.equals("")) {
	JOptionPane.showMessageDialog(null, "Enter Password");
	} else {
	JOptionPane.showMessageDialog(null, "Your username:"+uname+" pwd is:"+pwd);
	t1.setText("");
	t2.setText("");
	}
	}

	if (e.getSource() == b2) {
	t1.setText("");
	t2.setText("");
	}

	}
}

You may also like to read:

2 thoughts on “Advantages of Swing Over Applets”

Leave a Reply