0% found this document useful (0 votes)
16 views22 pages

Ajp CHP 2

Uploaded by

rahul13237
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)
16 views22 pages

Ajp CHP 2

Uploaded by

rahul13237
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/ 22

Unit 2

Swings
Outcomes
1) Differentiate between AWT and Swing on the given aspect
2) Develop GUI programs using swing components for the
given problem
3) Use the given type of button in Java based GUI
4) Develop GUI programs using advanced swing components
for the given problem
Swings in Java
❑ Java Swing is a part of Java Foundation Classes (JFC) that
is used to create window-based applications. It is built on the
top of AWT (Abstract Windowing Toolkit) API and entirely
written in java.
❑ Unlike AWT, Java Swing provides platform-independent and
lightweight components.
❑ The javax.swing package provides classes for java swing API
such as JButton, JTextField, JTextArea, JRadioButton,
JCheckbox, Jmenu etc.
Swing Features
1. Light Weight - Swing component 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.
2. Rich controls - Swing provides a rich set of advanced
controls like Tree, TabbedPane, slider, colorpicker, table
controls etc.
3.Highly Customizable - Swing controls can be
customized in very easy way as visual apperance is
independent of internal representation.
4.Pluggable look-and-feel- SWING based GUI
Application look and feel can be changed at run time
based on available values.
Difference between AWT and Swing
AWT SWING

1) AWT components Java swing components


are platform-dependent. are platform-independent.
2) AWT components Swing components
are heavyweight. are lightweight.
3) AWT doesn't support Swing supports pluggable
pluggable look and feel. look and feel.
4) AWT provides less Swing provides more powerful
components than Swing. components such as tables,
lists, scrollpanes, colorchooser,
tabbedpane etc.
5) AWT doesn't follows Swing follows MVC.
MVC(Model View Controller)
where model represents data,
view represents presentation
and controller acts as an
interface between model and
Swing Components

1) JButton

JButton class provides functionality of a button. JButton


class has three constuctors.
i) JButton(Icon ic)
ii) JButton(String str)
iii) JButton(String str, Icon ic)

It allows a button to be created using icon, a string or both.


JButton supports ActionEvent. When a button is pressed
an ActionEvent is generated.

Jbuttondemo.java
Swing components

2) JTextField

It is used for taking input of single line of text. It is most


widely used text component. It has three constructors,

i)JTextField(int cols)

ii)JTextField(String str, int cols)

iii)JTextField(String str)

cols represent the number of columns in text field.

MyTextField.java
Swing components
3) JCheckBox
JCheckBox class is used to create checkboxes in frame.
Constructors Description

JCheckBox() Creates an initially unselected checkbox


button with no text and no icon.
JCheckBox(Icon icon) Constructs an initially unselected check box
with an icon.
JCheckBox(Icon icon, Constructs check box with icon and it
boolean state) specifies whether it was initially selected or
not.
JCheckBox(String str) Creates an initially unselected check box with
text.
JCheckBox(String str, Creates the check box with text and it
boolean state) specifies whether it is initially selected or not.
JCheckBox(String str, Icon Creates an initially unselected check box with
icon) specified text and icon.
JCheckBox(String str, Icon Create a check box with specified text and
icon, boolean state) icon and specifies state
Jcheckboxdemo.java
Swing components
4) JRadioButton
Radio button is a group of related button in which only
one can be selected. JRadioButton class is used to create
a radio button in Frames.

Constructor Description
JRadioButton() Creates an unselected radio
button with no text.

JRadioButton(String s) Creates an unselected radio


button with specified text.

JRadioButton(String s,boolean Creates a radio button with


selected) the specified text and selected
status.

JRadioButtonExample.java
Swing components
5) JComboBox

Combo box is a combination of text fields and drop-down


list.
JComboBox component is used to create a combo box in
Swing.

Following is the constructor for JComboBox,

JComboBox(String arr[])

JComboDemo.java
Swing components
6) JLabel

• The object of JLabel class is a component for


placing text in a container.
• It is used to display a single line of read only text.
• The text can be changed by an application but a
user cannot edit it directly.
• It inherits JComponent class.
JLabel Constructors

Constructor Description
JLabel() Creates a JLabel instance with no image
and with an empty string for the title.

JLabel(String s) Creates a JLabel instance with the


specified text.
JLabel(Icon i) Creates a JLabel instance with the
specified image.
JLabel(String s, Icon i, int Creates a JLabel instance with the
horizontalAlignment) specified text, image, and horizontal
alignment.

JLabelExample.java
Swing components
7) JTextArea

• The object of a JTextArea class is a multi line


region that displays text.
• It allows the editing of multiple line text. It inherits
JTextComponent class
JTextArea Constructors

Constructor Description
JTextArea() Creates a text area that displays no text
initially.
JTextArea(String s) Creates a text area that displays
specified text initially.
JTextArea(int row, int column) Creates a text area with the specified
number of rows and columns that
displays no text initially.

JTextArea(String s, int row, int Creates a text area with the specified
column) number of rows and columns that
displays specified text.

JTextAreaExample.java
Swing components
8) JProgressBar
The class JProgressBar is a component which visually
displays the progress of some task.
Constructors :
1) JProgressBar() Creates a horizontal progress bar that
displays a border but no progress string.
2) JProgressBar(int orient) Creates a progress bar with the
specified orientation, which can be either SwingConstants.
VERTICAL or SwingConstants.HORIZONTAL.
3) JProgressBar(int min, int max)Creates a horizontal
progress bar with the specified minimum and maximum.
4) JProgressBar(int orient, int min, int max)Creates a
progress bar using the specified orientation, minimum, and
maximum.

Jprogressbar.java
9) ToolTips
❑ Creating a tool tip for any JComponent is easy. You just
use the setToolTipText method to set up a tool tip for the
component.
❑ For example, to add tool tips to three buttons, you add
only three lines of code:

b1.setToolTipText("Click this button to disable the middle


button.");
b2.setToolTipText("This middle button does nothing when
you click it.");
b3.setToolTipText("Click this button to enable the middle
button.");

ToolTipExample.java
10) Seperator
❑ The JSeparator class provides a horizontal or vertical
dividing line or empty space. It's most commonly used in
menus and tool bars.
❑ We can use separators without even knowing that a
JSeparator class exists, since menus and tool bars provide
convenience methods that create and add separators
customized for their containers.
❑ Separators are somewhat similar to borders, except that they
are the components which are drawn inside a container,
rather than around the edges of a particular component.
Here is a picture of a menu that has three separators, used to
divide the menu into four groups of items:

SeparatorDemo.java
11) JTable
❑ The JTable class is used to display data in tabular
form. It is composed of rows and columns.
❑ Constructors of JTable

1) JTable()
Creates a table with empty cells.

2) JTable(Object[][] rows, Object[]columns)


Creates a table with the specified data.
JTableExample.java
12) JTrees
❑ JTree is a Swing component with which we can display
hierarchical data.
❑ A JTree has a 'root node' which is the top-most parent for all
nodes in the tree. A node is an item in a tree.
❑ A node can have many children nodes. These children nodes
themselves can have further children nodes. If a node doesn't
have any children node, it is called a leaf node.
❑ The node is represented in Swing API as TreeNode which is
an interface. The interface MutableTreeNode extends this
interface which represents a mutable node.
❑ Swing API provides an implementation of this interface in the
form of DefaultMutableTreeNode class.
❑ DefaultMutableTreeNode class is used to represent node. This
class is provided in the Swing API and we can use it to
represent our nodes.
❑ This class has a add() method which takes in an instance of
MutableTreeNode.
Jtree Constructors

Constructor Description

1) JTree() Creates a JTree with a sample


model.

2) JTree(Object[] value) Creates a JTree with every element


of the specified array as the child of
a new root node.

3) JTree(TreeNode root) Creates a JTree with the specified


TreeNode as its root, which displays
the root node.

JTreeExample.java
13) JTabbedPane
The JTabbedPane class is used to switch between a group of
components by clicking on a tab with a given title or icon. It
inherits JComponent class.

Constructor Description
JTabbedPane() Creates an empty TabbedPane
with a default tab placement of
JTabbedPane.Top.
JTabbedPane(int Creates an empty TabbedPane
tabPlacement) with a specified tab placement.
JTabbedPane(int Creates an empty TabbedPane
tabPlacement, int with a specified tab placement
tabLayoutPolicy) and tab layout policy.
14) JScrollPane
A JscrollPane is used to make scrollable view of a
component. When screen size is limited, we use a scroll pane
to display a large component or a component whose size can
change dynamically.

Constructor Purpose

JScrollPane() It creates a scroll pane. The


Component parameter, when
JScrollPane(Component)
present, sets the scroll pane's
JScrollPane(int, int) client. The two int parameters,
when present, set the vertical
JScrollPane(Component, int,
and horizontal scroll bar
int)
policies (respectively).

Scrollpanedemo.java
MVC Architecture
▪ MVC stands for Model View and Controller. It is
a design pattern that separates the business logic,
presentation logic and data.
▪ Model represents the state of the application i.e.
data. It can also have business logic.
▪ View represents the presentation i.e. UI(User
Interface).
▪ Controller acts as an interface between View and
Model. Controller intercepts all the incoming
requests.

You might also like