Showing posts with label applet programming. Show all posts
Showing posts with label applet programming. Show all posts

The keyEvent class in java Programming

Saturday, 17 November 2012
A key event is generated when keyboard input occurs.There are three types of key events ,which are identified by these integer constant

KEY_FIRST
Marks the first integer id for the range of key event ids.
KEY_LAST
Marks the last integer id for the range of key event ids.
KEY_PRESSED
The key pressed event type.
KEY_RELEASED
The key released event type.
KEY_TYPED
The key typed event type.

 

Constructor in KeyEvent

KeyEvent(Component, int, long, int, int)

public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar)
Constructs a KeyEvent object with the specified source component, type, modifiers, and key.
Parameters: source - the object where the event originated

KeyEvent(Component, int, long, int, int, char) 

Constructs a KeyEvent object with the specified source component, type, modifiers, and key.
public KeyEvent(Component source,int id,long when, int modifiers, int keyCode)

KeyEvent Method

getKeyChar()
Returns the character associated with the key in this event.
getKeyCode()
Returns the integer key-code associated with the key in this event.
getKeyModifiersText(int)
Returns a String describing the modifier key(s), such as "Shift", or "Ctrl+Shift".
getKeyText(int)
Returns a String describing the keyCode, such as "HOME", "F1" or "A". isActionKey() Returns whether or not the key in this event is an "action" key, as defined in Event.java.
paramString()
setKeyChar(char)
setKeyCode(int)
setModifiers(int)


CHAR_UNDEFINED,KEY_PRESSED and KEY_RELEASED events which do not map to a valid

Unicode character do not have a defined keyChar.


VK_0
VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39)
VK_1
VK_2
VK_3
VK_4
VK_5
VK_6
VK_7
VK_8
VK_9

VK_A
VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A)
VK_ACCEPT
VK_ADD
VK_ALT
VK_B
VK_BACK_QUOTE
VK_BACK_SLASH
VK_BACK_SPACE
VK_C
VK_CANCEL
VK_CAPS_LOCK
VK_CLEAR
VK_CLOSE_BRACKET
VK_COMMA
VK_CONTROL
VK_CONVERT
VK_D
VK_DECIMAL
VK_DELETE
VK_DIVIDE
VK_DOWN
VK_E
VK_END
VK_ENTER

The ItemEvent class in java programming

An itemEvent is generated when a check or a list item is clicked or when a check able menu item is selected or deselected .There are two types of item events,which are identified by the following integer

DESELECTED
the user deselected an item

SELECTED 
the use selected an item

in addition ItemEvent defines one integer constant ITEM_STATE_CHANGED that signifies a change of state

ItemEvent has this constructor

ItemEvent(ItemSelectable src,int type,Object entry,int state)






getItem()


The getItem() method can be used to obtain a reference to the item that generated an event .Its signature is show here

Object getItem()

getItemSelectable()

The getItemSelectable() method can be used to obtain a reference to the ItemSelectable object that generated an event

ItemSelectable getItemSelectable()

getStateChange()

The getStateChange() method returns the satechange for the event

int getStateChange()

Event class and event listener in java programming

Friday, 16 November 2012
This classes that represent events are at the core of java'sevent hanflimg mechanism.This we begin our study of eventhandling with a tour of the events classes .As you will see ,thet provide a consistent easy to use means of encapsulating events ,At the root of the java events c;ass hierachy is Eventobject ,Which in java.util .It is the super class for all events .

Object getSource()
This method is defined in the Event Object class. It returns the object that originated the event. So if the user has clicked a Push button, the method getSource returns the object corresponding to Push button is generated.
int getID()
This method returns the integer value corresponding to the type of event. For example if the user has clicked on the mouse, the method getID returns the integer defined as MouseEvent.MOUSE_CLICKED.
AWT event classes 
The subclasses of ATW Event can be categorized into two groups - Semantic events and low-level events. Semantic events directly correspond to high level user interactions with a GUI component. Clicking of a button is an example of a semantic event. Following event classes are semantic classes.
ActionEvent
AdjustmentEvent
ItemEvent
TextEvent 
 
Multiple low-level events may get generated for each high level user event. Following event classes are low level event classes.

ComponentEvent
ContainerEvent
FocusEvent
KeyEvent
MouseEvent
PaintEvent
WindowEvent

AdjustmentEvent is generated when the user adjusts the position of a scrollbar. Scrollbar is the only GUI control that receives the AdjustmentEvent.


ItemEvent is generated when an item is selected or deselected. Following components generate ItemEvents.
CheckBox : When the state of the CheckBox is changed.
CheckBoxMenuItem : When the state of a MenuItem is changed.
Choice : When the state of a ChoiceBox is changed.
List : When an item in list is selected or deselected.

TextEvent is generated when the contents of text component are changed. The components that generate TextEvent are TextArea and TextField.
Now I will describe the GUI components corresponding to low level event classes.
FocusEvent : This event is generated when a component gains or looses focus. Focus may be gained by bringing the mouse over a component (or by using the tab key). The component that has the focus receives all user keyboard events. Focus events are generated by objects of Component class and all its subclasses.

KeyEvent and MouseEvent are subclasses of abstract InputEvent class. Both these events are generated by objects of type Component class and its subclasses. The KeyEvent is generated when the user presses or releases a key on the keyboard. The MouseEvent is generated when the user presses the mouse or moves the mouse.

WindowEvent are generated for the Window class and its subclasses. These events are generated if an operation is performed on a window. The operation could be closing of window, opening of window, activating a window etc.

PaintEvent is generated when a component needs to be repainted (for example when an application which is in front is closed and the component needs to be redrawn.) PaintEvents are internally handled by AWT, and cannot (and should not) be handled by you in the application.

ComponentEvent are also generated by objects of type Component class and its subclasses. This event is generated when a component is hidden, shown, moved or resized.

ContainerEvents are generated when a component is added or removed from a container. ComponentEvent and ContainerEvent are handled by AWT and are not normally handled by the user.


Table Event types and corresponding EventSource & EventListener
Event Type Event Source Event Listener interface
ActionEvent Button, List, MenuItem, TextField ActionListener
AdjustmentEvent Scrollbar AdjustmentListener
ItemEvent Choice, Checkbox, CheckboxMenuItem, List ItemListener
TextEvent TextArea, TextField TextListener
ComponentEvent Component ComponentListener
ContainerEvent Container ContainerListener
FocusEvent Component FocusListener
KeyEvent Component KeyListener
MouseEvent Component MouseListener, MouseMotionListener
WindowEvent Window WindowListener


A listener is an object that is notified when an event occur .It has two major requirements .First it must have been registerd with one or meore source to recieve notifications about specific types of events .Second it must implemented methods to recieve and process thes notifications .The methods that recieves and process events are defied in a set of interdaces found in java.awt.event .For example the MouseMotionListener interface defines two methods to recieve notifications whenthe mouse is dragged or moved .Any object may recieve and process notifications when the mouse is dragged or moved .Any object may recieve and process one or both of these events if it provides n implementation of this interface .


Table Event Listener Interfaces and corresponding methods which it defines
Event Listener interface Event Listener Methods
ActionListener actionPerformed(ActionEvent evt)
AdjustmentListener adjustmentValueChanged(AjustmentEvent evt)
ItemListener itemStateChanged(ItemEvent evt)
TextListener textValueChanged(TextEvent evt)
ComponentListener componentHidden(ComponentEvent evt), componentMoved(ComponentEvent evt), componentResized(ComponentEvent evt), componentShown(ComponentEvent evt)
ContainerListener componentAdded(ContainerEvent evt), componentRemoved(ContainerEvent evt)
FocusListener focusGained(FocusEvent evt), focusLost(FocusEvent evt)
KeyListener keyPressed(KeyEvent evt), keyReleased(KeyEvent evt), keyTyped(KeyEvent evt)
MouseListener mouseClicked(MouseEvent evt), mouseEntered(MouseEvent evt), mouseExited(MouseEvent evt), mousePressed(MouseEvent evt), mouseReleased(MouseEvent evt)
MouseMotionListener mouseDragged(MouseEvent evt), mouseMoved(MouseEvent evt)
WindowListener windowActivated(WindowEvent evt), windowClosed(WindowEvent evt), windowClosing(WindowEvent evt), windowDeactivated(WindowEvent evt), windowDeiconified(WindowEvent evt), windowIconified(WindowEvent evt), windowOpened(WindowEvent evt)
Adapter classes 
 
Event adapters facilitate implementing listener interfaces. Many event listener interfaces have more than one event listener methods. For such interfaces, Java technology defines adapter classes. These have empty implementation (stubs) of all the event listener methods defined in the interface they implement. A listener can subclass the adapter and override only stub methods for handling events of interest. The table below lists the low level event listener interfaces and their adapters.

Table Event Listener Interfaces and their corresponding adapter classes. 
 
Event Listener interface Event Listener Adapter
ComponentListener ComponentAdapter
ContainerListener ContainerAdapter
FocusListener FocusAdapter
KeyListener KeyAdapter
MouseListener MouseAdapter
MouseMotionListener MouseMotionAdapter
WindowListener WindowAdapter


 

learn java programming Copyright © 2011-2012 | Powered by appsackel.org