Sponsored Links


Results 1 to 2 of 2

Thread: KeyPress Event Handlers in java

  1. #1
    Senior Member saneha's Avatar
    Join Date
    Apr 2011
    Posts
    266

    30 KeyPress Event Handlers in java

    Sponsored Links1


    Sir,

    Sponsored Links

    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?

  2. #2
    Administrator Vuhelper's Avatar
    Join Date
    Apr 2011
    Posts
    9,578
    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.

    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);
    }
    }
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. EID ul Adha Is An Event When We Share Our Joys
    By viki in forum Eid Celebration
    Replies: 0
    Last Post: 08-25-2014, 11:39 PM
  2. Replies: 0
    Last Post: 06-23-2011, 11:41 PM
  3. Replies: 1
    Last Post: 04-28-2011, 08:55 PM
  4. Event Handling in java
    By saneha in forum Java forum
    Replies: 1
    Last Post: 04-24-2011, 09:36 PM
  5. Register event generator in java
    By saneha in forum Java forum
    Replies: 1
    Last Post: 04-24-2011, 07:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
-: Vuhelp Disclaimer :-
None of the files shown here are hosted or transmitted by this server. The links are provided solely by this site's users. The administrator's or staff of Vuhelp.net cannot be held responsible for what its users post, or any other actions of its users. You may not use this site to distribute or download any material when you do not have the legal rights to do so. It is your own responsibility to adhere to these terms. If you have any doubts about legality of content or you have any suspicions, feel free to contact us.
Online Education | JhelumSoft