SlideShare a Scribd company logo
2
Most read
3
Most read
14
Most read
M.SUJITHA,
II-M.SC(CS&IT),
Nadar Saraswathi College Of Arts and Science, Theni
 Java includes libraries to provide multi-platform
support for Graphic User Interface objects.
 Java's GUI components include labels, text fields,
text areas, buttons.
 The Abstract Windowing Toolkit (AWT) also
includes containers which includes these
components.
 Containers include frames (windows), canvases
which are used to draw and panels which are used
to group components.
 Panels and canvases are contained in frames while
buttons and other components can be placed either
directly on frames or in panels inside the frames.
 These GUI components are automatically drawn
whenever the window is drawn.
 These GUI components are handled using Java's
event model.
 When a user interacts with a component, an event is
generated by the component that you interact with.
 For each component of your program, the
programmer is required to designate one or more
objects to "listen" for events from that component.
 If the program has a button labelled "start" you
must assign one or more objects which will be notified
when a user clicks on the button.
BUTTONS
 Button is a class in package java.awt which
represents buttons on the screen.
 The constructor is: public Button(String label)
which, when executed, creates a button with "label"
printed on it.
 The button is large enough to display the label.
 There is also a parameter less constructor that
creates an unlabeled button.
 Buttons respond to a variety of messages.
 "Action Listener" to a button with the method:
public void addActionListener(ActionListener
listener);
ADDING BUTTONS TO A FRAME OR PANEL
 A command to add a button to a frame or panel:
 my Frame .add(start Button);
 This code is used in the constructor for the frame or
panel. add(start Button) or simply add(start Button).
 The class extends Frame, which is part of java.awt.
 The constructor for Frame takes a String parameter and
creates a window with the string in the title bar.
 The constructor for Button Demo calls the super class
constructor, and then sets the size of the new Frame to be 400
x 200.
 The set Layout command tells the new Frame that new
components should be added from left to right across the panel.
EXAMPLE CODING:
import java.awt.*;
public class Button Demo extends Frame
{
protected Button start Button, stop Button;
// Constructor sets features of the frame, creates
buttons, //
public Button Demo()
{
super("Button demo");
// calls Frame constructor //
which adds title to window setSize(400,200);
// sets the size of the window //
Grid Layout. setLayout(new FlowLayout());
// create two new buttons
labels start and stop startButton = new Button("Start");
stopButton = new Button("Stop");
add(startButton); //
add buttons to frame add(stopButton);
// create an object to listen to both buttons:
ButtonListener myButtonListener = new
ButtonListener();
startButton.addActionListener(myButtonListener);
stopButton.addActionListener(myButtonListener);
setVisible(true);
// Show the window on the screen. //
Trivial main program associated with ButtonDemo //
public static void main(String args[])
{
// Create an instance of Buttons ButtonDemo app = new
ButtonDemo()
}
}
ACTION LISTENERS FOR BUTTONS
 Objects which implement the Action Listener
interface are required to implement a method action
Performed which takes a parameter of type Action
Event.
 When an action occurs to a button, all objects
which have been added as Action Listener's for that
button are notified by calling their action Performed
method with a parameter with information on the
exact event that occurred.
 The system automatically creates the ActionEvent
object and sends it along to the listener.
 It is need to manually create an ActionEvent
object in this course.The most useful methods of
ActionEvent are
INNER CLASSES
A bit heavy to have to create a completely
separate class in order to create a listener for the
two buttons in our Button Demo class.
 Two alternatives are possible.
 1.One is to let the frame itself be the
Action Listener for the button.
 2.public class Button Demo extends Frame
implements Action Listener .
 This is the style suggested in Core Java for
handling action events in simple cases.
 There is another style which is almost as simple,
but more general. It involves the use of what are
called "inner classes".
EXAMPLE SOURCE CODE:
import java.awt.*;
import java.awt.event.*;
public class ButtonDemo extends Frame
{
protected Button startButton, stopButton; public ButtonDemo()
{
startButton.addActionListener(myButtonListener);
stopButton.addActionListener(myButtonListener);
}
public static void main(String args[])
{
{
if (source == startButton) System.out.println("Start button");
else if (source == stopButton) System.out.println("Stop button");
} } }
 ThetButton Listener is declared to be protected, it
can only be used inside the containing class, Button
Demo. The method Performed is still public. If it is
declared as protected.
 These nested classes would now be contained within
a single file named ButtonDemo.java.
 Another advantage of using nested classes is that
all of the instance variables (and methods) of the
outer class are visible inside the inner class.
 This can be handy for picking up information from
other components of the outer class.
OTHER GUI COMPONENTS
LABELS
 A Label is a very simple component which contains a
string.
 The constructors are public Label()
 // creates label with no text public Label//
 The methods available are public String getText()
 The return label text public void setText(String s)
 It sets the label text the user can change the text
in Labels.
TEXT FIELDS
 A TextField is a single line area that the user can
type into.
 It is a good way of getting text input from the
user.
 The constructors are public Text Field () .
 When the user types into a text field and then
hits the return or enter key, it generates an event
which can be handled by the same kind of Action
Listener used with Buttons.
 If for some reason the user likes to be notified
every time any change is made to the Text Field one
can associate a Text Listener to the field.
TEXT AREAS
 Text Area is a class that provides an area to hold
multiple lines of text.
 It is fairly similar to Text Field except that no
special event is generated by hitting the return key.
 The constructors are
public Text Area(int rows, int columns)
// create text area with rows, columns, and displaying s
Methods public void set Editable(booleans)
// if false the Text Area is not user editable public
String get Text() //
return text in Text Area public void set Text(String s)
// sets the text

More Related Content

PPTX
Evolution of computer
PPT
ARTIFICIAL INTELLIGENCE
PPT
Memory management
PPT
Design engineering
PDF
Java threads
PPTX
C++ language basic
PPTX
Presentation on C++ Programming Language
PPTX
Mobile computing
Evolution of computer
ARTIFICIAL INTELLIGENCE
Memory management
Design engineering
Java threads
C++ language basic
Presentation on C++ Programming Language
Mobile computing

What's hot (20)

PPTX
Introduction TO Finite Automata
PPTX
Exception handling
PPTX
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
PDF
Visual Basic 6.0
PPTX
JAVA AWT
PPT
Finite automata
PPT
Js ppt
PDF
Java conditional statements
PPTX
Java abstract class & abstract methods
PPTX
Constructor in java
PPT
Exception Handling in JAVA
PPS
Java Exception handling
PPTX
Java packages
PPTX
Java script cookies
PPT
Final keyword in java
PPTX
OOP Introduction with java programming language
PPTX
I/O Streams
PPTX
Inheritance in java
PPTX
Context free grammar
PPT
Object-oriented concepts
Introduction TO Finite Automata
Exception handling
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Visual Basic 6.0
JAVA AWT
Finite automata
Js ppt
Java conditional statements
Java abstract class & abstract methods
Constructor in java
Exception Handling in JAVA
Java Exception handling
Java packages
Java script cookies
Final keyword in java
OOP Introduction with java programming language
I/O Streams
Inheritance in java
Context free grammar
Object-oriented concepts
Ad

Similar to GUI components in Java (20)

PPT
Basic of Abstract Window Toolkit(AWT) in Java
PDF
AWT (Abstract Window Toolkit) Controls.pdf
PPTX
Java Abstract Window Toolkit (AWT) Presentation. 2024
PPTX
Java Abstract Window Toolkit (AWT) Presentation. 2024
PPTX
awt and swing new (Abstract Window Toolkit).pptx
PPT
AWT information
PPTX
Creating GUI.pptx Gui graphical user interface
PPT
engineeringdsgtnotesofunitfivesnists.ppt
PPT
Unit 5.133333333333333333333333333333333.ppt
PDF
Ajp notes-chapter-01
DOCX
Lecture8 oopj
PPTX
AbstractWindowToolkit33333333333333.pptx
PPT
Swing and AWT in java
PDF
Swingpre 150616004959-lva1-app6892
PPTX
javaprogramming framework-ppt frame.pptx
PDF
Ingles 2do parcial
PDF
Ajp notes-chapter-01
PPT
introduction to JAVA awt programmin .ppt
PPTX
Lecture 2 Introduction to AWT (1).ppt.hello
Basic of Abstract Window Toolkit(AWT) in Java
AWT (Abstract Window Toolkit) Controls.pdf
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
awt and swing new (Abstract Window Toolkit).pptx
AWT information
Creating GUI.pptx Gui graphical user interface
engineeringdsgtnotesofunitfivesnists.ppt
Unit 5.133333333333333333333333333333333.ppt
Ajp notes-chapter-01
Lecture8 oopj
AbstractWindowToolkit33333333333333.pptx
Swing and AWT in java
Swingpre 150616004959-lva1-app6892
javaprogramming framework-ppt frame.pptx
Ingles 2do parcial
Ajp notes-chapter-01
introduction to JAVA awt programmin .ppt
Lecture 2 Introduction to AWT (1).ppt.hello
Ad

More from kirupasuchi1996 (15)

PPTX
Rotor machine,subsitution technique
DOCX
rotor machine
PPTX
DVI,FRACTAL IMAGE,SUB BAND IMAGE,VIDEO CODING AND WAVELET BASED COMPRESSION
PPTX
Cyper crime
PPTX
PPTX
Image compression standards
PPTX
Language and Processors for Requirements Specification
PPTX
Software Cost Factor
PPTX
Designing Techniques in Software Engineering
PDF
Dmppt 180312092027
PDF
Datatransferandmanipulation 180214044522
PDF
Filesharing 180214044607
PDF
B tree-180214044656
PPTX
Addressingmodes
PPTX
Managing,working with files
Rotor machine,subsitution technique
rotor machine
DVI,FRACTAL IMAGE,SUB BAND IMAGE,VIDEO CODING AND WAVELET BASED COMPRESSION
Cyper crime
Image compression standards
Language and Processors for Requirements Specification
Software Cost Factor
Designing Techniques in Software Engineering
Dmppt 180312092027
Datatransferandmanipulation 180214044522
Filesharing 180214044607
B tree-180214044656
Addressingmodes
Managing,working with files

Recently uploaded (20)

PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PPTX
Web Security: Login Bypass, SQLi, CSRF & XSS.pptx
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
ai-archetype-understanding-the-personality-of-agentic-ai.pdf
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
How to Build Crypto Derivative Exchanges from Scratch.pptx
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
PDF
How AI Agents Improve Data Accuracy and Consistency in Due Diligence.pdf
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
PDF
Event Presentation Google Cloud Next Extended 2025
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Belt and Road Supply Chain Finance Blockchain Solution
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
creating-agentic-ai-solutions-leveraging-aws.pdf
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Web Security: Login Bypass, SQLi, CSRF & XSS.pptx
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Chapter 2 Digital Image Fundamentals.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
ai-archetype-understanding-the-personality-of-agentic-ai.pdf
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
How to Build Crypto Derivative Exchanges from Scratch.pptx
A Day in the Life of Location Data - Turning Where into How.pdf
How AI Agents Improve Data Accuracy and Consistency in Due Diligence.pdf
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
NewMind AI Weekly Chronicles - August'25 Week I
Enable Enterprise-Ready Security on IBM i Systems.pdf
Event Presentation Google Cloud Next Extended 2025
NewMind AI Monthly Chronicles - July 2025
Belt and Road Supply Chain Finance Blockchain Solution
Understanding_Digital_Forensics_Presentation.pptx
creating-agentic-ai-solutions-leveraging-aws.pdf

GUI components in Java

  • 2.  Java includes libraries to provide multi-platform support for Graphic User Interface objects.  Java's GUI components include labels, text fields, text areas, buttons.  The Abstract Windowing Toolkit (AWT) also includes containers which includes these components.  Containers include frames (windows), canvases which are used to draw and panels which are used to group components.  Panels and canvases are contained in frames while buttons and other components can be placed either directly on frames or in panels inside the frames.
  • 3.  These GUI components are automatically drawn whenever the window is drawn.  These GUI components are handled using Java's event model.  When a user interacts with a component, an event is generated by the component that you interact with.  For each component of your program, the programmer is required to designate one or more objects to "listen" for events from that component.  If the program has a button labelled "start" you must assign one or more objects which will be notified when a user clicks on the button.
  • 4. BUTTONS  Button is a class in package java.awt which represents buttons on the screen.  The constructor is: public Button(String label) which, when executed, creates a button with "label" printed on it.  The button is large enough to display the label.  There is also a parameter less constructor that creates an unlabeled button.  Buttons respond to a variety of messages.  "Action Listener" to a button with the method: public void addActionListener(ActionListener listener);
  • 5. ADDING BUTTONS TO A FRAME OR PANEL  A command to add a button to a frame or panel:  my Frame .add(start Button);  This code is used in the constructor for the frame or panel. add(start Button) or simply add(start Button).  The class extends Frame, which is part of java.awt.  The constructor for Frame takes a String parameter and creates a window with the string in the title bar.  The constructor for Button Demo calls the super class constructor, and then sets the size of the new Frame to be 400 x 200.  The set Layout command tells the new Frame that new components should be added from left to right across the panel.
  • 6. EXAMPLE CODING: import java.awt.*; public class Button Demo extends Frame { protected Button start Button, stop Button; // Constructor sets features of the frame, creates buttons, // public Button Demo() { super("Button demo"); // calls Frame constructor // which adds title to window setSize(400,200); // sets the size of the window //
  • 7. Grid Layout. setLayout(new FlowLayout()); // create two new buttons labels start and stop startButton = new Button("Start"); stopButton = new Button("Stop"); add(startButton); // add buttons to frame add(stopButton); // create an object to listen to both buttons: ButtonListener myButtonListener = new ButtonListener();
  • 8. startButton.addActionListener(myButtonListener); stopButton.addActionListener(myButtonListener); setVisible(true); // Show the window on the screen. // Trivial main program associated with ButtonDemo // public static void main(String args[]) { // Create an instance of Buttons ButtonDemo app = new ButtonDemo() } }
  • 9. ACTION LISTENERS FOR BUTTONS  Objects which implement the Action Listener interface are required to implement a method action Performed which takes a parameter of type Action Event.  When an action occurs to a button, all objects which have been added as Action Listener's for that button are notified by calling their action Performed method with a parameter with information on the exact event that occurred.  The system automatically creates the ActionEvent object and sends it along to the listener.  It is need to manually create an ActionEvent object in this course.The most useful methods of ActionEvent are
  • 10. INNER CLASSES A bit heavy to have to create a completely separate class in order to create a listener for the two buttons in our Button Demo class.  Two alternatives are possible.  1.One is to let the frame itself be the Action Listener for the button.  2.public class Button Demo extends Frame implements Action Listener .  This is the style suggested in Core Java for handling action events in simple cases.  There is another style which is almost as simple, but more general. It involves the use of what are called "inner classes".
  • 11. EXAMPLE SOURCE CODE: import java.awt.*; import java.awt.event.*; public class ButtonDemo extends Frame { protected Button startButton, stopButton; public ButtonDemo() { startButton.addActionListener(myButtonListener); stopButton.addActionListener(myButtonListener); } public static void main(String args[]) { { if (source == startButton) System.out.println("Start button"); else if (source == stopButton) System.out.println("Stop button"); } } }
  • 12.  ThetButton Listener is declared to be protected, it can only be used inside the containing class, Button Demo. The method Performed is still public. If it is declared as protected.  These nested classes would now be contained within a single file named ButtonDemo.java.  Another advantage of using nested classes is that all of the instance variables (and methods) of the outer class are visible inside the inner class.  This can be handy for picking up information from other components of the outer class.
  • 13. OTHER GUI COMPONENTS LABELS  A Label is a very simple component which contains a string.  The constructors are public Label()  // creates label with no text public Label//  The methods available are public String getText()  The return label text public void setText(String s)  It sets the label text the user can change the text in Labels.
  • 14. TEXT FIELDS  A TextField is a single line area that the user can type into.  It is a good way of getting text input from the user.  The constructors are public Text Field () .  When the user types into a text field and then hits the return or enter key, it generates an event which can be handled by the same kind of Action Listener used with Buttons.  If for some reason the user likes to be notified every time any change is made to the Text Field one can associate a Text Listener to the field.
  • 15. TEXT AREAS  Text Area is a class that provides an area to hold multiple lines of text.  It is fairly similar to Text Field except that no special event is generated by hitting the return key.  The constructors are public Text Area(int rows, int columns) // create text area with rows, columns, and displaying s Methods public void set Editable(booleans) // if false the Text Area is not user editable public String get Text() // return text in Text Area public void set Text(String s) // sets the text