Virtual Position Forum
Please register to watch content in detail
Thanks
Admin virtual position


Join the forum, it's quick and easy

Virtual Position Forum
Please register to watch content in detail
Thanks
Admin virtual position
Virtual Position Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

KeyPress Event Handlers in java

View previous topic View next topic Go down

GMT + 3 Hours KeyPress Event Handlers in java

Post by sharmila farooqi Sun May 29, 2011 6:00 pm

Sir,
Please explain how to handle various keys pressed. E.g. I want to shift
focus to next “Text Area” if "enter" key is pressed or to invoke a action
listener of “Search” button of address book GUI?
avatar
sharmila farooqi
Monstars
Monstars

Posts : 35
Join date : 2011-05-29

Back to top Go down

GMT + 3 Hours Re: KeyPress Event Handlers in java

Post by Vuhelper Sun May 29, 2011 6:02 pm

You can set the different events for the focused of the several components particularly. The generated focus events are performed by the FocusListener of the object used in your application using the addFocusListener() method. The generated event (FocusEvent) is passed to every FocusListener objects that receives such types of events using the addFocusListener() method of the object. The addFocusListener() method is takes the instance of MyFcousListener class.

MyFocusListener:
This is the inner class used in the FocusChange class in which, the focusGained() method has been used to receive the generated event. This method sets the text of the source of the event to the label component. This program displays the three command buttons on the frame. If you select any button of those then the text of that button will be shown on the label.

FocusAdapter:
This is the abstract class is used to receive the keyboard focus event.
Code:

import java.awt.*;
import java.awt.event.*;

public class FocusChange{
Label label;
public static void main(String[] args){
FocusChange fc = new FocusChange();
}

public FocusChange(){
Frame frame = new Frame("RoseIndia.Net");
Panel panel = new Panel();
Button yes = new Button("Yes");
Button no = new Button("No");
Button cancel = new Button("Cancel");
yes.addFocusListener(new MyFocusListener());
no.addFocusListener(new MyFocusListener());
cancel.addFocusListener(new MyFocusListener());
panel.add(yes);
panel.add(no);
panel.add(cancel);
frame.add(panel,BorderLayout.NORTH);
label = new Label();
frame.add(label,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}

public class MyFocusListener extends FocusAdapter{
public void focusGained(FocusEvent fe){
Button bt = (Button)fe.getSource();
String str = bt.getLabel();
label.setText(str);
}
}
}
avatar
Vuhelper
Deep Bench
Deep Bench

Posts : 97
Join date : 2011-05-29

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

Permissions in this forum:
You cannot reply to topics in this forum