Unit II Swigs
Unit II Swigs
SWINGS
Unit Outcomes (UOs): -
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).
∙ 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
∙ 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 has some title.
∙ Following table enlists various methods of Frame class
⮚Methods in JFrame class:-
void setResizable(boolean resizable) 🡪Sets frame to resizable
void setSize(int width,int height) 🡪Sets the width and height of a frame
∙ Various panes are content pane, glass pane, and root 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
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);
String getText();
∙ 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);
import javax.swing.*;
class TextFieldExample
{
∙ The swing button class can associate an icon and/or string with the JButton class
∙ The AbstractButton class generates action events when they are pressed.
JButton(Icon ic);
JButton(String s);
∙ The Check Box is also implementation of AbstractButton class. But the immediate superclass of
JCheckBox is JToggleButton.
JRadioButton(Icon ic);
∙ 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.:-
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.
∙
There are three constructors that can be used for this component :-
∙
JScrollPane(Component component)
∙ 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.
JTree(TreeNode root)
∙ 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.
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> */
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.