Module 5
GUI PROGRAMMING AND APPLETS
Java AWT (Abstract Window Toolkit)
• Java AWT (Abstract Window Toolkit) is an API
to develop Graphical User Interface (GUI) or
windows-based applications in Java.
• Java AWT components are platform-dependent
i.e. components are displayed according to the
view of operating system.
• AWT is heavy weight i.e. its components are using
the resources of underlying operating system (OS).
• The java.awt package provides classes for AWT API
such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.
Java AWT Hierarchy
• The hierarchy of Java AWT classes are given below.
MODULE 5.pptx gui programming and applets
Components
• All the elements like the button, text fields, scroll bars,
etc. are called components. In order to place every
component in a particular position on a screen, we need
to add them to a container.
Container
• The Container is a component in AWT that can contain
another components like buttons, textfields, labels etc.
The classes that extends Container class are known as
container such as Frame, Dialog and Panel. It is
basically a screen where the where the components are
placed at their specific locations. Thus it contains and
controls the layout of components.
• There are four types of containers in Java AWT:
Window
Panel
Frame
Dialog
Window:
• Window is a top-level container that represents a
graphical window or dialog box.
• The Window class extends the Container class,
which means it can contain other components, such
as buttons, labels, and text fields.
Panel:
• Panel is a container class in Java.
• It is a lightweight container that can be used for
grouping other components together within a
window or a frame.
Frame:
• The Frame is the container that contains the title bar
and border and can have menu bars.
Dialog:
• A dialog box is a temporary window an application
creates to retrieve user input.
Java AWT Example
• To create simple AWT example, you need a frame.
There are two ways to create a GUI using Frame in
AWT.
• By extending Frame class (inheritance)
• By creating the object of Frame class (association)
AWT Example by Inheritance
• Let's see a simple example of AWT where we
are inheriting Frame class.
• Here, we are showing Button component on the
Frame.
Example:
import java.awt.*;
public class AWTExample1 extends Frame
{
AWTExample1()
{
Button b = new Button("Click Me!!");
b.setBounds(30,100,80,30);
add(b);
setSize(300,300);
setTitle("This is our basic AWT example");
setLayout(null);
setVisible(true); }
public static void main(String args[])
{
AWTExample1 f = new AWTExample1();
}
}
Output
AWT Example by Association
• Let's see a simple example of AWT where we are
creating instance of Frame class.
• Here, we are creating a TextField, Label and Button
component on the Frame.
Example
import java.awt.*;
class AWTExample2
{
AWTExample2()
{
Frame f = new Frame();
Label l = new Label("Employee id:");
Button b = new Button("Submit");
TextField t = new TextField();
l.setBounds(20, 80, 80, 30);
t.setBounds(20, 100, 80, 30);
b.setBounds(100, 100, 80, 30);
f.add(b)
f.add(l);
f.add(t);
f.setSize(400,300);
f.setTitle("Employee info");
f.setLayout(null);
f.setVisible(true); }
public static void main(String args[])
{
AWTExample2 awt_obj = new AWTExample2();
}
} output
SWING
• Swing is a Java Foundation Classes [JFC] library and
an extension of the Abstract Window Toolkit
[AWT].
• Swing 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, JColorChooser etc.
Difference between AWT and Swing
Java AWT Java Swing
AWT components are platform-
dependent.
Java swing components are platform-
independent.
AWT components are heavyweight. Swing components are lightweight.
AWT doesn't support pluggable
look and feel.
Swing supports pluggable look and
feel.
AWT provides less components than
Swing.
Swing provides more powerful
components such as tables, lists,
scrollpanes, colorchooser, tabbedpane
etc.
AWT doesn't follows MVC(Model
View Controller) where model
represents data, view represents
presentation and controller acts as an
interface between model and view.
Swing follows MVC.
Hierarchy of Java Swing classes
Swing Containers
• Containers are an integral part of SWING GUI components.
• A container provides a space where a component can be
located.
• A Container in AWT is a component itself and it provides
the capability to add a component to itself.
• Following are certain noticable points to be considered.
• Sub classes of Container are called as Container.
• For example, JPanel, JFrame and JWindow.
• Container can add only a Component to itself.
• A default layout is present in each container which can be
overridden using setLayout method.
• Following is the list of commonly used containers
while designed GUI using SWING.
Panel:
• JPanel is the simplest container. It provides space in
which any other component can be placed,
including other panels.
Frame:
• A JFrame is a top-level window with a title and a
border.
Window:
• A JWindow object is a top-level window with no
borders and no menubar.
Swing Components
• A component is an independent visual control and
Java Swing Framework contains a large set of these
components which provide rich functionalities and
allow high level of customization.
• They all are derived from JComponent class.
• All these components are lightweight components.
• This class provides some common functionality
like pluggable look and feel, support for
accessibility, drag and drop, layout, etc.
• A container holds a group of components.
• It provides a space where a component can be
managed and displayed.
• Containers are of two types:
Top level Containers
Lightweight Containers
• Top level Containers
– It inherits Component and Container of AWT.
– It cannot be contained within other containers.
– Heavyweight.
– Example: JFrame, JDialog, JApplet
• Lightweight Containers
– It inherits JComponent class.
– It is a general purpose container.
– It can be used to organize related components
together.
– Example: JPanel
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.
• Unlike Frame, JFrame has the option to hide
or close the window with the help of
setDefaultCloseOperation(int) method.
Example
import javax.swing.JFrame;
public class JFrameExample
{
public static void main(String s[])
{
JFrame frame = new JFrame("JFrame Example");
frame.setSize(300, 300);
frame.setVisible(true);
}
}
MODULE 5.pptx gui programming and applets
JDialog
• The JDialog control represents a top level window
with a border and a title used to take some form of
input from the user. It inherits the Dialog class.
• Unlike JFrame, it doesn't have maximize and
minimize buttons.
• Commonly used Constructors
Constructor Description
JDialog() It is used to create a modeless dialog without a title
and without a specified Frame owner.
JDialog(Frame owner) It is used to create a modeless dialog with specified
Frame as its owner and an empty title.
import javax.swing.*;
import java.awt.*;
public class DialogExample
{
DialogExample()
{
JFrame f= new JFrame();
JDialog d = new JDialog(f , "RNSIT", true);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String args[])
{
new DialogExample();
}
}
MODULE 5.pptx gui programming and applets
JPanel
• The JPanel is a simplest container class. It
provides space in which an application can
attach any other component.
• It inherits the JComponents class.
• It doesn't have title bar.
JPanel class declaration
• public class JPanel extends JComponent imple
ments Accessible
Commonly used Constructors:
Constructor Description
JPanel() It is used to create a new
JPanel with a double buffer
and a flow layout.
JPanel(boolean
isDoubleBuffered)
It is used to create a new
JPanel with FlowLayout and
the specified buffering
strategy.
JPanel(LayoutManager layout) It is used to create a new
JPanel with the specified
layout manager.
import java.awt.*;
import javax.swing.*;
public class PanelExample
{
PanelExample()
{
JFrame f= new JFrame("Panel Example");
JPanel panel=new JPanel();
panel.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
JButton b1=new JButton("Button 1");
b1.setBounds(50,100,80,30);
b1.setBackground(Color.yellow);
JButton b2=new JButton("Button 2");
b2.setBounds(100,100,80,30);
b2.setBackground(Color.green);
panel.add(b1); panel.add(b2);
f.add(panel);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new PanelExample();
}
}
MODULE 5.pptx gui programming and applets
JButton
• We use JButton class to create a push button on the
UI.
• The button can include some display text or images.
• It yields an event when clicked and double-clicked.
• We can implement a JButton in the application by
calling one of its constructors.
• Syntax:
JButton okBtn = new JButton(“Click”);
• Commonly used Constructors:
Constructor Description
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.
Example:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class testswing extends JFrame
{
testswing()
{
JButton bt1 = new JButton("click");
setLayout(new FlowLayout());
setSize(400, 400)
add(bt1);
setVisible(true);
}
public static void main(String[] args)
{
new testswing();
}
}
JLabel
• In Java, Swingtoolkit contains a JLabel Class.
• It is under package javax.swing.JLabel class.
• It is used for placing text in a box.
• Only Single line text is allowed and the text
can not be changed directly.
• The JLabel Contains 4 constructors.
1. JLabel()
2. JLabel(String s)
3. JLabel(Icon i)
4. JLabel(String s, Icon i, int horizontalAlignment)
class SLabel
{
public static void main(String args[])
{
JFrame label_f= new JFrame("Label Demo");
JLabel label_l1,label_l2;
label_l1=new JLabel("Welcome to studytonight.com");
label_l1.setBounds(50,50, 200,30);
label_l2=new JLabel("How are You?");
label_l2.setBounds(50,100, 200,30);
label_f.add(label_l1);
label_f.add(label_l2);
label_f.setSize(300,300);
label_f.setLayout(null);
label_f.setVisible(true);
}
}
MODULE 5.pptx gui programming and applets
JTextField
• The object of a JTextField class is a text
component that allows the editing of a single line
text. It inherits JTextComponent class.
Commonly used Constructors:
Constructor Description
JTextField() Creates a new TextField
JTextField(String text) Creates a new TextField initialized with the
specified text.
JTextField(String text, int
columns)
Creates a new TextField initialized with the
specified text and columns.
JTextField(int columns) Creates a new empty TextField with the
specified number of columns.
import javax.swing.*;
class TextFieldExample
{
public static void main(String args[])
{
JFrame f= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField();
t1.setBounds(50,100, 200,30);
f.add(t1);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
MODULE 5.pptx gui programming and applets
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.
Commonly used 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 column)
Creates a text area with the specified number of rows
and columns that displays specified text.
import javax.swing.*;
public class TextAreaExample
{
TextAreaExample()
{
JFrame f= new JFrame();
JTextArea area=new JTextArea();
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();
}
}
MODULE 5.pptx gui programming and applets
BorderLayout (LayoutManagers)
• The LayoutManagers are used to arrange
components in a particular manner.
• The Java LayoutManagers facilitates us to
control the positioning and size of the
components in GUI forms.
• LayoutManager is an interface that is
implemented by all the classes of layout
managers.
• :
• There are the following classes that represent the
layout managers
java.awt.BorderLayout
java.awt.FlowLayout
java.awt.GridLayout
java.awt.CardLayout
java.awt.GridBagLayout
javax.swing.BoxLayout
javax.swing.GroupLayout
javax.swing.ScrollPaneLayout
javax.swing.SpringLayout etc.
BorderLayout
• The BorderLayout is used to arrange the components
in five regions: north, south, east, west, and center.
Each region (area) may contain one component only.
It is the default layout of a frame or window. The
BorderLayout provides five constants for each
region:
• public static final int NORTH
• public static final int SOUTH
• public static final int EAST
• public static final int WEST
• public static final int CENTER
import java.awt.*;
import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f = new JFrame();
JButton b1 = new JButton("NORTH");
JButton b2 = new JButton("SOUTH");
JButton b3 = new JButton("EAST");
JButton b4 = new JButton("WEST");
JButton b5 = new JButton("CENTER");
f.add(b1, BorderLayout.NORTH);
f.add(b2, BorderLayout.SOUTH);
f.add(b3, BorderLayout.EAST);
f.add(b4, BorderLayout.WEST);
f.add(b5, BorderLayout.CENTER);
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args)
{
new Border();
}
}
MODULE 5.pptx gui programming and applets
GridLayout
• The Java GridLayout class is used to arrange the
components in a rectangular grid. One component is
displayed in each rectangle.
Constructors of GridLayout class
• GridLayout(): creates a grid layout with one column per
component in a row.
• GridLayout(int rows, int columns): creates a grid
layout with the given rows and columns but no gaps
between the components.
• GridLayout(int rows, int columns, int hgap, int
vgap): creates a grid layout with the given rows and
columns along with given horizontal and vertical gaps.
import java.awt.*;
import javax.swing.*;
public class GridLayoutExample
{
JFrame frameObj;
GridLayoutExample()
{
frameObj = new JFrame();
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
frameObj.add(btn1); frameObj.add(btn2);
frameObj.add(btn3); frameObj.add(btn4);
frameObj.setLayout(new GridLayout(2,2));
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
public static void main(String argvs[])
{
new GridLayoutExample();
}
}
MODULE 5.pptx gui programming and applets
FlowLayout
• The Java FlowLayout class is used to arrange the
components in a line, one after another (in a
flow). It is the default layout of the applet or
panel.
Fields of FlowLayout class
• public static final int LEFT
• public static final int RIGHT
• public static final int CENTER
• public static final int LEADING
• public static final int TRAILING
• Constructors of FlowLayout class
• FlowLayout(): creates a flow layout with
centered alignment and a default 5 unit
horizontal and vertical gap.
• FlowLayout(int align): creates a flow layout
with the given alignment and a default 5 unit
horizontal and vertical gap.
• FlowLayout(int align, int hgap, int
vgap): creates a flow layout with the given
alignment and the given horizontal and vertical
gap.
import java.awt.*;
import javax.swing.*;
public class FlowLayoutExample
{
JFrame frameObj;
FlowLayoutExample()
{
frameObj = new JFrame();
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
frameObj.add(b1); frameObj.add(b2);
frameObj.add(b3); frameObj.add(b4);
frameObj.setLayout(new FlowLayout());
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
public static void main(String argvs[])
{
new FlowLayoutExample();
}
}
MODULE 5.pptx gui programming and applets
Applet Fundamentals
• Applet is a special type of program that is embedded
in the webpage to generate the dynamic content.
• It runs inside the browser and works at client side.
• Java Plug-in software is responsible to manage the
life cycle of an applet.
• Lifecycle of Java Applet
init
Start
paint
stop
destroy
• init(): is used to initialized the Applet. It is invoked only
once.
• start(): is invoked after the init() method or browser is
maximized. It is used to start the Applet.
• paint( ) is also called when the applet begins execution.
Whatever the cause, whenever the applet must redraw its
output, paint( ) is called. The paint( ) method has one
parameter of type Graphics.
• stop(): is used to stop the Applet. It is invoked when
Applet is stop or browser is minimized.
• destroy( ) : The destroy( ) method is called when the
environment determines that your applet needs to be
removed completely from memory.
• The stop( ) method is always called before destroy( ).
example of Applet by html file:
//First.java
import java.awt.*;
import java.applet.*;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString(“Hello World", 20, 20);
}
}
//RunHelloWorld.html
<html>
<body>
<applet code="First" width=“200" height=“60">
</applet>
</body>
</html>
How to run:
• javac First.java
• appletviewer RunHelloWorld.html
MODULE 5.pptx gui programming and applets
• This applet begins with two import statements.
• The first imports the Abstract Window Toolkit (AWT)
classes. Applets interact with the user (either directly or
indirectly) through the AWT, not through the console-
based I/O classes.
• The AWT contains support for a window-based, graphical
user interface.
• The second import statement imports the applet
package, which contains the class Applet.
• Every applet that you create must be a subclass of Applet.
• paint( ) is called each time that the applet must
redisplay its output.
• Inside paint( ) is a call to drawString( ), which is a
member of the Graphics class.
• The call to drawString( ) in the applet causes the
message “A Simple Applet” to be displayed beginning
at location 20,20.
• Here are the key points that you should remember
now:
• Applets do not need a main( ) method.
• Applets must be run under an applet viewer or a
Java-compatible browser.
• User I/O is not accomplished with Java’s stream I/O
classes. Instead, applets use the interface provided
by the AWT or Swing.
Difference between Application and Applets
Passing Parameter in Applet
• We can get any information from the HTML file
as a parameter.
• For this purpose, Applet class provides a
method named getParameter().
Syntax:
public String getParameter(String parameterName)
Example
MyApplet.java
import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet
{
String n, a;
public void init()
{
n = getParameter("name");
a = getParameter("age");
}
public void paint(Graphics g)
{
g.drawString("Name is: " + n, 20, 20);
g.drawString("Age is: " + a, 20, 20);
}}
rnsit.html
<html>
<body>
<applet code="MyApplet" height="300"
width="500">
<param name="name" value="Ramesh" />
<param name="age" value="25" />
</applet>
</body>
</html>
MODULE 5.pptx gui programming and applets

More Related Content

PPTX
Computer Programming NC III - Java Swing.pptx
PDF
Swingpre 150616004959-lva1-app6892
PPT
Swing and AWT in java
DOC
java swing notes in easy manner for UG students
PDF
Z blue introduction to gui (39023299)
PPTX
Awt, Swing, Layout managers
PPTX
AWT.pptx
PPTX
awt and swing new (Abstract Window Toolkit).pptx
Computer Programming NC III - Java Swing.pptx
Swingpre 150616004959-lva1-app6892
Swing and AWT in java
java swing notes in easy manner for UG students
Z blue introduction to gui (39023299)
Awt, Swing, Layout managers
AWT.pptx
awt and swing new (Abstract Window Toolkit).pptx

Similar to MODULE 5.pptx gui programming and applets (20)

PPT
Chap1 1 1
PPT
Chap1 1.1
PDF
Java GUI Programming for beginners-graphics.pdf
PDF
PPTX
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
PPTX
AbstractWindowToolkit33333333333333.pptx
PDF
Swing
PDF
Swing
PDF
JAVA GUI PART I
PPTX
Chapter 1 swings
PPTX
Chapter iii(building a simple user interface)
PPT
Unit4 AWT, Swings & Layouts power point presentation
PPTX
swings.pptx
PPTX
Creating GUI.pptx Gui graphical user interface
PPT
Unit 1- awt(Abstract Window Toolkit) .ppt
PPTX
JAVA SWING PPT FOR PROGRAMMING AND CODING
PPT
awt.ppt java windows programming lecture
PPT
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
Chap1 1 1
Chap1 1.1
Java GUI Programming for beginners-graphics.pdf
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
AbstractWindowToolkit33333333333333.pptx
Swing
Swing
JAVA GUI PART I
Chapter 1 swings
Chapter iii(building a simple user interface)
Unit4 AWT, Swings & Layouts power point presentation
swings.pptx
Creating GUI.pptx Gui graphical user interface
Unit 1- awt(Abstract Window Toolkit) .ppt
JAVA SWING PPT FOR PROGRAMMING AND CODING
awt.ppt java windows programming lecture
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
Ad

Recently uploaded (20)

PDF
Software defined netwoks is useful to learn NFV and virtual Lans
PDF
25AF1191PC303 MODULE-1 CHAIN SURVEYING SEMESTER III SURVEYING
PDF
IAE-V2500 Engine for Airbus Family 319/320
PDF
ASPEN PLUS USER GUIDE - PROCESS SIMULATIONS
PPTX
Research Writing, Mechanical Engineering
PDF
Performance, energy consumption and costs: a comparative analysis of automati...
PDF
B461227.pdf American Journal of Multidisciplinary Research and Review
PPT
Module_1_Lecture_1_Introduction_To_Automation_In_Production_Systems2023.ppt
PDF
ST MNCWANGO P2 WIL (MEPR302) FINAL REPORT.pdf
PDF
PhD defense presentation in field of Computer Science
PDF
V2500 Owner and Operatore Guide for Airbus
PDF
THE PEDAGOGICAL NEXUS IN TEACHING ELECTRICITY CONCEPTS IN THE GRADE 9 NATURAL...
PDF
IAE-V2500 Engine Airbus Family A319/320
PPTX
22ME926Introduction to Business Intelligence and Analytics, Advanced Integrat...
PPTX
sub station Simple Design of Substation PPT.pptx
PPTX
highway-150803160405-lva1-app6891 (1).pptx
PPTX
IOP Unit 1.pptx for btech 1st year students
PDF
ITEC 1010 - Networks and Cloud Computing
PDF
Introduction to Machine Learning -Basic concepts,Models and Description
PPTX
MODULE 02 - CLOUD COMPUTING-Virtual Machines and Virtualization of Clusters a...
Software defined netwoks is useful to learn NFV and virtual Lans
25AF1191PC303 MODULE-1 CHAIN SURVEYING SEMESTER III SURVEYING
IAE-V2500 Engine for Airbus Family 319/320
ASPEN PLUS USER GUIDE - PROCESS SIMULATIONS
Research Writing, Mechanical Engineering
Performance, energy consumption and costs: a comparative analysis of automati...
B461227.pdf American Journal of Multidisciplinary Research and Review
Module_1_Lecture_1_Introduction_To_Automation_In_Production_Systems2023.ppt
ST MNCWANGO P2 WIL (MEPR302) FINAL REPORT.pdf
PhD defense presentation in field of Computer Science
V2500 Owner and Operatore Guide for Airbus
THE PEDAGOGICAL NEXUS IN TEACHING ELECTRICITY CONCEPTS IN THE GRADE 9 NATURAL...
IAE-V2500 Engine Airbus Family A319/320
22ME926Introduction to Business Intelligence and Analytics, Advanced Integrat...
sub station Simple Design of Substation PPT.pptx
highway-150803160405-lva1-app6891 (1).pptx
IOP Unit 1.pptx for btech 1st year students
ITEC 1010 - Networks and Cloud Computing
Introduction to Machine Learning -Basic concepts,Models and Description
MODULE 02 - CLOUD COMPUTING-Virtual Machines and Virtualization of Clusters a...
Ad

MODULE 5.pptx gui programming and applets

  • 1. Module 5 GUI PROGRAMMING AND APPLETS Java AWT (Abstract Window Toolkit) • Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI) or windows-based applications in Java. • Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system.
  • 2. • AWT is heavy weight i.e. its components are using the resources of underlying operating system (OS). • The java.awt package provides classes for AWT API such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc. Java AWT Hierarchy • The hierarchy of Java AWT classes are given below.
  • 4. Components • All the elements like the button, text fields, scroll bars, etc. are called components. In order to place every component in a particular position on a screen, we need to add them to a container. Container • The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel. It is basically a screen where the where the components are placed at their specific locations. Thus it contains and controls the layout of components.
  • 5. • There are four types of containers in Java AWT: Window Panel Frame Dialog Window: • Window is a top-level container that represents a graphical window or dialog box. • The Window class extends the Container class, which means it can contain other components, such as buttons, labels, and text fields.
  • 6. Panel: • Panel is a container class in Java. • It is a lightweight container that can be used for grouping other components together within a window or a frame. Frame: • The Frame is the container that contains the title bar and border and can have menu bars. Dialog: • A dialog box is a temporary window an application creates to retrieve user input.
  • 7. Java AWT Example • To create simple AWT example, you need a frame. There are two ways to create a GUI using Frame in AWT. • By extending Frame class (inheritance) • By creating the object of Frame class (association)
  • 8. AWT Example by Inheritance • Let's see a simple example of AWT where we are inheriting Frame class. • Here, we are showing Button component on the Frame. Example: import java.awt.*; public class AWTExample1 extends Frame { AWTExample1() {
  • 9. Button b = new Button("Click Me!!"); b.setBounds(30,100,80,30); add(b); setSize(300,300); setTitle("This is our basic AWT example"); setLayout(null); setVisible(true); } public static void main(String args[]) { AWTExample1 f = new AWTExample1(); } }
  • 11. AWT Example by Association • Let's see a simple example of AWT where we are creating instance of Frame class. • Here, we are creating a TextField, Label and Button component on the Frame. Example import java.awt.*; class AWTExample2 { AWTExample2() {
  • 12. Frame f = new Frame(); Label l = new Label("Employee id:"); Button b = new Button("Submit"); TextField t = new TextField(); l.setBounds(20, 80, 80, 30); t.setBounds(20, 100, 80, 30); b.setBounds(100, 100, 80, 30); f.add(b) f.add(l); f.add(t); f.setSize(400,300); f.setTitle("Employee info"); f.setLayout(null); f.setVisible(true); }
  • 13. public static void main(String args[]) { AWTExample2 awt_obj = new AWTExample2(); } } output
  • 14. SWING • Swing is a Java Foundation Classes [JFC] library and an extension of the Abstract Window Toolkit [AWT]. • Swing 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, JColorChooser etc.
  • 15. Difference between AWT and Swing Java AWT Java Swing AWT components are platform- dependent. Java swing components are platform- independent. AWT components are heavyweight. Swing components are lightweight. AWT doesn't support pluggable look and feel. Swing supports pluggable look and feel. AWT provides less components than Swing. Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc. AWT doesn't follows MVC(Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view. Swing follows MVC.
  • 16. Hierarchy of Java Swing classes
  • 17. Swing Containers • Containers are an integral part of SWING GUI components. • A container provides a space where a component can be located. • A Container in AWT is a component itself and it provides the capability to add a component to itself. • Following are certain noticable points to be considered. • Sub classes of Container are called as Container. • For example, JPanel, JFrame and JWindow. • Container can add only a Component to itself. • A default layout is present in each container which can be overridden using setLayout method.
  • 18. • Following is the list of commonly used containers while designed GUI using SWING. Panel: • JPanel is the simplest container. It provides space in which any other component can be placed, including other panels. Frame: • A JFrame is a top-level window with a title and a border. Window: • A JWindow object is a top-level window with no borders and no menubar.
  • 19. Swing Components • A component is an independent visual control and Java Swing Framework contains a large set of these components which provide rich functionalities and allow high level of customization. • They all are derived from JComponent class. • All these components are lightweight components. • This class provides some common functionality like pluggable look and feel, support for accessibility, drag and drop, layout, etc.
  • 20. • A container holds a group of components. • It provides a space where a component can be managed and displayed. • Containers are of two types: Top level Containers Lightweight Containers
  • 21. • Top level Containers – It inherits Component and Container of AWT. – It cannot be contained within other containers. – Heavyweight. – Example: JFrame, JDialog, JApplet • Lightweight Containers – It inherits JComponent class. – It is a general purpose container. – It can be used to organize related components together. – Example: JPanel
  • 22. 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. • Unlike Frame, JFrame has the option to hide or close the window with the help of setDefaultCloseOperation(int) method.
  • 23. Example import javax.swing.JFrame; public class JFrameExample { public static void main(String s[]) { JFrame frame = new JFrame("JFrame Example"); frame.setSize(300, 300); frame.setVisible(true); } }
  • 25. JDialog • The JDialog control represents a top level window with a border and a title used to take some form of input from the user. It inherits the Dialog class. • Unlike JFrame, it doesn't have maximize and minimize buttons. • Commonly used Constructors Constructor Description JDialog() It is used to create a modeless dialog without a title and without a specified Frame owner. JDialog(Frame owner) It is used to create a modeless dialog with specified Frame as its owner and an empty title.
  • 26. import javax.swing.*; import java.awt.*; public class DialogExample { DialogExample() { JFrame f= new JFrame(); JDialog d = new JDialog(f , "RNSIT", true); d.setSize(300,300); d.setVisible(true); } public static void main(String args[]) { new DialogExample(); } }
  • 28. JPanel • The JPanel is a simplest container class. It provides space in which an application can attach any other component. • It inherits the JComponents class. • It doesn't have title bar. JPanel class declaration • public class JPanel extends JComponent imple ments Accessible
  • 29. Commonly used Constructors: Constructor Description JPanel() It is used to create a new JPanel with a double buffer and a flow layout. JPanel(boolean isDoubleBuffered) It is used to create a new JPanel with FlowLayout and the specified buffering strategy. JPanel(LayoutManager layout) It is used to create a new JPanel with the specified layout manager.
  • 30. import java.awt.*; import javax.swing.*; public class PanelExample { PanelExample() { JFrame f= new JFrame("Panel Example"); JPanel panel=new JPanel(); panel.setBounds(40,80,200,200); panel.setBackground(Color.gray); JButton b1=new JButton("Button 1"); b1.setBounds(50,100,80,30); b1.setBackground(Color.yellow);
  • 31. JButton b2=new JButton("Button 2"); b2.setBounds(100,100,80,30); b2.setBackground(Color.green); panel.add(b1); panel.add(b2); f.add(panel); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } public static void main(String args[]) { new PanelExample(); } }
  • 33. JButton • We use JButton class to create a push button on the UI. • The button can include some display text or images. • It yields an event when clicked and double-clicked. • We can implement a JButton in the application by calling one of its constructors. • Syntax: JButton okBtn = new JButton(“Click”);
  • 34. • Commonly used Constructors: Constructor Description 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.
  • 35. Example: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class testswing extends JFrame { testswing() { JButton bt1 = new JButton("click"); setLayout(new FlowLayout());
  • 36. setSize(400, 400) add(bt1); setVisible(true); } public static void main(String[] args) { new testswing(); } }
  • 37. JLabel • In Java, Swingtoolkit contains a JLabel Class. • It is under package javax.swing.JLabel class. • It is used for placing text in a box. • Only Single line text is allowed and the text can not be changed directly. • The JLabel Contains 4 constructors. 1. JLabel() 2. JLabel(String s) 3. JLabel(Icon i) 4. JLabel(String s, Icon i, int horizontalAlignment)
  • 38. class SLabel { public static void main(String args[]) { JFrame label_f= new JFrame("Label Demo"); JLabel label_l1,label_l2; label_l1=new JLabel("Welcome to studytonight.com"); label_l1.setBounds(50,50, 200,30); label_l2=new JLabel("How are You?"); label_l2.setBounds(50,100, 200,30); label_f.add(label_l1); label_f.add(label_l2); label_f.setSize(300,300); label_f.setLayout(null); label_f.setVisible(true); } }
  • 40. JTextField • The object of a JTextField class is a text component that allows the editing of a single line text. It inherits JTextComponent class. Commonly used Constructors: Constructor Description JTextField() Creates a new TextField JTextField(String text) Creates a new TextField initialized with the specified text. JTextField(String text, int columns) Creates a new TextField initialized with the specified text and columns. JTextField(int columns) Creates a new empty TextField with the specified number of columns.
  • 41. import javax.swing.*; class TextFieldExample { public static void main(String args[]) { JFrame f= new JFrame("TextField Example"); JTextField t1,t2; t1=new JTextField(); t1.setBounds(50,100, 200,30); f.add(t1); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } }
  • 43. 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. Commonly used 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 column) Creates a text area with the specified number of rows and columns that displays specified text.
  • 44. import javax.swing.*; public class TextAreaExample { TextAreaExample() { JFrame f= new JFrame(); JTextArea area=new JTextArea(); 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(); } }
  • 46. BorderLayout (LayoutManagers) • The LayoutManagers are used to arrange components in a particular manner. • The Java LayoutManagers facilitates us to control the positioning and size of the components in GUI forms. • LayoutManager is an interface that is implemented by all the classes of layout managers. • :
  • 47. • There are the following classes that represent the layout managers java.awt.BorderLayout java.awt.FlowLayout java.awt.GridLayout java.awt.CardLayout java.awt.GridBagLayout javax.swing.BoxLayout javax.swing.GroupLayout javax.swing.ScrollPaneLayout javax.swing.SpringLayout etc.
  • 48. BorderLayout • The BorderLayout is used to arrange the components in five regions: north, south, east, west, and center. Each region (area) may contain one component only. It is the default layout of a frame or window. The BorderLayout provides five constants for each region: • public static final int NORTH • public static final int SOUTH • public static final int EAST • public static final int WEST • public static final int CENTER
  • 49. import java.awt.*; import javax.swing.*; public class Border { JFrame f; Border() { f = new JFrame(); JButton b1 = new JButton("NORTH"); JButton b2 = new JButton("SOUTH"); JButton b3 = new JButton("EAST"); JButton b4 = new JButton("WEST"); JButton b5 = new JButton("CENTER");
  • 50. f.add(b1, BorderLayout.NORTH); f.add(b2, BorderLayout.SOUTH); f.add(b3, BorderLayout.EAST); f.add(b4, BorderLayout.WEST); f.add(b5, BorderLayout.CENTER); f.setSize(300, 300); f.setVisible(true); } public static void main(String[] args) { new Border(); } }
  • 52. GridLayout • The Java GridLayout class is used to arrange the components in a rectangular grid. One component is displayed in each rectangle. Constructors of GridLayout class • GridLayout(): creates a grid layout with one column per component in a row. • GridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components. • GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and columns along with given horizontal and vertical gaps.
  • 53. import java.awt.*; import javax.swing.*; public class GridLayoutExample { JFrame frameObj; GridLayoutExample() { frameObj = new JFrame(); JButton btn1 = new JButton("1"); JButton btn2 = new JButton("2"); JButton btn3 = new JButton("3"); JButton btn4 = new JButton("4");
  • 54. frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3); frameObj.add(btn4); frameObj.setLayout(new GridLayout(2,2)); frameObj.setSize(300, 300); frameObj.setVisible(true); } public static void main(String argvs[]) { new GridLayoutExample(); } }
  • 56. FlowLayout • The Java FlowLayout class is used to arrange the components in a line, one after another (in a flow). It is the default layout of the applet or panel. Fields of FlowLayout class • public static final int LEFT • public static final int RIGHT • public static final int CENTER • public static final int LEADING • public static final int TRAILING
  • 57. • Constructors of FlowLayout class • FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap. • FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap. • FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment and the given horizontal and vertical gap.
  • 58. import java.awt.*; import javax.swing.*; public class FlowLayoutExample { JFrame frameObj; FlowLayoutExample() { frameObj = new JFrame(); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4");
  • 59. frameObj.add(b1); frameObj.add(b2); frameObj.add(b3); frameObj.add(b4); frameObj.setLayout(new FlowLayout()); frameObj.setSize(300, 300); frameObj.setVisible(true); } public static void main(String argvs[]) { new FlowLayoutExample(); } }
  • 61. Applet Fundamentals • Applet is a special type of program that is embedded in the webpage to generate the dynamic content. • It runs inside the browser and works at client side. • Java Plug-in software is responsible to manage the life cycle of an applet. • Lifecycle of Java Applet init Start paint stop destroy
  • 62. • init(): is used to initialized the Applet. It is invoked only once. • start(): is invoked after the init() method or browser is maximized. It is used to start the Applet. • paint( ) is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called. The paint( ) method has one parameter of type Graphics. • stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized. • destroy( ) : The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. • The stop( ) method is always called before destroy( ).
  • 63. example of Applet by html file: //First.java import java.awt.*; import java.applet.*; public class First extends Applet { public void paint(Graphics g) { g.drawString(“Hello World", 20, 20); } }
  • 64. //RunHelloWorld.html <html> <body> <applet code="First" width=“200" height=“60"> </applet> </body> </html> How to run: • javac First.java • appletviewer RunHelloWorld.html
  • 66. • This applet begins with two import statements. • The first imports the Abstract Window Toolkit (AWT) classes. Applets interact with the user (either directly or indirectly) through the AWT, not through the console- based I/O classes. • The AWT contains support for a window-based, graphical user interface. • The second import statement imports the applet package, which contains the class Applet. • Every applet that you create must be a subclass of Applet. • paint( ) is called each time that the applet must redisplay its output. • Inside paint( ) is a call to drawString( ), which is a member of the Graphics class.
  • 67. • The call to drawString( ) in the applet causes the message “A Simple Applet” to be displayed beginning at location 20,20. • Here are the key points that you should remember now: • Applets do not need a main( ) method. • Applets must be run under an applet viewer or a Java-compatible browser. • User I/O is not accomplished with Java’s stream I/O classes. Instead, applets use the interface provided by the AWT or Swing.
  • 69. Passing Parameter in Applet • We can get any information from the HTML file as a parameter. • For this purpose, Applet class provides a method named getParameter(). Syntax: public String getParameter(String parameterName) Example
  • 70. MyApplet.java import java.awt.*; import java.applet.*; public class MyApplet extends Applet { String n, a; public void init() { n = getParameter("name"); a = getParameter("age"); } public void paint(Graphics g) { g.drawString("Name is: " + n, 20, 20); g.drawString("Age is: " + a, 20, 20); }}
  • 71. rnsit.html <html> <body> <applet code="MyApplet" height="300" width="500"> <param name="name" value="Ramesh" /> <param name="age" value="25" /> </applet> </body> </html>