Listeners

Listeners

View Answers

October 25, 2008 at 11:32 AM

Hi friend,

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyListenerDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("This is the Window Title");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTextField jfield = new JTextField(20);
jfield.addKeyListener(new KeyListener() {

// Handle the key typed event from the text field
public void keyTyped(KeyEvent e) {
displayInfo(e, "KEY TYPED: ");
}
public void keyPressed(KeyEvent e) {
displayInfo(e, "KEY PRESSED: ");
}
public void keyReleased(KeyEvent e) {
displayInfo(e, "KEY RELEASED: ");
}

protected void displayInfo(KeyEvent e, String s) {
String keyString, modString, tmpString, actionString, locationString;

int id = e.getID();
if (id == KeyEvent.KEY_TYPED) {
char c = e.getKeyChar();
keyString = "key character = '" + c + "'";
} else {
int keyCode = e.getKeyCode();
keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")";
}

int modifiers = e.getModifiersEx();
modString = "modifiers = " + modifiers;
tmpString = KeyEvent.getModifiersExText(modifiers);
if (tmpString.length() > 0) {
modString += " (" + tmpString + ")";
} else {
modString += " (no modifiers)";
}

actionString = "action key? ";
if (e.isActionKey()) {
actionString += "YES";
} else {
actionString += "NO";
}

locationString = "key location: ";
int location = e.getKeyLocation();
if (location == KeyEvent.KEY_LOCATION_STANDARD) {
locationString += "standard";
}

else {
locationString += "unknown";
}

System.out.println(keyString);
System.out.println(modString);
System.out.println(actionString);
System.out.println(locationString);
}

});
frame.add(jfield);
frame.setVisible(true);
}
}
--------------------------------------------------

visit for more information.

http://www.roseindia.net/java/example/java/swing/

Thanks









Related Tutorials/Questions & Answers:
Event Listeners
Event Listeners  I want to learn the Event Listeners in Java. Any... is the good tutorial and example of Event Listeners in Java. Please check the tutorial Java - Event Listeners Example in Java Applet. Thanks
Action Listeners
Action Listeners  Please, could someone help me with how to use action listeners I am creating a gui with four buttons. I will like to know how to apply the action listener to these four buttons.   Hello Friend, Try
Advertisements
Event listeners in mxml
Event listeners in mxml  hi.. just tell me about How do you add event listeners in mxml components. Now AS3 components? Please give an example in both MXML and AS3 Thanks  Ans: There are some function
ModuleNotFoundError: No module named 'supervisor-event-listeners'
ModuleNotFoundError: No module named 'supervisor-event-listeners'  Hi...: No module named 'supervisor-event-listeners' How to remove the ModuleNotFoundError: No module named 'supervisor-event-listeners' error? Thanks
adding mouse listeners to drop target
adding mouse listeners to drop target  import java.awt.*; import java.awt.dnd.DnDConstants; import java.awt.dnd.DragGestureRecognizer; import... with adding mouse listeners to "table" which is drop target, to accept drop
Listeners - Java Beginners
Listeners - Java Beginners
Java - Event Listeners Example in Java Applet
Java - Event Listeners Example in Java Applet       Introduction The event... for helping in implementing event listeners are present in the java.awt.event.
Flex KeyboardEvent Listener
listeners. In the example when certain specific keys are pressed the function... are created using conditional statements and with these listeners when ever
TriggerListeners and JobListeners
;    To perform any action you create Listeners objects.... Then during run time listeners are registered with the scheduler, and must be given a name. Listeners can be registered as either "global" or "non-global"
Action Event Listener
;       Action Listeners can be implemented... event listeners. 1. Using "actionListener" attribute of the component... be implemented for the action event listeners. processAction method signature
Event Adapters
Event Adapters       There are some event listeners that have multiple methods to implement. That is some of the listener interfaces contain more than one method. For instance
Flex Add Event Listener example
on it. Event listeners are the methods or functions in which events are added on the flex components or it can be said as event listeners handles the event
Value Change Event Listener
more than one listeners. 1. Using "valueChangeListener" attribute... for the value change event listeners. processValueChange method signature
Enhancing the Test Plan
Enhancing the Test Plan       As you have seen we have added some basic elements to the Test Plan like Sampler and some listeners. Similarly you can add many other elements
hibernate
hibernate  what is hibernate listeners
Different types of event in Java AWT
Flex Examples
application, how can you handle the event and listeners and many more that you can
Java AWT
Java AWT  What interface is extended by AWT event listeners
Swings/awt - Swing AWT
Swings/awt  Hi, how to write action listeners to the Buttons in RichTextEditor tool bar.. thanks in advance...it's Urgent... i am very much new to Swings
validate jtextfield in two different class use keylistener
validate jtextfield in two different class use keylistener  validate jtextfield in java swing will use two class, design part in one class and method calling in one class will use Keylisteners or change listeners
Flex event
: In the targeting phase, Flex invokes the event dispatcher?s listeners. No other nodes on the display list are examined for event listeners. 3. Bubbling phase: In the bubbling phase, Flex examines an event?s ancestors for event listeners. Flex
Java Ivent Handler - Swing AWT
Java Ivent Handler  Is it possible to make two listeners work simultaneously?I used a keyboard listener to detect key press and also a mouse listener to detect different events generated by mouse.But when the mouse is moving
JSF Related
JSF Related  In what way JSF makes the difference between Applets?? After reading this article I understood instead of using Request and response, we are directly dealing with the Listeners... If soo... we can develop the frames
sdf
sdf  In what way JSF makes the difference between Applets?? After reading this article I understood instead of using Request and response, we are directly dealing with the Listeners... If soo... we can develop the frames
JSF
JSF  In what way JSF makes the difference between Applets?? After reading this article I understood instead of using Request and response, we are directly dealing with the Listeners... If soo... we can develop the frames
java - Java Beginners
java  hi all, In Java swings, I created a array of JPanel and added listeners to it to move. These panel has null layout. When i run the application for the first time panel are in fixed position as i given..if i move
Flex event
Flex event  Hi... please give me the answer with example What is event Bubbling? Thanks in advance  Event Bubbling: In the bubbling phase, Flex examines an event?s ancestors for event listeners. Flex starts
Same email-massage is sending twice in online server(tomcat) but working fine in my local server(Tomcat)..
Same email-massage is sending twice in online server(tomcat) but working fine in my local server(Tomcat)..  Sir, I created one project in jsp for a software company. I am using listeners and timer for sending automatic
Flex ArrayCollection
. Listeners can be added to listen the event. // Creates
javascript event handling examples
event listeners. document.attachEvent('onclick', handleClick); } else
Flex event phase detection example
the event, triggering and then re-detecting listeners in reverse order for handling the event again. Event listeners are the functions or the methods that we create in our applications and inside event listeners, we create event associated
JSF Training
Events Listener Action Listeners can be implemented in 2 ways. If you use... but second way permits you to include more than one event listeners.   ... listeners.    Phase Event Listener JSF life-cycle includes six
Event Handling In Java
: Mouse KeyEvent : Keyboard   Event Listeners : Event Listeners.... Listeners listens the event generated by a component. In Java almost all
JAVA - Java Beginners
of code and the action listeners. You will need two panels ? one for the drawing
Help with Temperature program
¢ Next add functionality to the application: Add listeners to all the tool
Using a variable filename - Java Beginners
first you add action listeners with your buttons.Now in the method actionPerformed
Java Quartz Framework
and/or trigger listeners -- a data source for JDBC transactions and  -- plug-ins, thread
java.lang.ClassNotFoundException: com.sun.faces.config.ConfigureListener
org.apache.catalina.core.StandardContext listenerStart SEVERE: Skipped installing application listeners
Find Name of Excel Sheet
; listeners in the HSSFRequest object we can construct an instance..., inputStream). Once we make this call, the listeners that we constructed
Flex adding single event on varied components Example
Flex single event on varied components       In the example below, listeners for a single... listeners in to several components is depicted. listener.mxmlADS_TO_REPLACE_1
Flex KeyboardEvent properties
listeners and event handlers for the keyboard event have to be created and applied first. Now, in the example listeners are created on the flex TextArea control
Core Java Interview Question, Interview Question
interface is extended by AWT event listeners? Answer: All AWT event listeners
Button in Flex
and deselected. ADS_TO_REPLACE_1 Buttons typically use event listeners to perform
Java Swing Undo Redo Operations
undoableEditHappened(UndoableEditEvent e) is called to notify the listeners that the undoable
addEventListner in flex
addEventListner in flex  Hi..... How do you add event listeners in mxml components and AS3 components? Can you give me the example for that so i can clearly understand....ADS_TO_REPLACE_1 Thanks   Ans
Events
as listeners. No event takes place if there is no listener i.e. nothing happens when an event takes place if there is no listener. No matter how many listeners... is that all the listeners are always notified. Moreover, you can also call
Jmeter - Jmeter Tutorials
and some listeners. Similarly you can add many other elements in your Test Plan
Free J2EE Online Training
, Session Management, Filters, Listeners and related things. Through years
J2EE Online Training
Sharing, Http Servlet Package, Session Management, Filters, Listeners

Ads