Friday, August 2, 2013

KeyListener

KeyListener is an interface to receive key events that are generated by TextField, TextArea, and other components when a key is pressed, released, or typed.

A class that wants to process the key events has to implement the KeyListener interface or extends the KeyAdapter class. Then listener is registered with the source component by using the addKeyListener(KeyListener listener) method. You need to override the three methods of the KeyListener interface. These methods are keyPressed(KeyEvent e), keyReleased(KeyEvent e), and keyTyped(KeyEvent e). If you extend the KeyAdapter class, you need to override only methods of interest.

This is a very simple dictionary program. For the user interface, we need one TextField, one List, and one TextArea.  The data for this example are stored in the HashMap object, termsmap. Each entry of the HashMap contains key term, and its translation text. When the program firstly opens, all key terms are read from the HashMap to place in the list box,  lsterms. The action to be performed when the use types the text in the text field is comparing this text with each key term in the listbox. The code of this action is written in the keyReleased method. When part of the key term matches the text input, the entire key term is selected in the list box and its translation text stored in the HashMap is retrieved to display in the text area.

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.List;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.TreeMap;
import java.util.Iterator;
import java.util.Set;
class Show extends Frame implements KeyListener{
TextField txtsearch;
List lstterms;
TextArea txtarea;
TreeMap<String,String> termsmap;
Show(String title){
setTitle(title);
setSize(new Dimension(500,300));
setLayout(new BorderLayout());
setVisible(true);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
txtsearch=new TextField(20);
txtsearch.addKeyListener(this);
lstterms=new List(15,false);
txtarea=new TextArea(10,50);
txtarea.setEditable(false);
add(txtsearch,BorderLayout.NORTH);
add(lstterms,BorderLayout.CENTER);
add(txtarea,BorderLayout.EAST);
init();
txtsearch.requestFocus();
validate();
}
public void keyPressed(KeyEvent e){


}
public void keyReleased(KeyEvent e){
selectItem(txtsearch.getText());

}

public void keyTyped(KeyEvent e){


}

public void selectItem(String textosearch){
for(int i=0;i<lstterms.getItemCount();i++){
if(textosearch.length()<=lstterms.getItem(i).length()){
String st=lstterms.getItem(i).substring(0,textosearch.length()).toLowerCase();
if(textosearch.toLowerCase().equals(st)){
lstterms.select(i);
showTran(lstterms.getItem(i));
break;
}
}

}
}

public void showTran(String key){
txtarea.setText(termsmap.get(key));
}

public void init(){
 //add sample key terms and their translation text to the HashMap
termsmap=new TreeMap<String,String>();
termsmap.put("Abandon","verb 1 to leave sb 2 to stop doing sth");
termsmap.put("Abate","verb to become less strong.");
termsmap.put("Abbreviation","noun a short form of a word or phrase");
termsmap.put("Baa","noun the sound that a sheep makes");
termsmap.put("Baby","noun 1 a very young child 2. a very yourng animal or bird");
//Read data from the HashMap to place in the List
Set<String> s=termsmap.keySet();
Iterator<String> iterator=s.iterator();
while(iterator.hasNext()){
String item=iterator.next();
lstterms.add(item);

}

}


}
public class KeyListenerTest {
public static void main(String[] args){
new Show("KeyListener");

}
}

keylistener key event

No comments:

Post a Comment