Advanced Java Tutorial


Total available pages count: 55
Subject - Java Technologies

Callback methods

The callback method is provided by API Provider & is defined during the application development. It represents the event method in the response to the event. All the callback methods are provided in the listener interface.

Java.awt.event package provides the list of classes & Listener Interfaces for event Handling.

Event Classes

Listener Interfaces

ActionEvent

ActionListener

mouseEvent

MouseListener and MouseMotionListener

MouseWheelEvent

MouseWheelListener

KeyEvent

KeyListener

ItemEvent

ItemListener

TextEvent

TextListener

AdjustmentEvent

AdjustmentListener

WindowEvent

WindowListener

ComponentEvent

ComponentListener

ContainerEvent

ContainerListener

FocusEvent

FocusListener

 

To perform Event handling:

We need to register the component with the listener. To register the component with the listener, many classes have provided the registration method.

for Button & MenuItem

public void addActionListener(ActionListener a)
{
}

 

TextField

public void addActionListener(ActionListener a)
{
}
public void addTextstener(TextListener a)
{
}

 

TextArea

public void addTextListener(TextListener a)
{
}

 

For Checkbox & Choice

public void addItemListener(ItemListener a)
{
}

 

List

public void addActionListener(ActionListener a)
{
}
public void addItemListener(ItemListener a)
{
}

 

We can perform event Handling at 3 places within the program,

  • Within class
  • Other class
  • Anonymous class

 

AWT Button

The button class has been used to create a labeled button. it has a platform-independent implementation. When the button gets pushed, the application will pop up result in some action.

Button class declaration,
 

public class Button extends Component implements Accessible

 

AWT Label

The object of the label is the component for placing the text in the container. It is used to display the single line of read-only text
The AWT Label is used to display a single line of read-only text. The object of the label is a component for placing text in the container. Text in the application can not modify by the user directly, it can be changed by the application itself only.
 

 

Label class declaration:

public class Label extends Component implements Accessible

 

AWT TextField

The object in the textfield class is the text component that allows the editing of single-line text. AWT Textfield inherits the TextComponent class.
 
 

Textfield class declaration:

public class TextFiled extends TextComponent

 

AWT TextArea

The object in TextArea class is a multiline region that displays the text. It allows editing of multiple texts that inherits TextComponent class.

Text area class declaration:

public class TextArea extends TextComponent

 

AWT Checkbox

The checkbox class used to create the checkbox in the AWT application.nit is used to turn an option true or false. Clicking on the checkbox changes the state from true to false or vice versa.
 

Checkbox class declaration:

public class Checkbox extends Component implements ItemSelectable, Accessible

 

AWT CheckboxGroup

The object in CheckboxGroup is used to group together a set of checkboxes that inherits the object class. At the same time, only one checkbox button is allowed to be in ON state & other are remain in OFF state.

In AWT, CheckboxGroup allows the creation of the radio button.it has no special control for creating the radio button.

CheckboxGroup class declaration:

public class CheckboxGroup extends Object implements Serializable

 

AWT Choice

The object in the chosen class is used to show the popup menu of the choice that inherits the component class. Choice can be selected by the user to view on top of the menu.
 

public class choice extends Component implements ItemSelectable, Accessible

 

AWT List

The object in the List class represents a list of text items that inherit the component class. With the help of a list, users can choose one or more than one item.
 

 

public class List extends Component inplements ItemSelectable, Accessible

 

AWT Canvas

The canvas control represents the black rectangular area where the application can draw input events from a user that inherits the component class.

public class Canvas extends Component implements Accessible

 

AWT Scrollbar

The object in the scrollbar class is used to add vertical & horizontal Scrollbar. The scrollbar based GUI component allows you to see the invisible number of row & columns.
 

public class Scrollbar extends Components implements Adjustable,Accessible

 

AWT MenuItem & Menu

The object in the Menu item class adds a simple labeled menu item on the menu. The item that used in the menu must belong to MenuItem or any of its subclass.
 

MenuItem Class Declaration:

public class MenuItem extends MenuComponent implements Accessible

 

MenuClass Declaration:
 

 

public class Menu extends MenuItem implements MenuContainer, Accessible

 

AWT PopupMenu

The PopupMenu can be dynamically popped up with a specific position within the component that inherits the Menu class.

PopupMenu Class Declaration:

public class popupMenu extends Menu implements MenuContainer, Accessible

 

AWT Panel

The Panel provides the space in which the application attaches any component, it is the simplest container class that inherits the container class.
 

Panel class Declaration

public class Panel extends Container implements Accessible

 

AWT Dialog

The dialog control represents the top-level window with the title & border that used to take some form of input from the user that inherits the window class. whereas Frame doesn’t have to minimize & Maximize buttons.
 

 

Dialog & Frame both inherit the window class. the frame can have minimized & maximized buttons but the dialog doesn’t have it.

Dialog class declaration:

public class Dialog extends window

 

AWT Toolkit

The AWT Toolkit is an abstract superclass of every implementation. The subclass of the toolkit is used to bind the various components that inherit object class.

AWT toolkit class declaration:

public abstract class Toolkit extends Object

 

AWT Event Class

AWT provides various event classes which are represented by the event.

EventObject class

EventObject is the root class from which all the event state objects can be derived. The events are built with reference to the object, the source is logically deemed to be the object upon which the event initially occurred.
 

Declaration of class:

public class EventObject extends Object implements Serializable

 

Field

Protected Object Source:

The event initially occurred on the object.

 

/*class constructor */
EventObject(Object source) : Constructs a prototypical Events.

/* class Method */
Object getSource() : The object on which the Events initially occurred.
String toString() : Returns a String representation of this EventObject.

 

AWTEvent class

AWTEvent class defined in java.awt package that is the root event class for all the AWT events.

Declaration of AWTEvent Class:

public class AWTEvent extends EventObject

 

Basically, AWTEvent class inherits method from classes: 

java.util.EventObject
java.lang.Object

/*AWTEventclass constructor */
AWTEvent(Event event)
constructs an AWTEvent object from the parameters of a 1.0-style event.

AWTEvent(java.lang.object source, int id)
constructs an AWTEvent object with the specified source object and type.

/* AWTEvent class Method */
protected void consume() : Consumes this event, if this event can be consumed.
int getID() : Returns the event type.
protected boolean isConsumed() : Returns a string representing the state of this Event.
void setSource(java.lang.Object newSource) : Retargets an event to a new source.
java.lang.String toString() : returns a String representation of this object.

 

InputEvent Class

In AWT, it is the root event class for all component-level input events. The input event delivered to the listener before it processed by the source where it originated. inputEvent allows listener & component subclasses to consume the event so that source will not process in their default manner. e.g. consuming Mousepressed event on the button component.

 

InputEvent Class declaration:

public abstract class InputEvent extends ComponentEvent

 

InputEvent class  inherits method from classes: 

java.awt.event.ComponentEvent
java.awt.AWTEvent
java.util.EventObject
java.lang.Object

/* InputEvent Class Method */
void consume() : Consumes this event so that it will not be proccessed in the default manner by the source which originated it.
int getModifiers() : Returns the modifier mask for this event.
int getModifiers() : Returns the extended modifier mask for this event.
static String getModifiersExText(int modifiers): Returns a string describing the extended modifier keys and mouse buttons,such as "Shift","button1", or "Ctrl+Shift",
long getwhen() : Returns the timestamp of when this event occured.
boolean isAltDown() : Returns whether or not the Alt modifier is down on this event.
boolean isAltGraphDown() : Returns whether or not the AltGraph modifier is down on this event.
boolean isConsumed() : Returns whether or not this event has been consumed.
boolean isControlDown() : Returns whether or not the Control modifier is down on this event.
boolean isMetaDown() : Returns whether or not the Meta modifier is down on this event.
boolean isShiftDown() : Returns whether or not the Shift modifier is down on this event.

 

ActionEvent Class

ActionEvent Class is defined in java.awt.event. it is generated when a button is clicked or an item of the list is double-clicked.
 

 

public class ActionEvent extends AWTEvent

 

ActionEvent class inherits method from classes: 

java.awt.AWTEvent
java.util.EventObject
java.lang.Object

/*ActionEvent Class constructor */
ActionEvent(java.lang.object source, int id,java.lang.String command):Constructs an ActionEvent object.
ActionEvent(java.lang.object source, int id,java.lang.String command,int modifiers):Constructs an ActionEvent object with modifier key.
ActionEvent(java.lang.object source, int id,java.lang.String command,long when,int modifier):Constructs an ActionEvent object with the specified modifier and timestamp.


/* ActionEvent class method */
java.lang.String getActionCommand() : Returns the command string associated with this action.
int getModifiers(): Returns the modifier keys held down this action event.
long getWhen() : Returns the timestamp of when this event occurred.
java.lang.String paramString() : Returns a parameter string identifying this action event.

 

Key Event

Key event generated when we enter any character. There are 3 types of key events presented by an integer constant.

  • EY_PRESSED
  • KEY _REPASED
  • KEY_TYPED
     
public class keyEvent extends InputEvent

 

Keyevent class inherits method from classes:

java.awt.event.InputEvent
java.awt.event.AWTEvent
java.awt.event.ComponentEvent
java.util.EventObject
java.lang.Object
 

/* KeyEvent class constructors */
keyEvent(component source, int id, long when, int modifiers, int keyCode)
keyEvent(component source, int id, long when, int modifiers, int keyCode, char keychar)
keyEvent(component source, int id, long when, int modifiers, int keyCode, char keychar, int keyLocation)

/*keyevent class Methods */
char getkeyChar() : Returns the character associated with the key in this event.
int getkeyCode() : Returns the integer keyCode associated with the key in this event.
int getkeyLocation() : Returns the location of the key that originated this key event.
static string getkeyModifiersText(int modifiers): Returns a string describing the modifier key(s), such as "shift", or "Ctrl+Shift".
static string getkeyText(int keycode): Returns a string describing the  keycode, such as "HOME", "F1" or "A".
boolean isActionKey() : Returns whether the key in this event is an "action" key.
string paramString() : Returns a parameter string identifying this event.
void setKeyChar(char keyChar) : set the keyChar value to indicate a logical character.
void setKeyCode(int keyCode) : set the keyCode value to indicate a physical key.
void setModifiers(int modifiers) : Deprecated. as of JDK1.1.4

 

MouseEvent

  • Mouse action that is occurred in component known as mouse event. These event is get generated by the component object for mouse motion event & mouse events
  • Mouse key is pressed & released
  • Mouse key is clicked
  • The cursor of mouse key enter & exit the unobscured part of component geometry
  • Mouse key is moved
  • Mouse key is dragged
public class MouseEvent extends InputEvent

 

Mouse Event class inherits the method from classes:

  • java.awt.AWTEvent
  • java.awt.event.Input Event
  • java.awt.event.ComponentEvent
  • java.util.EventObject
  • java.lang.Object


 

/* Mouse Event class method */
int getButton() : Returns which, f any, of the mouse buttons has changed state.
int getClickCount() : Returns the number of mouse clicks associated with this event.
point getLocalOnScreen() : Returns the absolute x,y position of the event.
static string getMouseModifiersText(int modifiers) : returns a string describing the modifier keys and mouse button.
point getPoint() :Returns the x,y position of the event relative to the source component.
int getx() : returns the horizontal x position of the event relative to the source component.
int getXOnScreen() : Returns the absolute horizontal x position of the event.
int gety() : Returns the vertical y position of the event relative to the source component.
int getYOnScreen() : Returns the absolutevertical y position of the event.
boolean ispopupTrigger() : returns whether or not this mouse event is the popup menu trigger event for the platfrom.
String paramString() : Returns a parameter string identifying this event.
void translatePoint(int x,int y) : Translates the event's coordinates to a new position by adding specified x and y offsets.

 

Text Event

When we enter character or text in textfield or in a text area, is called a text event. The instance of the TextEvent does not include the character.
 

public class TextEvent extends AWTEvent

 

TextEvent class inherits the method from classes: 

  • java.util.EventObject
  • java.awt.AWTEvent
  • java.lng.Object
     

 

/* TextEvent class constructor */
TextEvent(Object source, int id) : Constructs a TextEvent object.

/*TextEvent class Method */
String paramString() : Returns a parameter string identifying this text event.

 

WindowEvent Class

The object in the window Event class represents the change in the state of the window. The low-level event is get generated by the window object when it is opened, activated, closed, deactivated, iconified, or deiconified. When focus gets transferred into or outside of the window.

Declaration of windowEvent class:

public class WindowEvent extends ComponentEvent

 

WindowEvent class inherits method of  below classes:

java.awt.AWTEvent
java.awt.event.ComponentEvent
java.util.EventObject
java.lang.Object

/*WindowEvent class Constryctors */
WindowEvent(Window source, int id): Constructs a WindowEvent object.
WindowEvent(Window source, int id, int oldState, int newState): Constructs a WindowEvent object with the specified previous and new window states.
WindowEvent(Window source, int id, window opposite): Constructs a WindowEvent object with the specified opposite window.
WindowEvent(Window source, int id, window opposite, int oldState, int newState): Constructs a WindowEvent object.
/* windowEvent class Methods */

int getNewState() : For WINDOW_STATE_CHANGED events returns the new state of window.
int getOldState() : For WINDOW_STATE_CHANGED events returns the previous state of window.
Window getOppositeWindow() : Returns the other Window involved in this focus or activation change.
Window getWindow() : Returns the originator of the event.
String paramString() : Returns a parameter string identifying this event.

 

AdjustmentEvent Class

AdjustmentEvent Class represents the adjustment event emitted by the adjustable object.
 

Declaration of AdjustmentEvent class:

public class AdjustmentEvent extends AWTEvent

 

AdjustmentEvent interface inherits method of below classes: 

  • java.awt.AWTEvent
  • java.util.EventObject
  • java.lang.Object

 

/*AdjustmentEvent class Constructors */
AdjustmentEvent(Adjustable Source, int id, int type, int value) : Constructs an AdjustmentEvent object with the specified Adjustable Source, event type, adjustment 

type, and value.

AdjustmentEvent(Adjustable Source, int id, int type, int value, boolean isAdjusting) : Constructs an AdjustmentEvent object with the specified Adjustable source, 

event type, value

/* AdjustmentEvent class methods */
Adjustable getAdjustable() : Returns the Adjustable objust where this event originated.
int getValue() : Returns the type of adjustment which caused the value changed event.
int getValue(): Returns the current value in the adjustment event.
boolean getValueIsAdjusting() : Returns true if this is one of multiple adjustment events.
String ParamString() : Returns a string representing the state of this Events.

 

ComponentEvent Class

ComponentEvent class represent that component moved, changed visibility or changed size.
 

Declaration of ComponentEvent Class:

public class ComponentEvent extends AWTEvent

 

ComponentEvent interface inherits method of below classes :

  •  java.awt.AWTEvent
  • java.lang.Object
  • java.util.EventObject
     

 

/* ComponentEvent class constructors */
ComponentEvent(Component Source, int id): Constructs a ComponentEvent object.

/*ComponentEvent class methods */
Component getComponent() : Returns the originator of the event.
String paramString() : Returns a parameter string identifying this event.

 

ContainerEvent Class

ContainerEvent class represent the container’s changes in content because component might be added or removed.

Declaration of ContainerEvent class:

public class ContainerEvent extends ComponentEvent

 

ContainerEvent class inherits method from below classes :

  • java.awt.AWTEvent
  • java.awt.ComponentEvent
  • java.util.EventObject
  • java.lang.Object
     

 

/*ContainerEvent class Constructor */
ContainerEvent(Component source, int id, Component child) : Constructs a ContainerEvent object
/* ContainerEvent class Methds */
Component getChild() : Returns the Component that was affected by the event.
Container getContainer() : Returns the originato of the event.
String paramString() : Returns a parameter string identifying this event.

 

MouseMotionEvent class

MouseMotionEvent interface indicates a mouse action occurred in the component. The low-level event is generated by the component object when the mouse is moved or dragged.
 

Declaration of MouseMotionEvent class:

public class MouseMotionEvent extends InputEvent

 

The MouseMotionEvent interface inherits method from below classes:

  • java.awt.AWTEvent
  • java.awt.event.InputEvent
  • java.awt.event.ComponentEvent
  • java.util.EventObject
  • java.lang.Object


 

/* MouseMotionEvent interfaace methods */

void mouseDragged(MouseEvent e) : Invoked when a mouse button is pressed on a component and then dragged.
void mouseMoved(MouseEvent e) : Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.

 

PaintEvent Class

The PaintEvent class used to ensure that paint or update method calls are serialized along with the other event that delivered from the event queue.
 

 

Declaration of PaintEvent class:

public class PaintEvent extends ComponentEvent

 

PaintEvent class inherits method from below classes:

  • java.awt.AWTEvent
  • java.awt.ComponentEvent
  • java.util.EventObject
  • java.lang.Object
/*paintEvent class constructor*/
PaintEvent(Component Source, int id, Rectangle updateRect) : Constructs a PaintEvent object with the specified source component and type.

/* PaintEvent class methods */
Rectangle getUpdateRect() : Returns the rectangle representing the area which nedds to be repainted in respone to this event.
String paramString() : Returns a parameter string identifying this event.
void setUpdateRect(Rectangle updateRect) : sets the rectangle representing the area which needs to be repainted in response to this event.

 



Comments