Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


tarun1
Listeners
1 Answer(s)      6 years and 2 months ago
Posted in : Java Beginners



Advertisement
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
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
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... listeners are registered with the scheduler, and must be given a name. Listeners can be registered as either "global" or "non-global". Global listeners
Action Event Listener
;       Action Listeners can be implemented... event listeners. 1. Using "actionListener" attribute of the component... listeners. processAction method signature: public void
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... listeners. processValueChange method signature: public void
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
Flex Examples
application, how can you handle the event and listeners and many more that you can
hibernate
hibernate  what is hibernate listeners
Java AWT
Java AWT  What interface is extended by AWT event listeners
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
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
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
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 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
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 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
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
javascript event handling examples
event listeners. document.attachEvent('onclick', handleClick); } else
Flex ArrayCollection
. Listeners can be added to listen the event. // Creates
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
 

 

 

DMCA.com