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

Ajp 2 PPT

ajp

Uploaded by

towaxa1167
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)
6 views

Ajp 2 PPT

ajp

Uploaded by

towaxa1167
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/ 77

Swings

By
Sangita B.Chavan
Lecturer in Computer Technology
Govt. Polytechnic,Ahmednagar
Learning Objective
 Differentiate between AWT and Swing
on the given aspect.
Introduction of JFC (Swing):

 JDK 1.2 was introduced with a new set of


packages-the Java Foundation Classes,orJFC-that
includes an improved user interface called the
Swing components.
 Swing components facilitate efficient graphical
user interface(GUI)development.
 These components are a collection of lightweight
visual components.
 Swing components contain a replacement for the
heavyweight AWT components as well as complex
user-interface components such as trees and
tables.
Introduction of Swing
 Java Swing is a GUI Framework that contains a set of
classes to provide more powerful and flexible GUI
components than AWT.
 Swing provides the look and feel of modern Java
GUI.
 Swing library is an official Java GUI tool kit released
by Sun Microsystems.
 It is used to create graphical user interface with Java.
 Swing components allow mixing AWT heavyweight
and swing lightweight components in an application.
 The major difference between lightweight and
heavyweight components is that lightweight
components can have transparent pixels while
heavyweight components are always opaque.
 Lightweight components can be non-
rectangular while heavyweight components
can be rectangular.
 The main package used to develop swing
applications in javax.swing ,but the
java.awt is also included in import
statements because swing depend on the
common framework shared with the AWT
components.
 Class names that start with ‘J’ are the
components that are added to an application.
 E.g: JLabel,Jbutton.
Swing Features
 Platform Independent − Swing components are
independent of native Operating System's API as
Swing API controls are rendered mostly using pure
JAVA code instead of underlying operating system
calls.
 Rich Controls − Swing provides a rich set of
advanced controls like Tree, TabbedPane, slider,
colorpicker, and table controls.
 Highly Customizable − Swing controls can be
customized in a very easy way as visual apperance is
independent of internal representation.
 Pluggable look-and-feel − SWING based GUI
Application look and feel can be changed at run-
time, based on available values.
Swing look & feel AWT look & feel
Difference between AWT and Swing
Summary

 Differentiate between AWT and Swing


on the given aspect.
Swings

By
Sangita B.Chavan
Lecturer in Computer Technology
Govt. Polytechnic,Ahmednagar
Learning Objective
 Develop GUI programs using swing
components for the given problem.
Swing Components:

 A swing GUI contains two main elements:


components and containers.
 A component is an individual control
like a button or label.
 A container is an object which can hold
other components and containers.
 Swing components are derived from the
class JComponent (except the four top-
level containers).
 JComponent supports pluggable look and
feel and supports the functionality common
for all the components.
 JComponent class inherits the AWT
classes Container and Component.
 All swing components are represented as
classes and are present
in javax.swing package.
 Some of the components available in the
Swings package are listed below:
Hierarchy of the Heavyweight Classes:-
JFrame:-

 The JFrame class implements a top-level


container.
 It is a direct subclass of the Frame class
and is used to create a swing application.
 A JFrame can automatically close when
the close button of title bar is pressed,but
to make this action cause the application
to exit,WindowListener should be
used.
 Ex-jframe.java
JApplet:-
 JApplet is the JFC equivalent of the
AWT Applet class.
 It uses the same methods as AWT
applet,because it is a direct subclass of
the Applet class.
 JApplet also allows you to add a menu bar
by using the setJMenuBar() method of its
JRootPane.
 Ex-japplet.java
JDialog:-
 A dialog window is used to get input from the
user to simply inform the user of an error.
 JDialog has the following constructors:
JDialog(Frame owner): It is used to create a
modeless dialog with specified Frame as its
owner and an empty title.
JDialog(Frame owner, String title, boolean
modal): It is used to create a dialog with the
specified title, owner Frame and modality.
 A dialog cannot appear before its parent frame.
 Ex-jdialog.java
Basic swing components:-
 Icon:-
 Icon is an interface which is implemented by the
ImageIcon class.
 The ImageIcon class defines various constructors
that can be used to create icon-images.
ImageIcon():Creates an uninitialized image icon.

ImageIcon(String filename) :Creates an


ImageIcon from the specified file.

ImageIcon(Image image):Creates an ImageIcon


from an image object.
ImageIcon(byte[ ] imagedata):Creates an
ImageIcon from an array of bytes which
were read from an image file containing a
supported image format, such as GIF, JPEG,
or (as of 1.3) PNG.

 It defines methods like,


 getIconHeight(): Gets the height of the
icon.
 ,getIconWidth(): Gets the width of the
icon. etc.to work with icon-images.
 Image getImage():Returns this icon's
Image.
Summary
 Develop GUI programs using swing
components for the given problem.
Swings

By
Sangita B.Chavan
Lecturer in Computer Technology
Govt. Polytechnic,Ahmednagar
Learning Objective
 Develop GUI programs using swing
components for the given problem.
JLabel:-
 Label are text string that can be used to label other UI
components.labels are passive controls.
 To create labels,:-
JLabel():Creates a JLabel instance with no image and with an
empty string for the title.
JLabel(String text) :Creates a JLabel instance with the
specified text.
JLabel(String text,int horizontalAlignment):Creates a
JLabel instance with the specified text, and horizontal
alignment.
JLabel(String text,Icon icon,int
horizontalAlignment):Creates a JLabel instance with the
specified text, image, and horizontal alignment.
JLabel(Icon icon) Creates a JLabel instance with the
specified image.
Here,icon is the image to be displayed by the label
 The horizontal alignments are stored as constants
defined in Swing constants:
LEFT,CENTER,RIGHT,LEADING or TRAILING.
JLabel label=new
JLabel(“text”,SwingConstants.LEFT);
or
JLabel label=new
JLabel(“text”,JLabel.LEFT);
Commonly used Methods:
Methods Description

String getText() t returns the text string that a


label displays.

void setText(String text) It defines the single line of text


this component will display.

void It sets the alignment of the


setHorizontalAlignment(int label's contents along the X
alignment) axis.

Icon getIcon() It returns the graphic image


that the label displays.

int getHorizontalAlignment() It returns the alignment of the


label's contents along the X
axis.
JTextField:-
 A JTextField is a subclass of the JTextComponent class.
 It can hold a single row of text and its width is either set to
accommodate a string used in the constructor or set by int
parameter specifying the number of columns.
JTextField():Creates a new TextField
JTextField(int column):Creates a new empty TextField
with the specified number of columns.
JTextField(String s):Creates a new TextField initialized with
the specified text.
JTextField(string s,int column):Creates a new TextField
initialized with the specified text and columns.
 Commonly used Methods:
void setFont(Font f):It is used to set the current font.
void setEditable(boolean b):to make editable or read
only,works like a password field etc.
 Ex-txtlbl.java,jtextfield.java
JTextArea:-
 It is a multiline input area that large amount of text
can be entered and displayed.
 To create JTextArea:
 JTextArea():Creates a text area that displays no
text initially.
 JTextArea(String s):Creates a text area that
displays specified text initially.
 JTextArea(int rows,int cols):Creates a text area
with the specified number of rows and columns that
displays no text initially.
 JTextArea(String s,int rows,int cols):Creates a
text area with the specified number of rows and
columns that displays specified text.
Commonly used Methods:

Methods Description

void setRows(int rows) It is used to set specified number


of rows.

void setColumns(int cols) It is used to set specified number


of columns.

void setFont(Font f) It is used to set the specified font.

void insert(String s, int It is used to insert the specified


position) text on the specified position.

void append(String s) It is used to append the given text


to the end of the document.
JScrollBar:-
 JScrollBar are used to select continuous values
between a specified minimum and maximum.
 JScrollBars may be oriented horizontally or
vertically.
 The constructors are as follows:
JScrollBar ():Creates a vertical scrollbar with the
initial values.
JScrollBar(int orientation):Creates a scrollbar
with the specified orientation and the initial values.
JScrollBar (int orientation,int initial,int
thumbsize,int min,int max):Creates a scrollbar
with the specified orientation, initial value,
thumbsize, minimum, and maximum.
JComboBox:-
 JComboBox is a component that combines a
button or editable field and a drop-down list.
 The user can select a value from the drop-down
list,which appears at the user’s request.
 The following constructors are supported by this
class:
public JComboBox():Creates a JComboBox
with a default data model.
public JComboBox(object[ ] items)
:Creates a JComboBox that contains the
elements in the specified Array.
public JComboBox(Vector items):Creates a
JComboBox that contains the elements in the
specified Vector.
Commonly used Methods:
Methods Description

void addItem(Object It is used to add an item to the


anObject) item list.

void removeItem(Object It is used to delete an item to the


anObject) item list.
void removeAllItems() It is used to remove all the items
from the list.
void setEditable(boolean It is used to determine whether the
b) JComboBox is editable.

public void sets the maximum number of rows


setMaximumRowCount(int the JComboBox displays. if number
count): of object in the model is greater
than count,combo box uses a
scrollbar.
Summary
 Develop GUI programs using swing
components for the given problem.
Swings

By
Sangita B.Chavan
Lecturer in Computer Technology
Govt. Polytechnic,Ahmednagar
Learning Objective
 Use the given type of button in Java based
GUI.
Buttons:-
 Swing button are subclasses of the AbstractButton class.
 AbstractButton is an abstract subclass of JComponent.
 It includes JButton,JToggleButton such as JCheckBox
,JRadioButton
 JButton:-
 The JButton class is used to create a labeled button that has
platform independent implementation. (i.e The JButton class
creates a push button that generates an event when it is
pressed.)
 It inherits AbstractButton class.
 Commonly used Constructors:
 JButton():It creates a button with no text and icon.
 JButton(String s):It creates a button with the specified
text.
 JButton(Icon i):It creates a button with the specified icon
object.
•Jbutton(String s,Icon i): It creates a button with
the specified text and icon.

Commonly used Methods of AbstractButton class:


Methods Description

void setText(String s) It is used to set specified text on


button

String getText() It is used to return the text of


the button.

void setEnabled(boolean b) It is used to enable or disable the


button.

void setIcon(Icon b) It is used to set the specified


Icon on the button.

Icon getIcon() It is used to get the Icon of the


button.
JCheckBox:-
 The JCheckBox class is used to create a checkbox.
 JCheckBoxes are user interface component that
have two states:checked and unchecked(true or
false)which display its state to the user.
 JCheckBox class has the following constructors:
JCheckBox(): Creates an initially unselected
checkbox button with no text and no icon.
JCheckBox(String s): Creates an initially
unselected check box with text.
JCheckBox(String s,boolean state): Creates
the check box with text and it specifies whether it
is initially selected or not.
JCheckBox(Icon i):Constructs an initially
unselected check box with an icon.
JCheckBox(Icon i,boolean state)
:Constructs check box with icon and it
specifies whether it was initially selected
or not.
JCheckBox(String s,Icon i) Creates an
initially unselected check box with
specified text and icon.
JCheckBox(String s,Icon I,boolean
state):Create a check box with specified
text and icon and specifies that whether it
is initially selected or not.
JRadioButton:-
 The JRadioButton class is used to create a radio button.
It is used to choose one option from multiple options. It
is widely used in exam systems or quiz.
 It should be added in ButtonGroup to select one radio
button only.
 JRadioButton class has the following constructors:
JRadioButton() :Creates an unselected radio button
with no text.
JRadioButton(String s):Creates an unselected radio
button with specified text.
JRadioButton(String s,boolean state):Creates a
radio button with the specified text and selected status.
JRadioButton(Icon i):Creates an initially unselected
radio button with the specified icon.
JRadioButton(Icon i,boolean state)
:Creates an initially selected button with
specified image.
JRadioButton(String s,Icon i):Creates a
radion button with specified string and
specified image that is initially unselected.
JRadioButton(String s,Icon I,boolean
state):Creates a radio button that has
specified text, image and selection state.
Commonly used Methods:
Methods Description

void setText(String s) It is used to set specified text on


button.

String getText() It is used to return the text of the


button.

void setEnabled(boolean b) It is used to enable or disable the


button.

void setIcon(Icon b) It is used to set the specified Icon


on the button.

Icon getIcon() It is used to get the Icon of the


button.

void setMnemonic(int a) It is used to set the mnemonic on


the button.
Summary
 Use the given type of button in Java based
GUI.
Swings

By
Sangita B.Chavan
Lecturer in Computer Technology
Govt. Polytechnic,Ahmednagar
Learning Objective
 Develop Graphical user interface(GUI)
programs using advanced swing
components for the given problem.
JTabbedPane:-
 The JTabbedPane class is used to switch between a group
of components by clicking on a tab with a given title or
icon.
 Tab/components are added to a TabbedPane object
by using the addTab() and insertTab() methods .
 A tab is represented by an index corresponding to
the position it was added in,where the first tab has
an index equal to 0,and the last tab an index equal
to the tab count minus 1.
 The following constructors are supported by this
class:
JTabbedPane():Creates an empty TabbedPane
with a default tab placement of JTabbedPane.Top.
JTabbedPane(int tabplacement):Creates an
empty TabbedPane with a specified tab placement.
JTabbedPane(int tabplacement,int
tablayoutpolicy):Creates an empty TabbedPane
with a specified tab placement and tab layout policy.
 Tab placement is the placement for the tabs relative to
the content and may be either
JTabbedPane.TOP(default),
JTabbedPane.BOTTOM, JTabbedPane.LEFT or
JTabbedPane.RIGHT.
 Tab layout policy is the policy for laying out tabs when
all tabs will not fit on one run and may be either:
JTabbedPane.WRAP_TAB_LAYOUT(default) or
JTabbedPane.SCROLL_TAB_LAYOUT.
 Many methods are supported by JTabbedPane.
1)public void insertTab(string title,Icon
icon,Component component,String tip,int
index):Inserts a new tab for the given component, at
the given index, represented by the given title and/or
icon, either of which may be null.
2)public void addTab(String title,Icon
icon,Component component,String tip):Adds
a component and tip represented by a title and/or icon,
either of which can be null.
OR
3)public Component add(String title,
Component component):Adds
a component with the specified tab title.
4)public int getTabCount():Returns the number of
tabs in this tabbedpane.
5)public String getTitleAt(int index):Returns
the tab title at index.

 Ex-jtabbedpane.java
JScrollPane:-
 JScrollPane is used to give a scrollable view to your
component. When the screen size is small or limited, we can
use a scroll pane to showcase a large component or a
component whose size changes dynamically.
 JScrollPane basically consists of JScrollBars and
JViewport.In addition ,a JScrollPane can have a column
header and row header.
 JViewport object that you specify with
setRowHeaderView() and setColumnHeaderView()
methods
 The column header viewport automatically scrolls left and
right.the row header acts in a similar fashion.
 The JViewport class manages components that have to be
scrolled.
 The JScrollPane class manages a single viewport in the centre
of its area
 Some of its constructor are:
JScrollPane(): Creates an empty scroll pane
(no viewPort).
JScrollPane(Component comp): Creates a
scroll pane with the specified component. When
the component content is larger than the view,
then horizontal and vertical scrollbar appears.
JScrollPane(int vsb,int hsb): Creates a scroll
pane with the specified scroll policies.
JScrollPane(Component comp,int vsb,int
hsb) Creates a scroll pane with the specified
component and specified scroll policies.
 vsb and hsb are int constants that defines when
vertical and horizontal scrollpane . These
constants are defined by the ScrollPaneConstants
interface.
 Some examples of these constants are:
 HORIZONTAL_SCROLLBAR_ALWAYS: Always
provide horizontal scrollbar
 HORIZONTAL_SCROLLBAR_AS_NEEDED:
provide horizontal scrollbar,if needed.
 VERTICAL_SCROLLBAR_ALWAYS: Always
provide vertical scrollbar
 VERTICAL_SCROLLBAR_AS_NEEDED: provide
vertical scrollbar,if needed.
 To create and set a viewport,if necessary,to used:
public void setViewportView(Component view)

Ex-jscrollpane.java
Modifier Method Description

void setColumnHeaderView( It sets the column header for


Component) the scroll pane.

void setRowHeaderView(Co It sets the row header for the


mponent) scroll pane.

void setCorner(String, It sets or gets the specified corner.


Component) The int parameter specifies which
corner and must be one of the
following constants defined in
Component getCorner(String) ScrollPaneConstants:
UPPER_LEFT_CORNER,
UPPER_RIGHT_CORNER,
LOWER_LEFT_CORNER,
LOWER_RIGHT_CORNER,
LOWER_LEADING_CORNER,
LOWER_TRAILING_CORNER,
UPPER_LEADING_CORNER,
UPPER_TRAILING_CORNER.

void setViewportView(Co Set the scroll pane's client.


mponent)
Tool Tips
 Tooltips are small windows of text that popup
when the user leaves the mouse cursor over
a component for a second or two.
 They are used to explain the functionality of
the component.
 Tooltips are not supported in AWT.
 You can create a tool tip for any Jcomponent
with setToolTipText() method. This method
is used to set up a tool tip for the
component.
 For example, to add tool tip to TextField,
you need to add only one line of code:

JTextField field=new JTextField();


field.setToolTipText("Enter your Passw
ord");
 When the cursor enters the boundary of
that component a popup appears and text is
displayed .
 Methods used:
 getToolTipText() : returns the tooltip text
for that component .
 setToolTipText(String s) : sets the tooltip
text for the component .
Summary
 Develop Graphical user interface(GUI)
programs using advanced swing
components for the given problem.
Swings

By
Sangita B.Chavan
Lecturer in Computer Technology
Govt. Polytechnic,Ahmednagar
Learning Objective
 Develop Graphical user interface(GUI)
programs using advanced swing
components for the given problem.
JTree:-
 A tree is a component that presents a hierarchical view of
data.
 It has a 'root node' at the top most which is a parent for all
nodes in the tree.
 A user has the ability to expand or collapse individual
subtrees in this display.
 An expanded node is one that displays its children.
 A collapsed node is one that hides them.
 Some of its constructors are shown :
JTree(): It creates a simple model for class JTree.
JTree(Object obj[ ]): Creates a JTree with every element
of the specified array as the child of a new root node.
JTree(TreeNode tn) Creates a JTree with the specified
TreeNode as its root, which displays the root node.
 The MutableTreeNode interface extends
TreeNode.it declares methods that can insert and
remove child nodes or change the parent node.
 The DefaultMutableTreeNode class implements the
MutableTreeNode interface.

 DefaultMutableTreeNode():Creates a tree node that


has no parent and no children,but which allows children.
 DefaultMutableTreeNode(Object obj):Creates a
tree node that has no parent and no children,but which
allows children,and initializes it with the specified user
object.
 To create a hierarchy of tree nodes ,the add() method
of DefaultMutableTreeNode can be used.
void add(MutableTreeNode child)
 The getPathForLocation() method is used to
translate a mouse click on a specific point of the tree to
a tree path.
TreePath getPathForLocation(int x,int y)
 Return value is a TreePath object that encapsulates
information about the tree node that was selected by
user.
 A JTree object generates events when a node is
expanded or collapsed.
 The addTreeExpansionListener() and
removeTreeExpansionListener() methods allow
listeners to register and unregister for these
notifications.
void
addTreeExpansionListener(TreeExpansionListen
er tel)
void
removeTreeExpansionListener(TreeExpansionLi
stener tel)
 Here ,tel is the listener object.
 Tree expansion events are described by the class
TreeExpansionEvent in the javax.swing.event package.
 The getPath() method of this class returns a TreePath object
that describes the path to the changed node.
TreePath getPath()
 The TreeExpansionListener interface provides two methods:
void treeCollapsed(TreeExpansionEvent tee)
void treeExpanded(TreeExpansionEvent tee)
 The first method is called when a subtree is hidden,and second
method is called when a subtree becomes visible.
 public int getChildCount():Returns the number of children of
this node.

 Ex-jtreeevent.java
jtree.java
JTable:-
 A table is a component that displays rows and columns of
data.
 Tables are implemented by the JTable class,which extends
JComponent.
 The JTable is used to display and edit regular two-dimensional
tables of cells.
 The following constructors can be used to create a JTable
object:
JTable():Creates a table with empty cells.

JTable(int numrows,int numcolumns): Constructs a


JTable with numrows and numcolumns of empty cells.

JTable(object[ ][ ]rowdata,object[ ]columnNames):


Constructs a JTable to display the values in the two-
dimensional array,row data,with column names,.All rows
must be of the same length as columnname.
 The following method are supported by this class:
 public void setGridColor(Color gridcolor):Sets the
color used to draw gridlines to gridColor and redisplays.the
default color is look-and-feel dependent.
 public void setRowHeight(int row,int rowHeight):Set
the height for row to rowheight.
 public int getSelectedColumn():Returns the index of
the first selected column,-1 if no column is selected.
 public int getSelectedRowCount(): Return the number
of selected rows.
 public int getRowCount():Return the number of rows in
this table model.
 public int getColumnCount():Return the number of
column in this table model.
 public String getColumnName(int
column):Returns the name of the column
appearing in the view at column position.
 public void getValueAt(int row,int
column) :Return the cell value at row and
column.
 public void setValueAt(object
value,int row,int column):set the cell
value at row and column.
Ex-jtable.java
JprogressBar:-
 The JProgressBar class is used to display
the progress of the task.
 Commonly used Constructors:
JProgressBar():It is used to create a
horizontal progress bar but no string text.
JProgressBar(int min, int max):It is
used to create a horizontal progress bar
with the specified minimum and maximum
value.
 JProgressBar(int orient):It is used to
create a progress bar with the specified
orientation, it can be either Vertical or
Horizontal by using
SwingConstants.VERTICAL and
SwingConstants.HORIZONTAL
constants.
 JProgressBar(int orient, int min, int
max):It is used to create a progress bar
with the specified orientation, minimum
and maximum value.
Commonly used Methods:
Method Description

void setStringPainted(boolean b) It is used to determine whether


string should be displayed.

void setString(String s) It is used to set value to the


progress string.

void setOrientation(int It is used to set the orientation,


orientation) it may be either vertical or
horizontal by using
SwingConstants.VERTICAL and
SwingConstants.HORIZONTAL
constants.

void setValue(int value) It is used to set the current


value on the progress bar.
Summary
 Develop Graphical user interface(GUI)
programs using advanced swing
components for the given problem.
Swings

By
Sangita B.Chavan
Lecturer in Computer Technology
Govt. Polytechnic,Ahmednagar
Learning Objective
 Explain the Architecture of MVC.
MVC:
 Swing architecture is rooted in the model-
view-controller (MVC).
 MVC architecture calls for a visual
application to be broken up into three
separate parts:
◦ A model that represents the data for the
application
◦ The view that is the visual representation of
that data
◦ A controller that takes user input on the
view and translates that to changes in the
model.”
MVC Architecture
 Software design pattern for software development.
 Model:
 Major function of this layer to maintain the data.
 Database and logic.
 View:
 Used to display full or partial data.
 User Interface
 Controller:
 Control the interaction and communication
between Model and view.
 Communication logic/integration logic
For example :Scrollbar
Summary
 Explain the Architecture of MVC

You might also like