Ajp CHP 2
Ajp CHP 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) JButton
Jbuttondemo.java
Swing components
2) JTextField
i)JTextField(int cols)
iii)JTextField(String str)
MyTextField.java
Swing components
3) JCheckBox
JCheckBox class is used to create checkboxes in frame.
Constructors Description
Constructor Description
JRadioButton() Creates an unselected radio
button with no text.
JRadioButtonExample.java
Swing components
5) JComboBox
JComboBox(String arr[])
JComboDemo.java
Swing components
6) JLabel
Constructor Description
JLabel() Creates a JLabel instance with no image
and with an empty string for the title.
JLabelExample.java
Swing components
7) JTextArea
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:
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.
Constructor Description
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
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.