Java AWT (Abstract Window Toolkit)
Java AWT is an API to develop GUI or window-based applications in java.
Java AWT components are platform-independent. Components are displayed according to the view of
operating system. AWT is heavy weight. Its components are using the resources of 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.
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.
Window
The Window is the container that have no borders and menu bars. You must use frame, dialog or
another window for creating a window.
Panel
The Panel is the container that doesn't contain title bar and can have menu bars. It can have other
components like button, textfield etc.
Frame
The Frame is the container that contain title bar and can have menu bars. It can have other components
like button, textfield etc.
Useful Methods of Component class
Java AWT Example
To create simple awt example, you need a frame. There are two ways to create a frame in AWT.
By extending Frame class (inherintace)
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 are showing Button
component on the Frame.
The setBounds(int xaxis, int yaxis, int width, int height) method is used in the above example that sets
the position of the awt button.
Output:
AWT Example by Association
Let's see a simple example of AWT where we are creating instance of Frame class. Here, we are
showing Button component on the Frame.
Output:
Event and Listener (Java Event Handling)
Changing the state of an object is known as an event. For example, click on button, dragging mouse
etc. The java.awt.event package provides many event classes and Listener interfaces for event handling.
Java Event classes and Listener interfaces
Steps to perform Event Handling
Following steps are required to perform event handling:
1. Register the component with the Listener
Registration Methods
For registering the component with the Listener, many classes provide the registration methods.
For example:
Button
public void addActionListener(ActionListener a){}
MenuItem
public void addActionListener(ActionListener a){}
TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
TextArea
public void addTextListener(TextListener a){}
Checkbox
public void addItemListener(ItemListener a){}
Choice
public void addItemListener(ItemListener a){}
List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}
Java Event Handling Code
We can put the event handling code into one of the following places:
1. Within class
2. Other class
3. Anonymous class
Java event handling by implementing ActionListener
public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above
example that sets the position of the component it may be button, textfield etc.
Output:
Java event handling by outer class
Java event handling by anonymous class
Java AWT Button
The button class is used to create a labeled button that has platform independent implementation. The
application result in some action when the button is pushed.
AWT Button Class declaration
Java AWT Button Example
Output:
Java AWT Button Example with ActionListener
Output:
Java AWT Label
The object of Label 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.
AWT Label Class Declaration
Java Label Example
Output:
Java AWT Label Example with ActionListener
Output:
Java AWT TextField
The object of a TextField class is a text component that allows the editing of a single line text. It
inherits TextComponent class.
AWT TextField Class Declaration
Java AWT TextField Example
Output:
Java AWT TextField Example with ActionListener
Output:
Java AWT TextArea
The object of a TextArea class is a multi line region that displays text. It allows the editing of multiple
line text. It inherits TextComponent class.
AWT TextArea Class Declaration
Java AWT TextArea Example
Output:
Java AWT TextArea Example with ActionListener
Output:
Java AWT Checkbox
The Checkbox 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".
AWT Checkbox Class Declaration
AWT Checkbox Example
Output:
Java AWT Checkbox Example with ItemListener
Output:
Java AWT CheckboxGroup
The object of CheckboxGroup class is used to group together a set of Checkbox. At a time only one
check box button is allowed to be in "on" state and remaining check box button in "off" state. It inherits
the object class.
Note: CheckboxGroup enables you to create radio buttons in AWT. There is no special control for
creating radio buttons in AWT.
AWT CheckboxGroup Class Declaration
Java AWT CheckboxGroup Example
Output:
Java AWT CheckboxGroup Example with ItemListener
Output:
Java AWT Choice
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. It inherits Component class.
AWT Choice Class Declaration
Java AWT Choice Example
Output:
Java AWT Choice Example with ActionListener
Output:
Java AWT List
The object of List class represents a list of text items. By the help of list, user can choose either one
item or multiple items. It inherits Component class.
AWT List Class Declaration
Java AWT List Example
Output:
Java AWT List Example with ActionListener
Output:
Java AWT Canvas
The Canvas control represents a blank
rectangular area where the application
can draw or trap input events from the
user. It inherits the Component class.
AWT Canvas class Declaration
Java AWT Canvas Example
Output:
Java AWT Scrollbar
The object of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a GUI
component allows us to see invisible number of rows and columns.
AWT Scrollbar Class Declaration
Java AWT Scrollbar Example
Output:
Java AWT Scrollbar Example with AdjustmentListener
Output:
Java AWT MenuItem and Menu
The object of MenuItem class adds a simple labeled menu item on menu. The items used in a menu
must belong to the MenuItem or any of its subclass.
The object of Menu class is a pull down menu component which is displayed on the menu bar. It
inherits the MenuItem class.
AWT MenuItem Class Declaration
AWT Menu Class Declaration
Java AWT MenuItem and Menu Example
Output:
Java AWT PopupMenu
PopupMenu can be dynamically popped up at specific position within a component. It inherits the
Menu class.
AWT PopupMenu Class Declaration
Java AWT PopupMenu Example
Output:
Java AWT Panel
The Panel is a simplest container class. It provides space in which an application can attach any other
component. It inherits the Container class.
It doesn't have title bar.
AWT Panel class declaration
Java AWT Panel Example
Output:
Java AWT Dialog
The Dialog 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 Window class.
Unlike Frame, it doesn't have maximize and minimize buttons.
Frame vs Dialog
Frame and Dialog both inherits Window class. Frame has maximize and minimize buttons but Dialog
doesn't have.
AWT Dialog class declaration
Java AWT Dialog Example
Output:
Java AWT Toolkit
Toolkit class is the abstract superclass of every implementation in the Abstract Window Toolkit.
Subclasses of Toolkit are used to bind various components. It inherits Object class.
AWT Toolkit Class Declaration
Java AWT Toolkit Example
Output:
Java AWT Toolkit Example: beep()
Output:
Java AWT Toolkit Example: Change TitleBar Icon
Output:
Java ActionListener Interface
The Java ActionListener is notified whenever you click on the button or menu item. It is notified
against ActionEvent. The ActionListener interface is found in java.awt.event package. It has only one
method: actionPerformed().
actionPerformed() method
The actionPerformed() method is invoked automatically whenever you click on the registered
component.
Java ActionListener Example: On Button click
Output:
Java MouseListener Interface
The Java MouseListener is notified whenever you change the state of mouse. It is notified against
MouseEvent. The MouseListener interface is found in java.awt.event package. It has five methods.
Methods of MouseListener Interface
The signature of 5 methods found in MouseListener interface are given below:
Java MouseListener Example
Output:
Java MouseListener Example 2
Output:
Java MouseMotionListener Interface
The Java MouseMotionListener is notified whenever you move or drag mouse. It is notified against
MouseEvent. The MouseMotionListener interface is found in java.awt.event package. It has two
methods.
Methods of MouseMotionListener Interface
The signature of 2 methods found in MouseMotionListener interface are given below:
Java MouseMotionListener Example
Output:
Java ItemListener Interface
The Java ItemListener is notified whenever you click on the checkbox. It is notified against ItemEvent.
The ItemListener interface is found in java.awt.event package. It has only one method:
itemStateChanged()
itemStateChanged() method
The itemStateChanged() method is invoked automatically whenever you click or unclick on the
registered checkbox component.
Java ItemListener Example
Output:
Java KeyListener Interface
The Java KeyListener is notified whenever you change the state of key. It is notified against KeyEvent.
The KeyListener interface is found in java.awt.event package. It has three methods.
Methods of KeyListener Interface
The signature of 3 methods found in KeyListener interface are given below:
Java KeyListener Example
Output:
Java WindowListener Interface
The Java WindowListener is notified whenever you change the state of window. It is notified against
WindowEvent. The WindowListener interface is found in java.awt.event package. It has three methods.
Methods of WindowListener Interface
The signature of 7 methods found in WindowListener interface are given below:
Java WindowListener Example
Java Adapter Classes
Java adapter classes provide the default implementation of listener interfaces. If you inherit the adapter
class, you will not be forced to provide the implementation of all the methods of listener interfaces. So
it saves code.
The adapter classes are found in java.awt.event, java.awt.dnd and javax.swing.event packages. The
Adapter classes with their corresponding listener interfaces are given below.
java.awt.event Adapter classes
java.awt.dnd Adapter classes
javax.swing.event Adapter classes
Java WindowAdapter Example
Output:
Java MouseAdapter Example
Output:
Java MouseMotionAdapter Example
Output:
Java KeyAdapter Example
Output:
Java JDBC Tutorial
Java JDBC is a java API to connect and execute query with the database. JDBC API uses jdbc drivers
to connect with the database.
What is API?
API (Application programming interface) is a document that contains description of all the features of a
product or software. It represents classes and interfaces that software programs can follow to
communicate with each other. An API can be created for applications, libraries, operating systems, etc.
There are 5 steps to connect any java application with the database in java using JDBC. They are as
follows:
Register the driver class
Creating connection
Creating statement
Executing queries
Closing connection
REGISTER THE DRIVER CLASS
The forName() method of Class class is used to register the driver class. This method is used to
dynamically load the driver class.
Syntaxt of forName() method
Example to register the MySQL Driver Class
Class.forName("com.mysql.jdbc.Driver");
CREATE THE CONNECTION OBJECT
The getConnection() method of DriverManager class is used to establish connection with the database.
Syntax of getConnection() method
Example to establish connection with the MySQL database
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","user2","password");
CREATE THE STATEMENT OBJECT
The createStatement() method of Connection interface is used to create statement. The object of
statement is responsible to execute queries with the database.
Syntax of createStatement() method
Example to create the statement object
Statement stmt = con.createStatement();
EXECUTE THE QUERY
The executeQuery() method of Statement interface is used to execute queries to the database. This
method returns the object of ResultSet that can be used to get all the records of a table.
Syntax of executeQuery() method
Example to execute query
ResultSet rs = stmt.executeQuery("select id,email,username from users");
while(rs.next()) {
System.out.println("id: " + rs.getInt(1));
System.out.println("Email: " + rs.getString(2));
System.out.println("username: " + rs.getString(3));
}
CLOSE THE CONNECTION OBJECT
By closing connection object statement and ResultSet will be closed automatically. The close() method
of Connection interface is used to close the connection.
Syntax of close() method
Example to close connection
con.close();