Applet or Swings Assignments

1. Write a simple Applet to display your name as a Banner, that is scroll your name right to left. Scroll by one character at a time. A Thread may be used for smoother scrolling effect. You can use Thread.sleep() to cause delay, in scrolling.
2. Repeat above 1. assignment, except scroll two characters at a time
3. Repeat above 1. assignment, except scroll in opposite direction, i..e from left to right.
4. Repeat above 1. assignment, except scroll one word at a time, instead of one character at a time.
5. Develop an Applet with textarea and button. Which some Text is filled in textarea, the text content need to be save to file, when button is clicked.
6. Develop windows explorer kind of utility, using which user can explore, files/folders stored in file system.

You may also like to read:
Core Java assignments
Basic Java assignments

In Swing, how to use JTabbedPane to create multiple Tabs

Below are the Swing UI Components used in this example
JPanel
JTabbedPane
JButton
JCheckBox
JComboBox

There are three Tabbed Panes, implemented in below classes.
1.CitiesPanel(extends from JPanel)
2.ColorsPanel(extends from JPanel)
3.FlavorsPanel(extends from JPanel)

CitiesPanel has four JButton components with City names.
When any of these Buttons are clicked, corresponding label on JButton, is set to JTextField.
CitiesPanel implements java.awt.event.ActionListener interface, to handle button click event.

JTabbedPane example
JTabbedPane example

ColorsPanel has three JCheckBox components with Color names.
When any of the check box is selected/unselected, name of check box gets displayed on dialog box, using JOptionPane.
ColorsPanel implements java.awt.event.ItemListener interface, to handle check box check/uncheck event.

JTabbedPane example
JTabbedPane example

FlavorsPanel has a JComboBox, with Fruit names.
When any item is selected from the drop down combox box. The selected item text, is set to JTextField.
FlavorsPanel implements java.awt.event.ItemListener interface, to handle combo box item selected event.

JComboBox-example
JComboBox-example

import javax.swing.*;

import java.awt.event.*;

/* 
 * 
 */

public class TabbedPaneDemo extends JApplet{
public void init()
	{
	JTabbedPane jtp = new JTabbedPane();
	jtp.addTab("Cities", new CitiesPanel());
	jtp.addTab("Colors", new ColorsPanel());
	jtp.addTab("Flavors", new FlavorsPanel());

	getContentPane().add(jtp);
	}

}

class CitiesPanel extends JPanel implements ActionListener{
	JTextField jft;
	public CitiesPanel(){
	JButton b1 = new JButton("Bangalore");
	b1.addActionListener(this);
	add(b1);
	JButton b2 = new JButton("Mumbai");
	b2.addActionListener(this);
	add(b2);
	JButton b3 = new JButton("Chennai");
	b3.addActionListener(this);
	add(b3);
	JButton b4 = new JButton("Delhi");
	b4.addActionListener(this);
	add(b4);

	jft = new JTextField(8);

	add(jft);

	}

	public void actionPerformed(ActionEvent ae)
	{
	String str = ae.getActionCommand();
	jft.setText(str);
	}
}

class ColorsPanel extends JPanel implements ItemListener{

	JCheckBox cb1,cb2,cb3;
	public ColorsPanel(){
	cb1 = new JCheckBox("Yellow");
	add(cb1);

	cb2 = new JCheckBox("Grey");
	add(cb2);

	cb3 = new JCheckBox("Blue");
	add(cb3);

	cb1.addItemListener(this);
	cb2.addItemListener(this);
	cb3.addItemListener(this);
	}

	public void itemStateChanged(ItemEvent ie)
	{
	JCheckBox cb = (JCheckBox)ie.getItem();
	JOptionPane.showMessageDialog(null, "You Selected:"+cb.getText());
	}
}


class FlavorsPanel extends JPanel implements ItemListener{
	JTextField jft1;
	public FlavorsPanel(){
	JComboBox jcb = new JComboBox(); //Drop down menu
	jcb.addItem("Mango");
	jcb.addItem("Orange");
	jcb.addItem("Chocolate");
	add(jcb);

	jft1 = new JTextField(8);
	jft1.setText("No Item Selected");
	add(jft1);

	jcb.addItemListener(this);
	}

	public void itemStateChanged(ItemEvent ie)
	{
	String str = (String)ie.getItem();

	jft1.setText(str);
	}
}

You may also like to read:
Advantages of Swing over Applet

What is an Applet, and its Life Cycle

An Applet, is used to develop Graphical User Interface Programs in Java.

Also Applets can be embedded as part of a HTML form. All Apllets need to inherit from java.applet.Applet class. Below are Life Cycle Methods, which need to be ovveridden in the Derived class of Applet

import java.applet.Applet;
import java.awt.Graphics;

/*
<applet code="AppletLifeCycleExample" width=150 height=150>
</applet>
*/

public class AppletLifeCycleExample extends Applet{

   /* Constructor is invoked first, when instance of the Applet is created */
   public AppletLifeCycleExample()
   {
   System.out.println("AppletLifeCycleExample()");
   }

    /*
     * init method is called only once, just before displaying
     * applet window
     */
    public void init() {
    super.init();
    System.out.println("init()");
    }

    /*
     * start method is called, whenever applet is displayed(first time)
     * or re displayed(whenever applet window is maximized, or when navigated back)
     */
    public void start() {
    super.start();
    System.out.println("start()");
    }

    /*
     * stop method is called when ever user navigates away or minimizes
     * the applet.
     */
    public void stop() {
    super.stop();
    System.out.println("stop()");
    }

    /* paint method is called every time applet need to be redrawn
     * It is possible to explicitly invoke paint(), using repaint() method
     */
    public void paint(Graphics g) {
    super.paint(g);
    System.out.println("paint()");
    }

    /*
     * destroy method is called only once when this applet is about to be completely destroyed.
     * Add cleanup code to free resources which are initialized
     * during the init method.
     */
    public void destroy() {
    super.destroy();
    System.out.println("destroy()");
    }

}
Output:
AppletLifeCycleExample()
init()
start()
paint()
stop() --minimized applet--
start() --maximized applet--
paint()
stop() -- again minimized applet--
start() -- again maximized applet--
paint()
stop() -- closed applet--
destroy()

You may also like to read:
Advantages of Swings compared to Applet

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: