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

Unit-II Swing and MVC Architecture

Uploaded by

Tanmay Warthe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Unit-II Swing and MVC Architecture

Uploaded by

Tanmay Warthe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Advanced Java Programming

by
Amol S. Gaikwad
Lecturer,
Government Polytechnic Gadchiroli
Advanced Java Programming
Unit-II
Swings and MVC
Architecture
Welcome!
Are you excited for a fun
learning session?
Unit Outcomes
Differentiate between AWT and Swing on the given aspect

Develop Graphical User Interface (GUI) programs using swing component for given
problems

Use the given type of button in Java based GUI

Develop Graphical User Interface (GUI) programs using advanced swing


component for the given problems
What is Swing ?
Swing is built from AWT It is a set of classes in java

Swing
Swing is used for creating More powerfull and more
Graphical User Interface flexible components
(GUI)

Additional components like Swing related classes are present


tabbed panes, scroll panes, trees in javax.swing package of java
and tables
Features of Swing
LightWeight Pluggable Look & Fill
Components are Possible to separate look
lightweight and feel of component
from it's logic
Written entirely in java
and don't depend on
Swing
possible to define entire
platform
sets of look-and-feels that
represent different GUI
lightweight components styles.
are more efficient and
flexible Look and feel consistent
across all platforms as
Look & feel of
well as for specific
components decide d by
platforms
swing not by O.S
Components and Containers

Container contains
Container group of components

Components are single


visual controls like :
Component
push buttons
checkbox

label
TextField etc.

Container also contains


other container

Fig : Containment Heirarchy


AbstractButton JComboBox

Classes in ButtonGroup JLabel

Swing ButtonGroup JRadioButton


ImageIcon
JScrollPane
JApplet
JTabbedPane
JCheckBox
JTable
JTree
JTextField
Swing vs AWT
Less powerfull and More powerfull and
less flexible more flexible

Have tabbed panes,


Don't have tabbed
scroll panes,trees, tables
panes, scroll
panes,trees, tables

AWT
Platform dependant Platform independant
Swing

Each component has


Each component has
few capabilities
more capabilities

Heavyweight code
lightweight code
JApplet class
If your applet uses swing then it must be subclass of JApplet class

JApplet extends Applet class

JApplet has more functionality than Applet

To add component in JApplet we create an object of Container


class which is returned by getContentPane() function

Container contentPane = getContentPane()

Call add() fuction of Container class using object of Container class


to add component to JApplet

contentPane.add(componet)
ImageIcon class and Icon interfacce
ImageIcon class is used to create icons (image)

Constructors of ImageIcon class :

ImageIcon(String filename) - creates icon from image file

ImageIcon(URL url) - creates icon from image given in resource url

Icon interface functions :

int getIconHeight() - returns height of icon in pixels


int getIconWidth() - returns width of icon in pixels

void paintIcon(Componet comp, Graphics g, int x, int y) - paints icon at


position x, y using graphics information in g object and additional
information in comp object
Labels - JLabel class
JLabel class is usedto create labels.

JLabel class is child \ subclass of JComponent class

It can display text as well as icons

Constructors of JLabel class :

JLabel(Icon i) - creates label from icon object i

JLabel(String s) - creates label from string s

JLabel(String s, Icon i, int align) - creates label using both string and icon and
also aligns the label to right,center,leading or trailing
Labels - JLabel class
Functions in JLabel class :

Icon getIcon() - returns icon used in label

String getText() - returns text used in label

void setIcon(Icon i) - sets or changes icon in the label

void setText(String s) - sets or changes text in the label


Sample Program of JLabel
import java.awt.*;
import javax.swing.*;
/*
<applet code="JLabelExample" width=250 height=150>
</applet>
*/
public class JLabelExample extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
ImageIcon img = new ImageIcon("emojis.jpg");
JLabel obj = new JLabel("New Label",img,JLabel.CENTER);
contentPane.add(obj); Output of Program
}
}
TextField - JTextField class
JTextField class is used to create single line text entry

JTextField class inherits JTextComponent class and JTextComponent inherits


JComponent class

Constructors of JTextField class :

JTextField() - creates simple textfield

JTextField(int cols) - creates textfield with no. of columns equal to col

JTextField(String s,int cols) - creates textfield with string s and no. of


columns equal to col

JTextField(String s) - creates textfield with string s


Sample Program of JTextField
import java.awt.*;
import javax.swing.*;
/*
<applet code="JTextFieldExample" width=250 height=150>
</applet>
*/

public class JTextFieldExample extends JApplet


{
JTextField obj;
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
obj = new JTextField("Enter Name",25);
contentPane.add(obj); Output of Program
}
}
Buttons in Swing
Using swing we can create button with icons

Swing buttons are subclass(child) class of AbstractButton class and


AbstractButton is subclass of JComponent class

Functions for buttons in swing :


void setDisabledIcon(Icon i) - displays icon when other icon is disabled

void setPressedIcon(Icon i) - displays icon when other icon is pressed

void setSelectedIcon(Icon i) - displays icon when other icon is selected

void setRolloverIcon(Icon i) - displays icon when mouse positioned over other icon
String getText() - returns text of a button
void setText() - changes text of button
Buttons - JButton class
It is used to create push buttons

We can create buttons with string, icon or both

Constructors of JButton class :

JButton(Icon i) - creates button with icon

JButton(String s) - creates button with string

JButton(String s, icon i) - creates button with both string and icon


Sample program of JButton
import java.awt.*;
import javax.swing.*;
/*
<applet code="JButtonExample" width=250 height=150>
</applet>
*/

public class JButtonExample extends JApplet


{
JButton obj;
public void init()
{
Container contentPane = getContentPane();
ImageIcon img =new ImageIcon("cg4.jpg");
contentPane.setLayout(new FlowLayout());
obj = new JButton(img);
contentPane.add(obj);
Output of Program
}
}
Checkboxes - JCheckBox class
It is used to create two state checkbox

JCheckBox is subclass (child) class of JTogglebutton class and JTogglebutton is


subclass of AbstractButton

Constructors of JCheckBox class :

JCheckBox(Icon i) - creates checkbox with icon


JCheckBox(Icon i,boolean state) - creates checkbox with icon and state true or false

JCheckBox(String s, boolean state) - creates checkbox with the string and state

JCheckBox(String s, Icon i) - creates checkbox with the both string and icon

JCheckBox(String s, Icon i, boolean state) - creates checkbox with the


string, icon and state true or false
Sample program of JCheckBox
import java.awt.*;
import javax.swing.*;
/*
<applet code="JCheckBoxExample" width=250 height=150>
</applet>
*/
public class JCheckBoxExample extends JApplet
{
JCheckBox c1, c2;
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
c1 = new JCheckBox("Mechanical");
c2 = new JCheckBox("Computer");
contentPane.add(c1);
contentPane.add(c2);
}
}
Output of Program
Radio Buttons - JRadioButton class
It is used to create radio buttons

JRadioButton is subclass (child) class of JTogglebutton class and JTogglebutton is


subclass of AbstractButton

Radio buttons must br group together using ButtonGroup class

Default constructor of ButtonGroup class is used to create group of buttons

Only one button from group of buttons can be selected at a time

void add(AbstractButton obj) - adds a single button in group of button


Radio Buttons - JRadioButton class
Constructors of JRadioButton class :

JRadioButton(Icon i) - creates radio button with icon


JRadioButton(Icon i,boolean state) - creates radio button with icon and state
true or false

JRadioButton(String s, boolean state) - radio button with the string and state

JRadioButtonString s, Icon i) - creates radio button with the both string and icon

JRadioButton(String s, Icon i, boolean state) - creates radio button with the


string, icon and state true or false
Sample program of JRadioButton
import java.awt.*;
import javax.swing.*;
/*
<applet code="JRadioButtonExample" width=250
height=150>
</applet>
*/
public class JRadioButtonExample extends JApplet
{
JRadioButton r1, r2;
ButtonGroup bg;
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
r1 = new JRadioButton("Civil");
r2 = new JRadioButton("Electrical");
bg = new ButtonGroup(); Output of Program
bg.add(r1);
bg.add(r2);
contentPane.add(r1);
contentPane.add(r2);
}
Combo Boxes - JComboBox class
Combo box is combination of text field and drop down list

JComboBox class is subclass (child) of JComponent class

Constructor of JComboBox class :

JComboBox() - creates default combo box

JComboBox(Vector v) - creates combo box with elements from vector

void addItem(Object obj) - adds choice items or elements to combo box


Sample Program of JComboBox class
import java.awt.*;
import javax.swing.*;
/*
<applet code="JComboExample" width=250 height=150>
</applet>
*/
public class JComboExample extends JApplet
{
JComboBox jb;
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
jb = new JComboBox();
jb.addItem("Male");
jb.addItem("Female");
jb.addItem("Other"); Output of Program
contentPane.add(jb);
}
}
Tabbed Panes - JTabbedPane class
Tabbed pane is group of folders in file cabinet, each folder has a name

Only one folder can be selected at time

Tabbed panes are commonly used for configuration setting options

JTabbedPane class is subclass (child) of JComponent class

void addTab(String str, Component comp) - adds component to a tab, str is title of a
tab and comp is component to be added to the tab
Tabbed Panes - JTabbedPane class
Procedure to create tabbed pane in applet

Create object of JTabbedPane class

Add tabs to the pane by using addTab() function

Repeat step 2 for adding more tabs

Add the tabbed pane to the content pane of the applet


Sample Program of JTabbedPane class
import java.awt.*;
import javax.swing.*; class College extends JPanel
import javax.swing.tree.*; {
/* public College()
<applet code="JTabExample" width=250 height=150> {
</applet> JButton c = new JButton("Government
*/ Polytechnic Gadchiroli");
add(c);
public class JTabExample extends JApplet }
{ }
public void init() class Departments extends JPanel
{ {
JTabbedPane jtp = new JTabbedPane(); public Departments()
jtp.add("College",new College()); {
jtp.add("Departments",new Departments()); JButton d1 = new JButton("Computer");
getContentPane().add(jtp); JButton d2 = new JButton("Mechanical");
} add(d1);
add(d2);
}
}
}
Output of Program
Scroll Panes - JScrollPane class
Scroll pane is a rectangular area in which other component can be seen

Scroll pane is created using JScrollPane class, which sublcass (child) of JComponent
class

Constructors of JScrollPane class :

JScrollPane(Component comp) - adds component to scroll pane

JScrollPane(int vsb, int hsb) - creates vertical and horizontal scroll bar

JScrollPane(Component comp, int vsb, int hsb) - adds component and creates
vertical and horizontal scroll bar
Scroll Panes - JScrollPane class
Constants in JScrollPane class :

HORIZONTAL_SCROLLBAR_ALWAYS - always used hoizontal scroll bar

HORIZONTAL_SCROLLBAR_AS_NEEDED - used hoizontal scroll bar when needed

VERTICAL_SCROLLBAR_ALWAYS - always used vertical scroll bar

VERTICAL_SCROLLBAR_AS_NEEDED - used vertical scroll bar when needed


Scroll Panes - JScrollPane class
Procedure to create scroll pane in an applet

Create object of JComponent class

Create object of JScrollPane class

Add scroll pane to content pane of applet


Sample Program of JScrollPane class
import java.awt.*;
for(int i=0;i<10;i++)
import javax.swing.*; {
/* for(int j=0;i<10;j++)
<applet code="JScrollExample" width=250 {
height=150> jp.add(new JButton("button "+b));
++b;
</applet> }
*/ }
int v =ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
public class JScrollExample extends JApplet int h =ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jp,v,h);
{
contentPane.add(jsp,BorderLayout.CENTER);
public void init() }
{ }
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(10,10));
int b=0;
Trees - JTree class
Tree is a component that shows heirarchical ( level by level) view of data

We can expand or collapse each subtree seen in display

Constructors of JTree class :

JTree(Hashtable ht) - creates tree with each element in hash table is child node

JTree(Object obj[ ]) - creates tree with each element in array obj is child node

JTree(TreeNode tn) - Makes node tn as root node or main node of a tree

JTree(Vector v) - creates tree with elements of vector v as child nodes


Trees - JTree class
Object of TreePath class stores information about tree node that was selected

TreePath class also stores information about path or location of tree node

TreeNode interface has methods that gives information about tree node

MutableTreeNode interface has methods that can insert and remove child nodes
and change parent node also

MutableTreeNode implements (inherits) TreeNode interface

DefaultMutableTreeNode class implements (inherits) MutableTreeNode interface, it


represents a node in a tree

DefaultMutableTreeNode(Object obj) - default constructor


Trees - JTree class
Procedure to create tree in an applet

Create object of JTree class

Create object of JScrollPane class

Add the tree to scroll pane

Add the scroll pane to the content pane of the applet


Sample Program of JTree class
import java.awt.*;
import javax.swing.*; top.add(a);
import javax.swing.tree.*; top.add(b);
/* tree = new JTree(top);
<applet code="JTreeExample" width=250 height=150> int v =
</applet> ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
*/ int h =
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDE
public class JTreeExample extends JApplet
D;
{
JScrollPane jsp = new JScrollPane(tree,v,h);
JTree tree;
public void init() contentPane.add(jsp,BorderLayout.CENTER);
{ }
Container contentPane = getContentPane(); }
contentPane.setLayout(new BorderLayout());
DefaultMutableTreeNode top = new
DefaultMutableTreeNode("Parent Node");
DefaultMutableTreeNode a = new DefaultMutableTreeNode("Child
Node 1");
DefaultMutableTreeNode b = new DefaultMutableTreeNode("Child
Node 2");
Output of Program
Tables - JTable class
Table is a component that displays data in rows and columns

Tables are created using JTable class , JTable class is sublcass (child) JComponent
class

Constructor of JTable class :

JTable(Object data[ ][ ],Object colheads[ ]) - creates table from data in two


dimensional array, and with headings from colheads array
Tables - JTable class
Procedure to create tree in an applet

Create object of JTable class

Create object of JScrollPane class

Add the table to scroll pane

Add the scroll pane to the content pane of the applet


Sample Program of JTree class
import java.awt.*;
JTable table = new JTable(data,colheads);
import javax.swing.*;
int v =ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
/*
int h =ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
<applet code="JTableExample" width=250
JScrollPane jsp = new JScrollPane(table,v,h);
height=150>
contentPane.add(jsp,BorderLayout.CENTER);
</applet>
}
*/
}
}
public class JTableExample extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
final String[] colheads = {"Student
Name","Branch"};
final Object[][] data = {{"Akshay","Computer"},
{"Mahesh","Electrical"} };
Output of Program
MVC Architecture
A visual component has three distinct aspects
How it looks on screen
How it reacts to user

State information of that component

MVC architecture is suitable for such components

In MVC architecture,M = Model, V = View, C = Controller

In MVC, Modal stores information about state of the component. Example - if we


create a checkbox then modal will store information about whether checkbox is
checked or not
MVC Architecture
In MVC, View decides how the component is displayed on screen, considering
current state or information in Model

In MVC, Controller decides how to the component react to user. Example - when
we click on checkbox,controller changes the model to show user choice ( checked
or unchecked )

Swing uses modified version of MVC which combines view and control into single
entity called UI delegate

Hence Swing's architecture is called as Model-Delegate architecture or Separable


Model architecture
Architectures

Model
Model

UI Delegate View Controller

View + Controller

Model-Delegate or Separable MVC Architecture


Model Architecture
Activity Time
Assessment Test
Program Assignment
Group Discussion
Supplemental
Video
https://nptel.ac.in/courses/106/105/1
06105191/
Additional Resources
https://www.tutorialspoint.com/java

https://www.javatpoint.com/free-java-
projects
Summary of Class
1 2 3
Lesson Recap 1 Lesson Recap 2 Lesson Recap 3
Swing vs GUI Using Buttons in GUI
AWT Swing

4 3
Lesson Recap 4
Develop GUI
using swing
components
Refrences
The Complete Reference Java Seventh Edition - Herbert
Schildt,McGraw Hill Publication
Thank You
For Attending!

You might also like