Graphical User Interface (GUI)
Graphical User Interface (GUI)
(GUI)
• AWT
• Applet
• SWING
• The application are divided into
• Standalone application
• CUI Application (Character user interface): Consider a program where we add two
numbers. We are getting the I/O on a command line argument. It will receive the value on
command prompt and will display the result on command line argument. This application
only support character type data. And that is why it is called character user interface.
• GUI Application: In GUI based application once we execute the program it will provide a
GUI for First value then second value and result will be displayed in GUI format as well.
The user interface is allowing graphical format so it is known as GUI application. GUI
application is java application, it was decided to take input in such a way through
collection of GUI application, the same collection is used to provide output.
• Distributed application
Package to Develop GUI Application
• Java.awt
• Javax.swing
• Java.applet
AWT (Abstract Windowing Toolkit)
• It is a framework, it will provide very good environment to prepare
GUI application
• It is a collection predefined classes and interfaces to prepare desktop
application
• It is an API, it can be used to prepare Window based application
• Components supported by AWT
• Following are the GUI components and classes or interfaces which are
representing GUI components in our application
• Without container we are unable to provide the interface
• Since to hold the labels, radio button, checkboxes etc we need
containers.
Container
• It is a GUI component which is able to manage other GUI components. E.g. frame,
window, panel
• Frame: Frame is a container which is able to manage some other GUI component
like label, text field, list boxes
• Frame is present in package java.awt.frame
• To create object to display two types of constructors are available
• Public Frame(): it will create a frame without title
• Public Frame(String title): it will create a frame with a particular title
Frame f=new Frame(“Login Frame”);
By using above line frame is created but by default it will be available in invisible
mode.
Example Frame
import java.awt.*;
public class JavaApplication8 {
public static void main(String[] args) {
// TODO code application logic here
Frame f=new Frame();
f.setVisible(true);
f.setSize(500, 500);
f.setBackground(Color.BLUE);
f.setTitle("Frame");
}
}
import java.awt.*;
class MyFrame extends Frame
{
MyFrame()
{
this.setVisible(true);
this.setSize(500, 500);
this.setTitle("USer Defined Frame Class");
this.setBackground(Color.BLUE);
}
}
public class JavaApplication8 {
public static void main(String[] args) {
MyFrame mf=new MyFrame();
}
}
Event Handling or Event Delegation Model
• Buttons , radio button, check boxes, labels are not capable of performing any task by their own.
Once we will click on the checkboxes or button then an event will be raised. But these buttons
or other component is not capable to handle these event.
• Listener is responsible to listen the event generated by GUI component, handle the event and
sending result back to GUI application. There are number of listeners are available in GUI
application with respect to GUI component.
• For the buttons an event will be raised named ActionEvent. To handle this event a separate
listener is available, and the name of listener is ActionListener
• In the same way check boxes, item event and item listener is available
• With respect to GUI component events are separated listeners are separated and corresponding
GUI components are able to raise the corresponding events and which are handled by
corresponding listeners. A set of components, a asset of events, a set of listeners are available.
• All events are classes and all the listeners are interfaces, these interfaces must be implement by
us.
ActionListener
Public void actionPerformed(Action event)
{
if(ADD){
tf3=tf1+tf2;
}
if(SUB){
tf3=tf1-tf2;
}
if(MUL){
tf3=tf1*tf2;
}
}
• Event Handling or event delegation model: Generating an event ,
delegating this event to listener, handling the event and giving back
the result to GUI application.
• In event handling there are following components
• GUI components i.e. Buttons
• Event
• listeners
• Listeners methods
• Java has provided predefined library to perform event handling in the
form of package named java.awt.event
Steps to perform event handling in GUI
application
• Create container class and create a 0 argument constructor, inside the
constructor declare a GUI component
class MyFrame extends Frame{
MyFrame(){
Button b=new Button(“submit”);
…..
}
}
• Select a listener and provide a listener implementation class with the implementation of
listener methods.
Class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
….
}
}
NOTE: In general, in GUI application, we will implement Listener interface in the same
container class
Class MyFrame extends Frame implements ActionListener{
…
}
Window Events
• After defining the property of frame one need to attach the listener to container
where MyFrame is GUI component and the windowLinetner that we have created
will be passed as parameter
• So firstly window event listener class where all the 7 methods is implemented.
• But when we create a frame by default frame is having minimize and maximize
action is available but close option is not available
• To define the closed option inside windowClosing() method we need to provide
System.exit(0)
• Since we are implementing windowListener interface so we have to implement all
the methods and only one method windowClosing() is important , if you so not
want to implement the unnecessary then one need to go another design method
and that is adapter design pattern
Adapter Design Pattern
• So instead of implementing windowListener interface extends the
WindowAdapter then only one method is required
public void windowClosing(WindowEvent we) method
WindowAdapter is a implementation class of windowListener where
null implementation is available for all the method that we are
extending for WindowListenerImpl so we can override our required
method not to provide implementation for all the methods.
Mouse Event are the interface that
needs to be implemented and the
methods to be overridden.
Mouse Event
Key Listener
Layout Mechanism
• To keep all GUI component in a particular order we need to go for
layout mechanism, may be align with window edges or may be rows
wise or may be along with rows and columns
• The main intention of layout mechanism is to manage all the GUI
element in an order in container
• FlowLayout
• BorderLayout
• GridLayout
• GridBagLayout
• CardLayout