AJP Chapter 2
AJP Chapter 2
SWING
Made by
Mrs. Smita Kuldiwar
Date:- 19/07/2020
• Swing is a part of Java Foundation Classes(JFC).
• It is used to develop GUI or window based
application.
• Swing components are platform independent.
• Swing components are lightweight.
• It is used for creating window based applications.
What is Swing? • Swing support pluggable look and feel.
• Swing provides more powerful components such
as tables, lists, scrollpanes, colorchooser and
tabbedpane, etc.
• Swing follows MVC.
• The javax.swing packages provides classes for
java swing API such as JButton, JTextArea,
JTextField, JRadioButton, JMenu, CheckBox etc.
Swing Features
Light weight :- Swing components are independent of native operating
systems API as swing control 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, table controls etc.
Pluggable Look and Feel:- Swing based GUI applications look and feel can be
changed at run time based on available values.
• Swing provides both additional components and
added functionality to AWT-replacement
components
• Swing components can change their appearance
based on the current "look and feel" library that's
being used.
• You can use the same look and feel as the platform
Advantages of you're on, or use a different look and feel
• Swing components follow the
Swing Model-View-Controller paradigm (MVC), and thus
can provide a much more flexible UI.
•Swing provides "extras" for components, such as:
• Icons on many components
• Decorative borders for components
• Tooltips for components
• Swing components are lightweight (less resource
intensive than AWT)
Difference Between AWT and Swing
AWT SWING
• Platform Dependent • Platform Independent
• Heavyweight • Lightweight
Java Swing
Class
Hierarchy
• All the components in swing like JButton,
JComboBox, JList, JLabel are inherited from
the JComponent class which can be added to
the container classes.
Explanation: • Containers are the windows like frame and
dialog boxes.
• Containers like JFrame and JDialog can only
add a component to itself.
• Any class which has other components in it
is called as a container class. For building
GUI applications at least one container class
is necessary.
Following are the three types of container
Container classes:
• Panel – It is used to organize components
Class on to a window
• Frame – A fully functioning window with
icons and titles
• Dialog – It is like a pop up window but not
fully functional like the frame.
Swing Components
• JButton:-The JButton class is used to create a labeled button that has platform
independent implementation. The application result in some action when the
button is pushed.
• 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.
• JTextField:- The object of a JTextField class is a text component that allows the
editing of a single line text.
• JTextArea:- The object of a JTextArea class is a multi line region that displays text.
It allows the editing of multiple line text.
• JCheckBox:-The JCheckBox class is used to create a checkbox. It is used to turn an
option on (true) or off (false). Clicking on a CheckBox changes its state from "on"
to "off" or from "off" to "on ".
• 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.
Swing Components cont…
• JComboBox :-The object of Choice class is used to show popup menu of choices. Choice
selected by user is shown on the top of a menu.
• JList:-The object of JList class represents a list of text items. The list of text items can be
set up so that the user can choose either one item or multiple items.
• JTable :-The JTable class is used to display data in tabular form. It is composed of rows
and columns.
• JTabbedPane:-The JTabbedPane class is used to switch between a group of components
by clicking on a tab with a given title or icon.
• JProgressBar:- The JProgressBar class is used to display the progress of the task.
• Jtree:- The JTree class is used to display the tree structured data or hierarchical data.
JTree is a complex component. It has a 'root node' at the topmost which is a parent for all
nodes in the tree.
• 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.
• JToolTip:- When the cursor enters the boundary of that component a popup appears,
and text is displayed. We can create a tool tip for any Jcomponent with
setToolTipText() method. This method is used to set up a tool tip for the component.
Link for chapter 1 quiz
• https://quizizz.com/join/quiz/5f108c2a73afd2001defcda8/start?stud
entShare=true
MVC Architecture
What is MVC Architecture in Java?
• Model designs based on MVC architecture follow the MVC design pattern and
they separate the application logic from the user interface when designing
software. As the name implies MVC pattern has three layers, which are:
• Model – Represents the business layer of the application.
• View – Defines the presentation of the application.
• Controller – Manages the flow of the application.
MVC Architecture cont…
• In Java Programming context, the Model consists of simple Java classes, the View
displays the data and the Controller consists of servlets. This separation results in
user requests being processed as follows:
• The browser on the client sends a request for a page to the controller present on
the server.
• The controller performs the action of invoking the model, thereby, retrieving the
data it needs in response to the request.
• The controller then gives the retrieved data to the view.
• The view is rendered and sent back to the client for the browser to display
JFrame
• The javax.swing.JFrame class is a type of container which inherits the
java.awt.Frame class.
• JFrame works like the main window where components like labels, buttons,
textfields are added to create a GUI.
Constructor Description
JFrame() It constructs a new frame that is
initially invisible.
JFrame(GraphicsConfiguration gc) It creates a Frame in the specified
Graphics Configuration of a screen
device and a blank title.
JFrame(String title) It creates a new, initially invisible
Frame with the specified title.
JFrame(String title, It creates a JFrame with the specified
GraphicsConfiguration gc) title and the specified Graphics
Configuration of a screen device.
JFrame Example
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Jpanel;
public class JFrameExample
{
public static void main(String s[])
{
JFrame frame = new JFrame("JFrame Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("JFrame By Example");
JButton button = new JButton();
button.setText("Button");
JFrame Example cont…
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(200, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
JComponent Example
import java.awt.Color;
import java.awt.Graphics; OUTPUT:-
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyJComponent extends JComponent {
public void paint(Graphics g) {
g.setColor(Color.green);
g.fillRect(30, 30, 100, 100);
}
}
JComponent Example cont…
public class JComponentExample {
public static void main(String[] arguments) {
MyJComponent com = new MyJComponent();
// create a basic JFrame
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("JComponent Example");
frame.setSize(300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add the JComponent to main frame
frame.add(com);
frame.setVisible(true);
}
}
JButton Example
import javax.swing.*; OUTPUT
public class example{
public static void main(String args[]) {
JFrame f = new JFrame("example");
JButton b = new JButton("click me");
b.setBounds(40,90,85,20);
f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
Jlabel:- Common constructors and methods
Constructor Description Methods Description
Constructor Description
JTextArea(int row, Creates a text area with void setFont(Font f) It is used to set the
int column) the specified number of specified font.
rows and columns that void insert(String s, It is used to insert
displays no text initially. int position) the specified text on
the specified
JTextArea(String s, Creates a text area with position.
int row, int column) the specified number of
rows and columns that void append(String s) It is used to append
displays specified text. the given text to the
end of the document.
JTextArea Example
import javax.swing.*;
public class TextAreaExample
{ OUTPUT:-
TextAreaExample(){
Frame f= new JFrame();
JTextArea area=new JTextArea("Welcome to javatpoint");
area.setBounds(10,30, 200,200);
f.add(area);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaExample();
}
}
JCheckBox
• Commonly used Constructors:
Constructor Description
Constructor Description
Constructor Description
Constructor Purpose