KEMBAR78
UNIT 2 SWIGS for java programing by .pptx
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
2. 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.
ii. 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[])
{
JFrame f= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField("Welcome to Javatpoint.");
t1.setBounds(50,100, 200,30);
OUTPUT:-
t2=new JTextField("AWT Tutorial");
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
t1 object
t2 object
setBounds(50,100,30,20)
( x, y, a, b)
x
y
a
,
b
⮚ 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.

UNIT 2 SWIGS for java programing by .pptx

  • 1.
    UNIT – II SWINGS UnitOutcomes (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
  • 2.
    Qno1: State thedifference 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.
  • 3.
    ⮚ Introduction toSwing āˆ™ 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 2. Additional Features āˆ™ Swing contains wide variety of new components such as tables, trees, sliders, progress bars and so on.
  • 4.
    ⮚ Limitations ofAWT 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.
  • 6.
    ⮚ JFrame āˆ™ InJava, 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. ii. JFrame (String title) This creates the new instance of frame which has some title. āˆ™ Following table enlists various methods of Frame class
  • 7.
    ⮚ Methods inJFrame 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. 🔪
  • 8.
    ⮚ JApplet āˆ™ TheJApplet 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.
  • 9.
    ⮚ Icons āˆ™ Theicons 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)
  • 10.
    ⮚ The iconsand 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.
  • 11.
    ⮚ JTextField āˆ™ TheJTextField 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);
  • 12.
    import javax.swing.*; class TextFieldExample { publicstatic void main(String args[]) { JFrame f= new JFrame("TextField Example"); JTextField t1,t2; t1=new JTextField("Welcome to Javatpoint."); t1.setBounds(50,100, 200,30); OUTPUT:- t2=new JTextField("AWT Tutorial"); t2.setBounds(50,150, 200,30); f.add(t1); f.add(t2); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } } t1 object t2 object setBounds(50,100,30,20) ( x, y, a, b) x y a , b
  • 13.
    ⮚ JButton āˆ™ Theswing 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);
  • 14.
    ⮚ Checkbox āˆ™ TheCheck 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);
  • 15.
    import javax.swing.*; public classCheckBoxExample { 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(); } }
  • 16.
    ⮚ 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);
  • 17.
    import javax.swing.*; public classRadioButtonExample { 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(); } }
  • 18.
    • Advanced SwingComponent ⮚ 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.
  • 19.
    āˆ™ Following programillustrates 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
  • 20.
    import javax.swing.*; public classTabbedPaneExample { 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(); } }
  • 21.
    ⮚ScrollPane āˆ™The scroll paneis 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:-
  • 22.
    Constants Meaning HORIZONTAL_SCROLLBAR_ALWAYS It alwaysdisplays 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.
  • 23.
    // Java Programfor 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); }
  • 24.
    ⮚ Trees āˆ™ Treeis 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.
  • 25.
    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); } }
  • 26.
    ⮚ Tables āˆ™ Tableis 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.
  • 27.
    import javax.swing.*; importjava.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); }
  • 28.
    ⮚ 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.
  • 29.
    import java.awt.*; importjavax.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) {}
  • 30.
    MVC ARCHITECTURE [MODELVIEW CONTROLLER]
  • 31.
  • 33.
  • 35.
  • 40.
    ⮚ MVC Architecture TheModel 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.