0% found this document useful (0 votes)
20 views40 pages

Unit II Swigs

Uploaded by

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

Unit II Swigs

Uploaded by

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

UNIT – II

SWINGS
Unit Outcomes (UOs): -

2a. Differentiate between AWT and Swing on the given aspect.


2b. Develop Graphical user interface (GUI) programs using swing components
for the given problem
2c. Use the given type of button in Java based GUI.
2d. Develop Graphical user interface (GUI) programs using advanced
swing components for the given problem

TOTAL MARKS=10
Qno1: State the difference between AWT and swing.
Ans: Difference between AWT and Swing in java
AWT
Swing
AWT stands for Abstract Window Toolkit. Swing is a part of Java
Foundation Class (JFC).

AWT components are heavy weight. Swing components


are light weight.

AWT components are platform dependent Swing components


are platform independent so there look and feel changes according to OS. look
and feel remains constant
AWT components are not very good Swing components
are better in look and feel as compared to Swings.
compared to AWT components.
⮚ Introduction to Swing

∙ Swing is another approach of graphical programming in Java.

∙ Swing creates highly interactive GUI applications.

∙ It is the most flexible and robust approach Swing Features

Swing has following two important features –

1. Pluggable Look and Feel:

∙ Swing support several looks and feels. Currently it includes support for Windows 98 and UNIX Motif.
∙ Swing allows the user to switch look and feel at runtime without closing the current application.
∙ It is possible to create your own look and feel for swing components.

1. Lightweight Components:
Most of the Swing components are lightweight. That means using simple graphics primitive components can be created
1. Additional Features

∙ Swing contains wide variety of new components such as tables, trees, sliders, progress bars and so on.
⮚ Limitations of AWT

Following are some limitations of AWT -

∙ AWT supports limited number of GUI components

∙ The components defined by the AWT are heavy weight components


∙ The behavior of AWT components varies when the container operating system changes.
∙ The AWT components are developed by using the platform specific code.
• The AWT component is converted by the native code of the operating system.
⮚ Swing Components
∙ In order to display any JComponent on the GUI, it is necessary to add this component to the container first.
∙ There are two important features of swing components - Firstly, all the component classes begin with the letter J and secondly
all these GUI components are descendant of JComponent class.
⮚ JFrame

∙ In Java, Frame is a standard graphical window.

∙ The frame can be displayed using the Frame class.

∙ The frame drawn using this class has standard minimize, maximize and close buttons.
∙ The syntax of frame class is -
i. JFrame ()

This creates the new instance of frame which is invisible initially.

i. JFrame (String title)

This creates the new instance of frame which has some title.
∙ Following table enlists various methods of Frame class
⮚Methods in JFrame class:-
void setResizable(boolean resizable) 🡪Sets frame to resizable

void setTitle(String Title) 🡪 Sets the title of the frame

void setSize(int width,int height) 🡪Sets the width and height of a frame

String getTitle() 🡪Obtains the title of the


frame

void setVisible(boolean visible) 🡪Set the frame visible or not.


⮚ JApplet

∙ The JApplet is a fundamental swing class. It extends the Applet class.


∙ This is much more powerful than the Applet class.

∙ It supports the pane.

∙ Various panes are content pane, glass pane, and root pane.

1. The add method can be used to add the content pane.

2. The add method of Container class can be used to add the components on the GUI.

3. All the life cycle methods used (such as init paint) in Applet class can be used with
JApplet.
⮚ Icons

∙ The icons are encapsulated by ImageIcon class. The constructors are -


ImageIcon(string fname)

ImageIcon(URL url)

The fname denotes the name of the file which contains the icon. The url denotes the resource of image.

⮚ Jlabel

∙ The JLabel is a component for placing the label component. The JLabel is a subclass of JComponent class.
The syntax for the JLabel is -
JLabel(Icon ic);

Label(String s);

JLabel(String s, Icon ic, int alignment)


⮚ The icons and text methods associated with the label are:-
Icon getIcon()

void setIcon(Icon ic) 🡪 ic represents the icon file.

String getText();

void setText(String s); 🡪 s represents the string.


⮚ JTextField

∙ The JTextField is extended from the JComponent class. The JTextField allows us to add a single line text.
∙ The syntax of using JTextField is -

JTextField();

JTextField(int col_val);

JTextField(String s,int col_val);

JTextField(String s);
import javax.swing.*;
class TextFieldExample
{

public static void main(String args[])


{ setBounds(50,100,30,20)
JFrame f= new JFrame("TextField Example"); ( x, y, a, b)
JTextField t1,t2;
t1=new JTextField("Welcome to Javatpoint.");
t1.setBounds(50,100, 200,30);
OUTPUT:-
t2=new JTextField("AWT Tutorial"); y
t2.setBounds(50,150, 200,30);
a t1 object
f.add(t1); f.add(t2);
f.setSize(400,400); x ,
b
f.setLayout(null);
f.setVisible(true);
}
}
t2 object
⮚ JButton

∙ The swing push button is denoted by using JButton class.

∙ The swing button class can associate an icon and/or string with the JButton class

∙ The swing button classes are subclasses of AbstractButton class.

∙ The AbstractButton class generates action events when they are pressed.

The syntax of JButton is:-

JButton(Icon ic);

JButton(String s);

JButton(String s,Icon ic);


⮚ Checkbox

∙ The Check Box is also implementation of AbstractButton class. But the immediate superclass of
JCheckBox is JToggleButton.

∙ The JCheckBox supports two states true or false.


We can associate an icon, string or the state with the checkboxes.
The syntax for the Check Box will be :-
JCheckBox(Icon ic);
JCheckBox(Icon ic, boolean state);
JCheckBox(String s);
JCheckBox(String s,boolean state);
JCheckBox(String s,Icon ic,boolea n state);
import javax.swing.*;
public class CheckBoxExample
{
CheckBoxExample()
{
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++"); OUTPUT:-
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckBoxExample();
}
}
⮚ Radio Button

∙ The JRadioButton is a subclass of JToggleButton. This control is similar to the checkboxes.

The syntax for the RadioButton are :-

JRadioButton(Icon ic);

JRadioButton (Icon ic, boolean state);

JRadioButton (String s);

JRadioButton (String s,boolean state);

JRadioButton (String s,Icon ic,boolean state);


import javax.swing.*;
public class RadioButtonExample
{
JFrame f;
RadioButtonExample()
{
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) Female");
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
f.add(r1);f.add(r2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args)
{
new RadioButtonExample();
}
}
• Advanced Swing Component
⮚ Tabbed Panes

∙ Tabbed pane is a type of component in which group of folders can be represented together and particular folder can be select
from the tab.

∙ Each folder has a title and when the user selects the particular folder on the tab then only it will be displayed. One folder can
displayed at a time.

∙ For displaying the tabbed pane there exists a JTabbedPane class which extends the JComponent
class.

∙ Using the addTab method the folders can be added to the tabbed pane. The syntax for this method is
void addTab(String str,Component comp)

The str represents the string which will be displayed as a title to the tab.
The comp is a reference to the component which should be added to the tabbed pane.
∙ Following program illustrates the use of tabbed pane.:-

Step 1 : Create an applet

Step 2 : Create the tabbed pane

Step 3 : Create a contentpane

Step 4 : Place the tabbed pane on the content pane

Step 5 : Define the components of tabbed pane


import javax.swing.*;
public class TabbedPaneExample
{
JFrame f;
TabbedPaneExample()
{
f=new JFrame();
JTextArea ta=new JTextArea(200,200);
JPanel p1=new JPanel();
p1.add(ta);
JPanel p2=new JPanel(); JPanel p3=new JPanel(); OUTPUT:-
JTabbedPane tp=new JTabbedPane();
tp.setBounds(50,50,200,200);
tp.add("main",p1); tp.add("visit",p2); tp.add("help",p3);
f.add(tp);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args)
{
new TabbedPaneExample();
}
}
⮚ScrollPane

The scroll pane is a rectangular areas in which some component can be placed.

The component can be viewed with the help of horizontal and vertical scroll bars.

Using the JScrollPane class the component can be added in the program.

The JScrollPane class extends the JComponent class.


There are three constructors that can be used for this component :-

JScrollPane(Component component)

JScrollPane(int vscrollbar, int hscrollbar)


JScrollPane(Component component, int vscrollbar, int hscrollbar)

The component represents the reference to the component.


The vscrollbar and hscrollbar are the integer values for the vertical and horizontal scroll bars.
These values can be defined by the constants such as:-
Constants
Meaning

HORIZONTAL_SCROLLBAR_ALWAYS 🡪It always displays the horizontal scroll bar.


HORIZONTAL_SCROLLBAR_NEEDED 🡪 It displays the horizontal scrollbar if required
VERTICAL_SCROLLBAR_ALWAYS 🡪It always displays the vertical scroll
bar.
VERTICAL_SCROLLBAR_NEEDED 🡪 It displays the vertical scrollbar if
required.
∙ Following is a simple steps which illustrates the use of scrollpane.

Step 1: Create a label


Step 2: Create a panel
Step 3: Use an image with the help of label
Step 4: Add the label on the panel
Step 5: Create a content pane.

Step 6: Create a scrollpane component by passing the image (within a panel) as a


component to it.
// Java Program for JScrollpane
import java.awt.*;
import javax.swing.*;
/*
<applet code="ScrollPaneDemo" width=150 height=150>
</applet>
*/
public class ScrollPaneDemo extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
OUTPUT:-
JPanel mypanel = new JPanel();

JLabel L1 = new JLabel();


ImageIcon i = new ImageIcon("img.jpg");
L1.setLocation(20, 100);
L1.setSize(120, 120); L1.setIcon(i); mypanel.add(L1);
int vscrollbar = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int hscrollbar = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(mypanel, vscrollbar, hscrollbar);
contentPane.add(jsp, BorderLayout.CENTER);
}
⮚ Trees

∙ Tree is a type of component that gives the hierarchical view of data. User can expand or shrink the nodes of the tree.
∙ In swing the trees are implemented using the JTree class. This class extends the JComponent.

∙ The most commonly used constructor for this class is :-

JTree(TreeNode root)

⮚ The root represents the root node of the tree.

∙ Using the DefaultMutableTreeNode, it creates a tree node with no root node, the child of root node, specified by user
object and it allows only children that have to be specified. It takes boolean types values either 'true' or 'false'. If you
will take 'true' that means children node are allowed.

Step 1: Make use of applet for implementation of the tree program


Step 2: Create a tree with root, child and grand child nodes.
Step 3: Create a content pane object.
Step 4: Add the JTree component on the content pane.
mport javax.swing.tree.*;
/*<applet code="TreeDemo" width=300 height=200> </applet>*/
ublic class TreeDemo extends JApplet
{
public void init()
{
Container contentPane=getContentPane();
OUTPUT:-
contentPane.setLayout(new BorderLayout());
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root", true);
DefaultMutableTreeNode c1 = new DefaultMutableTreeNode("Child 1");
root.add(c1);
DefaultMutableTreeNode c2 = new DefaultMutableTreeNode("Child 2");
root.add(c2);
DefaultMutableTreeNode gc1 = new DefaultMutableTreeNode("GrandChild 1");
DefaultMutableTreeNode gc2 = new DefaultMutableTreeNode("GrandChild 2");
DefaultMutableTreeNode gc3 = new DefaultMutableTreeNode("GrandChild 3");
c2.add(gc1);c2.add(gc2); c2.add(gc3);
DefaultMutableTreeNode c3 = new DefaultMutableTreeNode("Child 3"); root.add(c3);
DefaultMutableTreeNode c4 = new DefaultMutableTreeNode("Child 4"); root.add(c4);
DefaultMutableTreeNode c5 = new DefaultMutableTreeNode("Child 5"); root.add(c5);
JTree tree = new JTree(root); contentPane.add(tree);
}
}
⮚ Tables

∙ Table is a component that arranges the data in rows and columns.

∙ The JTable class extends the JComponent class.

∙ The constructor used for table component is -

JTable( object[][] tablevalues ,object [] columnheader)

The tablevalues indicate the data that can be arranged in tabular fashion. The columnheader denotes
the header for each column.
Following is a simple program that shows the use of JTable component.
import javax.swing.*; import java.awt.*;
import javax.swing.tree.*;
/*<applet code="TableDemo" width=100 height=100></applet> */

public class TableDemo extends JApplet


{
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
final String[] th = { "", "", ""};
final Object[][] mytable = {
OUTPUT:-
{ "8", "1","6"},
{ "3", "5","7"},
{ "4", "9","2"}
};
JTable table = new JTable(mytable,th);

int vscrollbar = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;


int hscrollbar = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

JScrollPane mypane = new JScrollPane(table, vscrollbar, hscrollbar);


contentPane.add(mypane, BorderLayout.CENTER);
}
⮚ Progress Bar

∙ Progress bar is used to display the progress of the task.

∙ It is normally used while creating a GUI for downloading or transferring of file.

In swing we use JProgressBar class for creating the progress bar

JProgressBar() ->It is used to create a horizontal progress bar.

JProgressBar(int min,int max) 🡪It is used to create a progress bar with minimum and maximum value.
import java.awt.*; import javax.swing.*;
public class ProgressBarProg extends JFrame
{
static JFrame frame;
static JProgressBar pbar;
public static void main(String[] args)
{
frame =new JFrame("Progress Bar Demo");
pbar=new JProgressBar(0,2000);
pbar.setBounds(50,50,150,30);
OUTPUT:-
pbar.setValue(0); pbar.setStringPainted(true);frame.add(pbar);
frame.setLayout(null); frame.setSize(300,300); frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fillBar();
}
public static void fillBar() {
int i=0; try {
while(i<=2000) {
pbar.setValue(i);
Thread.sleep(1000); i=i+20;
}
} catch(Exception e) {}
MVC ARCHITECTURE [MODEL VIEW CONTROLLER]
EXAMPLE
MODEL
MODEL VIEW CONTROLLER
⮚ MVC Architecture
The Model View Controller (MVC) design pattern specifies that an application consist of a data model,
presentation information, and control information. The pattern requires that each of these be separated into
different objects

• The Model contains only the pure application data, it contains no logic describing how to present the data to a
user.
• The View presents the model’s data to the user. The view knows how to access the model’s data,
but it does not know what this data means or what the user can do to manipulate it.
The Controller exists between the view and the model.
• It listens to events triggered by the view (or another external source) and executes the appropriate reaction to
these events.

You might also like