Event Handling-
- Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs. Java Uses the Delegation Event Model to handle the events. This model defines the standard mechanism to generate and handle the events.Let's have a brief introduction to this model.
Different mechanism-
Steps involved in event handling-
- The User clicks the button and the event is generated.
- Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object.
- Event object is forwarded to the method of registered listener class.
- the method is now get executed and returns.
Points to remember about listener-
- In order to design a listener class we have to develop some listener interfaces.These Listener interfaces forecast some public abstract callback methods which must be implemented by the listener class.
- If you do not implement the any if the predefined interfaces then your class can not act as a listener class for a source object.
The delegation event model-
- The Delegation Event model is one of the many techniques used to handle events in GUI (Graphical User Interface) programming languages. GUI represents a system where an user visually/graphically interacts with the system. Other interactive systems are text based or called CUI (Character User Interface). CUI interacts with the system by typing out commands in the console.
- Back in the old days, Java used a Chain of Responsibility pattern to process events. For example, when a button is clicked, a event is generated, which then is passed through a chain of components. The chain of components is defined by the hierarchy of classes and interfaces. An event is caught and handled by the handler class. This mechanism was used by Java version 1.0, which is very different from the event handling scheme of Java version 1.1 onwards. Old methods are still supported, but deprecated and hence not recommended for new programs. A modern approach is based on the delegation event model.
Event classes-
- The Event classes represent the event. Java provides us various Event classes but we will discuss those which are more frequently used.
Following is the list of commonly used event classes.
Control & Description
1 AWTEvent
It is the root event class for all AWT events. This class and its
subclasses supercede the original java.awt.Event class.
2 ActionEvent
The ActionEvent is generated when button is clicked or the item of a
list is double clicked.
3 InputEvent
The InputEvent class is root event class for all component-level input
events.
4 KeyEvent
On entering the character the Key event is generated.
5 MouseEvent
This event indicates a mouse action occurred in a component.
6 TextEvent
The object of this class represents the text events.
7 WindowEvent
The object of this class represents the change in state of a window.
8 AdjustmentEvent
The object of this class represents the adjustment event emitted by
Adjustable objects.
9 ComponentEvent
The object of this class represents the change in state of a window.
10 ContainerEvent
The object of this class represents the change in state of a window.
11 MouseMotionEvent
The object of this class represents the change in state of a window.
12 PaintEvent
The object of this class represents the change in state of a window.
Event Listener Interface-
- It is a marker interface which every listener interface has to extend.This class is defined in java.util package.
- Following is the list of commonly used event listeners.
Control & Description
1 ActionListener
This interface is used for receiving the action events.
2 ComponentListener
This interface is used for receiving the component events.
3 ItemListener
This interface is used for receiving the item events.
4 KeyListener
This interface is used for receiving the key events.
5 MouseListener
This interface is used for receiving the mouse events.
6 TextListener
This interface is used for receiving the text events.
7 WindowListener
This interface is used for receiving the window events.
8 AdjustmentListener
This interface is used for receiving the adjusmtent events.
9 ContainerListener
This interface is used for receiving the container events.
10 MouseMotionListener
This interface is used for receiving the mouse motion events.
11 FocusListener
This interface is used for receiving the focus events.
Adapter And Inner Classes-
Adapters:
Following is the list of commonly used adapters while listening GUI events in AWT.
Adapter & Description
1 FocusAdapter
An abstract adapter class for receiving focus events.
2 KeyAdapter
An abstract adapter class for receiving key events.
3 MouseAdapter
An abstract adapter class for receiving mouse events.
4 MouseMotionAdapter
An abstract adapter class for receiving mouse motion events.
5 WindowAdapter
An abstract adapter class for receiving window events.
Inner Classes-
Java inner class or nested class is a class which is declared inside the class or interface. We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
Additionally, it can access all the members of outer class including private data members and methods.
Syntax of Inner class
1. class Java_Outer_class{
2. //code
3. class Java_Inner_class{
4. //code
5. }
6. }
Working with windows-
- 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:
1. public abstract void windowActivated(WindowEvent e);
2. public abstract void windowClosed(WindowEvent e);
3. public abstract void windowClosing(WindowEvent e);
4. public abstract void windowDeactivated(WindowEvent e);
5. public abstract void windowDeiconified(WindowEvent e);
6. public abstract void windowIconified(WindowEvent e);
7. public abstract void windowOpened(WindowEvent e);
Graphics and text-
- Graphics controls allows application to draw onto the component or on image.
Graphics Controls
Control & Description
1 Graphics
It is the top level abstract class for all graphics contexts.
2 Graphics2D
It is a subclass of Graphics class and provides more sophisticated control over geometry, coordinate transformations, color management, and text layout.
3 Arc2D
Arc2D is the abstract superclass for all objects that store a 2D arc defined by a framing rectangle, start angle, angular extent (length of the arc), and a closure type (OPEN, CHORD, or PIE).
4 CubicCurve2D
The CubicCurve2D class is the abstract superclass fpr all objects which
store a 2D cubic curve segment and it defines a cubic parametric curve segment in (x,y) coordinate space.
5 Ellipse2D
The Ellipse2D is the abstract superclass for all objects which store a
2D ellipse and it describes an ellipse that is defined by a framing rectangle.
6 Rectangle2D
The Rectangle2D class is an abstract superclass for all objects that
store a 2D rectangle and it describes a rectangle defined by a location
(x,y) and dimension (w x h).
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
1. public class TextField extends TextComponent
Java AWT TextField Example
1. import java.awt.*;
2. class TextFieldExample{
3. public static void main(String args[]){
4. Frame f= new Frame("TextField Example");
5. TextField t1,t2;
6. t1=new TextField("Welcome to Javatpoint.");
7. t1.setBounds(50,100, 200,30);
8. t2=new TextField("AWT Tutorial");
9. t2.setBounds(50,150, 200,30);
10. f.add(t1); f.add(t2);
11. f.setSize(400,400);
12. f.setLayout(null);
13. f.setVisible(true);
14.}
15.}
Layout managers and menus-
Layout means the arrangement of components within the container. In other way we can say that placing the components at a particular position within the container. The task of layouting the controls is done automatically by the Layout Manager.
Layout Manager
The layout manager automatically positions all the components within the container. If we do not use layout manager then also the components are positioned by the default layout manager. It is possible to layout the controls by hand but it becomes very difficult because of the following two reasons.
- It is very tedious to handle a large number of controls within the container.
- Oftenly the width and height information of a component is not given when we need to arrange them.
- As we know that every top-level window has a menu bar associated with it. This menu bar consist of various menu choices available to the end user. Further each choice contains list of options which is called drop down menus. Menu and MenuItem controls are subclass of MenuComponent class.
- You want to read/write images to file location? Or you want to create an app like windows paint in java to edit images? If the first one you can use file and directory access package or file.nio package to save image in a file location. If you want to save blob/clob inside database then you can use JDBC call with RDBMS(MySQL). If you want to save images inside NoSQL(MongoDB) databases you can use the java connectivity library available for that database. The best practice to save image is like
1. Save the image to a file location like S3(AWS)
2. Keep the link to the file location inside a database.
3. Get the image when needed by calling the image path from databse
Sound and video-
JavaSE, via Java Sound API (in packages javax.sound), supports two types of audio:
- Sampled Audio: Sampled audio is represented as a sequence of time-sampled data of the amplitude of sound wave. It is supported in package javax.sound.sampled. The supported file formats are: "wav", "au" and "aiff". The samples can be either 8-bit or 16-bit, with sampling rate from 8 kHz to 48 kHz.
- Musical Instrument Digital Interface (MIDI): MIDI music is synthesized from musical notes and special sound effects, instead of time-sampled, like a recipe for creating musical sound. MIDI is supported in package javax.sound.midi.
Java Sound API also include a software sound mixer that supports up to 64 channels for sound effect and background music.
Java Media Framework (JMF), which is not part of JavaSE, is needed to support MP3 and advanced features. JOAL (Java Bindings on OpenAL) supports 3D sound effect.
Java applet-
An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal.
There are some important differences between an applet and a standalone Java application, including the following −
- An applet is a Java class that extends the java.applet.Applet class.
- A main() method is not invoked on an applet, and an applet class will not define main().
- Applets are designed to be embedded within an HTML page.
- When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine.
- A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.
- The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime.
- Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed.
- Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
Tags
Java