Applets & Swings
Applets & Swings
Hierarchy of Applet
What is Applet?
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.
Swing Example
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.
JLabel(String s, Icon i, int Creates a JLabel instance with the specified text, im
horizontalAlignment) and horizontal alignment.
Commonly used Methods:
Methods Description
void setText(String text) It defines the single line of text this component
display.
Icon getIcon() It returns the graphic image that the label displays.
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
Action getAction() It returns the currently set Action for this ActionE
source
, or null if no Action is set.
Constructor Description
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 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
void removeAllItems() It is used to remove all the items from the list.
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(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)
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
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
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