0% found this document useful (0 votes)
3 views

Applets & Swings

Oops Java 3_2 sem r18 applets & swings important topic covered full information

Uploaded by

polavenitharun48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Applets & Swings

Oops Java 3_2 sem r18 applets & swings important topic covered full information

Uploaded by

polavenitharun48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Applets

Hierarchy of Applet

What is Applet?

An applet is a Java program that can be


embedded into a web page. It runs inside the web
browser and works at client side .
An applet is embedded in an HTML page using the APPLET or OBJECT tag
and hosted on a web server.
Important points :
1. All applets are sub-classes (either directly or indirectly)
of java.applet.Applet class.
2. Applets are not stand-alone programs. Instead, they run
within either a web browser or an applet viewer. JDK
provides a standard applet viewer tool called applet
viewer.
3. In general, execution of an applet does not begin at main()
method.
4. Output of an applet window is not performed
by System.out.println(). Rather it is handled with various
AWT methods, such as drawString().

Life cycle of an applet:

When an applet begins, the following methods are


called, in this sequence:
1. init ()
2. start ()
3. paint ()
When an applet is terminated, the following sequence
of method calls takes place:
1. stop ()
2. destroy ()

Lifecycle methods for Applet


It provides 4 life cycle methods of applet.
Java.applet.Applet class
1. init( ) : The init( ) method is the first method to be called.
This is where you should initialize variables. This method
is called only once during the run time of your applet.

2. start( ) : The start( ) method is called after init( ). It is


also called to restart an applet after it has been
stopped. start( ) is called each time an applet’s HTML
document is displayed onscreen.

3. paint( ) : The paint( ) method is called each time an


AWT-based applet’s output must be redrawn.
4. stop(): stop( ) : The stop( ) method is called when a web
browser leaves the HTML document containing the applet

5. destroy( ) : The destroy( ) method is called when the


environment determines that your applet needs to be
removed completely from memory
.
How to run an Applet?
There are two ways to run an applet
1.By html file.
2.By appletViewer tool
To execute the applet by appletviewer tool, write in
command prompt:
c:\>javac filename.java
c:\>appletviewer filename.java

Displaying Graphics in Applet


java.awt.Graphics class provides many methods
for graphics programming.
Commonly used methods of Graphics class:

1.public abstract void drawString(String str,


int x, int y): is used to draw the specified
string.

2.public void drawRect(int x, int y, int width,


int height): draws a rectangle with the
specified width and height.

3.public abstract void fillRect(int x, int y, int


width, int height): is used to fill rectangle with
the default color and specified width and
height.
4.public abstract void drawOval(int x, int y,
int width, int height): is used to draw oval
with the specified width and height.

5.public abstract void fillOval(int x, int y, int


width, int height): is used to fill oval with the
default color and specified width and height.
6.public abstract void drawLine(int x1, int y1,
int x2, int y2): is used to draw line between
the points(x1, y1) and (x2, y2).

7.public abstract boolean drawImage(Image


img, int x, int y, ImageObserver
observer): is used draw the specified image.

8.public abstract void drawArc(int x, int y, int


width, int height, int startAngle, int
arcAngle): is used draw a circular or elliptical
arc.

9.public abstract void fillArc(int x, int y, int


width, int height, int startAngle, int
arcAngle): is used to fill a circular or elliptical
arc.

10. public abstract void setColor(Color


c): is used to set the graphics current color to
the specified color.
11. public abstract void setFont(Font
font): is used to set the graphics current font
to the specified font.
EventHandling in Applet
As we perform event handling in AWT or Swing, we c
perform it in applet also.
Passing Parameters to Applet

We can get any information from the HTML file as


parameter. For this purpose, Applet class provides
method named
getParameter().
Syntax:
1. public String getParameter
(String parameterName)

GUI Programming with Swing


Java Swing is a part of Java Foundation Classes (JFC) t
is used to create window-based applications.
It is built on the top of AWT (Abstract Windowing Toolkit)
and entirely written in java.
The javax. swing package provides classes f
java
swing API such as JButton, JTextField,
JTextArea, JRadioButton,
JCheckbox, JMenu, JItem etc.

What is JFC
The Java Foundation Classes (JFC) are a set
of GUI components which simplify the
development of desktop applications.
The hierarchy of java swing API is given below.

Commonly used Methods of Component class


The methods of Component class are widely used in java swing that are
given below.
Method Description

public void add(Component c) add a component on another component.

public void setSize(int width,int sets size of the component.


height)

public void sets the layout manager for the component.


setLayout(LayoutManager m)

public void setVisible(boolean b) sets the visibility of the component. It is by


default false.

Swing Example

There are two ways to create a frame:


o By creating the object of Frame class
(association)
o By extending Frame class (inheritance)
We can write the code of swing inside the
main (), constructor or any other method.

Creating a Swing Applet


As we prefer Swing to AWT. Now we can use JApplet that can have all the controls of sw
The JApplet class extends the Applet class.

MVC Architecture
Swing uses the model-view-controller architecture (MVC)
the fundamental design behind each of its components.
Essentially, MVC breaks GUI components into three elements

Swing API architecture follows loosely based MVC architecture in the following manner.
 Model represents component's data.
 View represents visual representation of the component's
data.
 Controller takes the input from the user on the view and
reflects the changes in Component's data.
Painting in Swing
java.awt.Graphics class provides many methods
for graphics programming.

Swing button
Swing defines four types of buttons: JButton, JToggleButton, JCheckBox, and
JRadioButton.

JButton
The JButton class is used to create a labeled button that has platform
independent implementation.

JButton class declaration

public class JButton extends AbstractButton implements Accessible


Commonly used Constructors:
Constructor Description

JButton() It creates a button with no text and icon.

JButton(String s) It creates a button with the specified text.

JButton(Icon i) It creates a button with the specified icon object.


Commonly used Methods of AbstractButton class:
Methods Description

void setText(String s) It is used to set specified text on button

String getText() It is used to return the text of the button.

void setEnabled(boolean b) It is used to enable or disable the button.

void setIcon(Icon b) It is used to set the specified Icon on the button.

Icon getIcon() It is used to get the Icon of the button.

void setMnemonic(int a) It is used to set the mnemonic on the button.

void addActionListener(ActionListener a) It is used to add the action listener to this object

JLabel class declaration


Let's see the declaration for javax.swing.JLabel class.

public class JLabel extends JComponent implements SwingConstants, Accessi


ble
Commonly used Constructors:
Constructor Description

JLabel() Creates a JLabel instance with no image and with an em


string for the title.

JLabel(String s) Creates a JLabel instance with the specified text.

JLabel(Icon i) Creates a JLabel instance with the specified image.

JLabel(String s, Icon i, int Creates a JLabel instance with the specified text, im
horizontalAlignment) and horizontal alignment.
Commonly used Methods:
Methods Description

String getText() t returns the text string that a label displays.

void setText(String text) It defines the single line of text this component
display.

void setHorizontalAlignment(int It sets the alignment of the label's contents along th


alignment) axis.

Icon getIcon() It returns the graphic image that the label displays.

int getHorizontalAlignment() It returns the alignment of the label's contents along


X axis.

TextField class declaration


Let's see the declaration for javax.swing.JTextField class.

1. public class JTextField extends JTextComponent implements SwingConstants

Commonly used Constructors:


Constructor Description

JTextField() Creates a new TextField

JTextField(String text) Creates a new TextField initialized with the specified text

JTextField(String text, int columns) Creates a new TextField initialized with the specified
and
columns.

JTextField(int columns) Creates a new empty TextField with the specified numbe
columns.
Commonly used Methods:
Methods Description

void addActionListener(ActionListener l) It is used to add the specified action listener to rec


action
events from this textfield.

Action getAction() It returns the currently set Action for this ActionE
source
, or null if no Action is set.

void setFont(Font f) It is used to set the current font.

void It is used to remove the specified action listener so


removeActionListener(ActionListener l) it no
longer receives action events from this textfield.

JTextArea class declaration


Let's see the declaration for javax.swing.JTextArea class.

public class JTextArea extends JTextComponent


Commonly used Constructors:

Constructor Description

JTextArea() Creates a text area that displays no text initially.

JTextArea(String s) Creates a text area that displays specified text initially.

JTextArea(int row, int Creates a text area with the specified number of rows
column) columns that displays no text initially.

JTextArea(String s, int row, Creates a text area with the specified number of rows
int column) columns that displays specified text.
Commonly used Methods:
Methods Description

void setRows(int rows) It is used to set specified number of rows.

void setColumns(int cols) It is used to set specified number of columns.

void setFont(Font f) It is used to set the specified font.

void insert(String s, int position) It is used to insert the specified text on the specified positio

void append(String s) It is used to append the given text to the end of the docum

JCheckBox class declaration


Let's see the declaration for javax.swing.JCheckBox class.

public class JCheckBox extends JToggleButton implements Accessible

RadioButton class declaration


Let's see the declaration for javax.swing.JRadioButton class.

public class JRadioButton extends JToggleButton implements Accessible

JComboBox class declaration


Let's see the declaration for javax.swing.JComboBox class.

1. public class JComboBox extends JComponent implements ItemSelectable, List


DataListener, ActionListener, Accessible
Commonly used Constructors:
Constructor Description

JComboBox() Creates a JComboBox with a default data model.

JComboBox(Object[] items) Creates a JComboBox that contains the elements in


specified array.

JComboBox(Vector<?> Creates a JComboBox that contains the elements in


items) specified Vector.

Commonly used Methods:


Methods Description

void addItem(Object anObject) It is used to add an item to the item list.

void removeItem(Object anObject) It is used to delete an item to the item list.

void removeAllItems() It is used to remove all the items from the list.

void setEditable(boolean b) It is used to determine whether the JComboBo


editable.

void addActionListener(ActionListener It is used to add the ActionListener.


a)

void addItemListener(ItemListener i) It is used to add the ItemListener.

JTable class declaration


Let's see the declaration for javax.swing.JTable class.

public class JTable extends JComponent implements ItemListener

Commonly used Constructors:


Constructor Description

JTable() Creates a table with empty cells.

JTable(Object[][] rows, Object[] columns) Creates a table with the specified data.
JScrollPane
A JscrollPane is used to make scrollable view of a
component. When screen size is limited, we use a scroll
pane to display a large component or a component
whose size can change dynamically.
 A JSrollPane is used to make a scrollable view of a component.
 A scroll pane is an object of the JScrollPane class which
extends JComponent class.
 When screen size is limited, we use a scroll pane to display a large component or a
component whose size can change dynamically.
 The important methods of JScrollPane class
are setColumnHeaderView(), setRowHeaderView(), setViewportView() and etc.

Constructors
Constructor Purpose

JScrollPane() It creates a scroll pane. The Component parameter, when present,


the scroll pane's client. The two int parameters, when present, set
vertical and
JScrollPane(Component)
horizontal scroll bar policies (respectively).
JScrollPane(int, int)

JScrollPane(Component,
int, int)

Useful Methods
Modifier Method Description

void setColumnHeaderView(Compo It sets the column header for the scroll pane.
nent)

void setRowHeaderView(Componen It sets the row header for the scroll pane.
t)

void setCorner(String, Component) It sets or gets the specified corner. The


Compone getCorner(String) parameter specifies which corner and must be
nt of the following constants defined in
ScrollPaneConstants:
UPPER_LEFT_CORNER,
UPPER_RIGHT_CORNER,
LOWER_LEFT_CORNER,
LOWER_RIGHT_CORNER,
LOWER_LEADING_CORNER,
LOWER_TRAILING_CORNER,
UPPER_LEADING_CORNER,
UPPER_TRAILING_CORNER.

void setViewportView(Component) Set the scroll pane's client.

Use of ScrollPane
JScrollPane is a container and you can add any component you want to it to make it
scrollable. Use setHorizontalScrollBarPolicy and setVerticalScrollBarPolicy to set the vertical
and horizontal scroll bar policies.

JScrollBar

 The object of the JScrollBar class is used to


add horizontal and vertical scrollbar which allow
the user to select items between a specified
minimum and maximum values.
 A JScrollBar class is an implementation of
a scrollbar and inherits the JComponent class.
Syntax

public class JScrollBar extends JComponent implements Adjustable,


Accessible
differences between a JScrollBar and a
JScrollPane

A JScrollBar is a component and it doesn't handle its


own events whereas a JScrollPane is a Container and
it handles its own events and performs its own scrolling.
A JScrollBar cannot have a JScrollPane whereas
a JScrollPane can have a JScrollBar.

JMenuBar, JMenu and JMenuItem


The JMenuBar class is used to display menubar on the window
or frame. It may have several menus.

The object of JMenu class is a pull down menu component


which is displayed from the menu bar. It inherits the JMenuItem
class.

The object of JMenuItem class adds a simple labeled menu


item. The items used in a menu must belong to the JMenuItem
or any of its subclass.

JMenuBar class declaration


public class JMenuBar extends JComponent implements MenuElement, Acces
sible
JMenu class declaration
public class JMenu extends JMenuItem implements MenuElement, Accessible
JMenuItem class declaration
public class JMenuItem extends AbstractButton implements Accessible, Menu
Element
JTabbedPane
The JTabbedPane class is used to switch between
a group of components by clicking on a tab with
a given title or icon. It inherits JComponent class.

JTabbedPane class declaration


public class JTabbedPane extends JComponent imple
ments Serializable, Accessible, SwingConstants

Commonly used Constructors:


Constructor Description

JTabbedPane() Creates an empty TabbedPane with a default


placement of JTabbedPane.Top.

JTabbedPane(int tabPlacement) Creates an empty TabbedPane with a specified tab


placement.

JTabbedPane(int tabPlacement, int Creates an empty TabbedPane with a specified


tabLayoutPolicy) placement and tab layout policy.

JTree
The JTree class is used to display the tree
structured data or hierarchical data.
JTree is a complex component. It has a 'root
node' at the top most which is a parent for all
nodes in the tree.
It inherits JComponent class.
JTree class declaration

public class JTree extends JComponent imple


ments Scrollable, Accessible

Commonly used Constructors:


Constructor Description

JTree() Creates a JTree with a sample model.

JTree(Object[] value) Creates a JTree with every element of the specified array as the child
new root node.

JTree(TreeNode root) Creates a JTree with the specified TreeNode as its root, which displays
root node.

DefaultMutableTreeNode
This class provides enumerations for efficiently
traversing a tree or subtree in various orders or for
following the path between two nodes
public class DefaultMutableTreeNode
extends Object
implements Cloneable, MutableTreeNode,
Serializable

A DefaultMutableTreeNode is a general-purpose
node in a tree data structure.
Constructor and Description
DefaultMutableTreeNode()
Creates a tree node that has no parent and no children, but whi
allows children.

DefaultMutableTreeNode(Object userObject)
Creates a tree node with no parent, no children, but which allow
children, and initializes it with the specified user object.

DefaultMutableTreeNode(Object userObject,
boolean allowsChildren)
Creates a tree node with no parent, no children, initialized
with the specified user object, and that allows children only if sp

You might also like