Java AWT Components & JDBC
Java AWT Components & JDBC
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.
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
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)
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:
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.
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){}
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:
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.
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.
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.
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.
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".
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.
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.
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.
Output:
Java AWT List Example with ActionListener
Output:
Java AWT Canvas
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.
Output:
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.
Output:
Java AWT PopupMenu
PopupMenu can be dynamically popped up at specific position within a component. It inherits the
Menu class.
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.
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.
Frame vs Dialog
Frame and Dialog both inherits Window class. Frame has maximize and minimize buttons but Dialog
doesn't have.
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.
Output:
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.
Output:
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.
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.
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.
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.
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.
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.
Output:
Output:
Java MouseMotionAdapter Example
Output:
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:
The forName() method of Class class is used to register the driver class. This method is used to
dynamically load the driver class.
The getConnection() method of DriverManager class is used to establish connection with the database.
The createStatement() method of Connection interface is used to create statement. The object of
statement is responsible to execute queries with the database.
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.
while(rs.next()) {
System.out.println("id: " + rs.getInt(1));
System.out.println("Email: " + rs.getString(2));
System.out.println("username: " + rs.getString(3));
}
By closing connection object statement and ResultSet will be closed automatically. The close() method
of Connection interface is used to close the connection.
con.close();