KeyPress Event Handlers in java
Virtual Position Forum :: Members Lounge :: Sciences & Knowledge :: Programing and Design :: Java Forum
Page 1 of 1 • Share
KeyPress Event Handlers in java
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?
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?
sharmila farooqi- Monstars
- Posts : 35
Join date : 2011-05-29
Re: KeyPress Event Handlers in java
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.
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);
}
}
}
Vuhelper- Deep Bench
- Posts : 97
Join date : 2011-05-29
Similar topics
» Event Handling in java
» Register event generator in java
» Java Tutorials For Beginners (From Basic to Advanced Level) Read more: Java Tutorial
» What is difference b/w java Buzzwords and java Keywords?
» Java is to Write Once Run Anywhere thats what java language
» Register event generator in java
» Java Tutorials For Beginners (From Basic to Advanced Level) Read more: Java Tutorial
» What is difference b/w java Buzzwords and java Keywords?
» Java is to Write Once Run Anywhere thats what java language
Virtual Position Forum :: Members Lounge :: Sciences & Knowledge :: Programing and Design :: Java Forum
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Mon Nov 25, 2024 6:33 am by ali001
» Ellipsis News: Stay Informed
Sat Nov 23, 2024 2:01 pm by ali001
» Goorevi App
Thu Nov 21, 2024 6:50 pm by ali001
» AMERICA EARNS! - Gift Card App
Mon Nov 18, 2024 11:07 am by ali001
» Kanba - Manage your Tasks
Thu Nov 14, 2024 12:21 pm by ali001
» Hemangiom'App
Tue Nov 05, 2024 11:25 am by ali001
» MindfulMe - Mental Health App
Mon Nov 04, 2024 10:50 am by ali001
» Learn Candlestick Patterns
Tue Oct 15, 2024 5:51 am by ali001
» Woh Pagal Si Episode 52 to 62 - Top Pakistani Drama
Sat Sep 21, 2024 6:26 pm by Mir Emmad Ali Khan Domki