DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    penny Guest

    get press enter event


    Hi,

    i have a JTextField instance,
    i enter some texts in the component.
    i want to know how get the "press enter" event.
    it is very convenience to do it vb or vc.
    but how to do in java.
    please give me a example...

    Really thanks

  2. #2
    Tom Duffy Guest

    Re: get press enter event


    Hello Penny:

    Hitting enter is the default ActionPerformed event for a textfield. I usually
    use inner classes to generate events but there are a number of ways to do
    this. Just create a class that implements ActionListener and then implement
    it for the textfield:

    class MyAction implements java.awt.event.ActionListener{
    public void actionPerformed(java.awt.event.ActionEvent event){
    Object object = event.getSource();
    if(object == MyTextField){
    MyTextField_actionPerformed(event);
    }
    }
    }

    Then, register the event with an instance of your event class:
    MyAction aMyAction = new MyAction();
    MyTextField.addActionListener(aMyAction);

    Finally, create the MyTextField_actionPerformed() event to actually do the
    work:

    void MyTextField_actionPerformed(java.awt.event.ActionEvent event){
    //do some useful stuff
    }

    Many IDEs generate something like this for you. I use Visual Cafe and this
    is a more generic example of the stuff that Cafe will generate for you.

    Hope this helps,

    Tom Duffy





    "penny" <penny336@sinaman.com> wrote:
    >
    >Hi,
    >
    >i have a JTextField instance,
    >i enter some texts in the component.
    >i want to know how get the "press enter" event.
    >it is very convenience to do it vb or vc.
    >but how to do in java.
    >please give me a example...
    >
    >Really thanks



  3. #3
    penny Guest

    Re: get press enter event


    Oh really??
    i just use jdk and textpad so i don't know.......
    thanks

    Alex
    "Tom Duffy" <td4729@hotmail.com> wrote:
    >
    >Hello Penny:
    >
    >Hitting enter is the default ActionPerformed event for a textfield. I usually
    >use inner classes to generate events but there are a number of ways to do
    >this. Just create a class that implements ActionListener and then implement
    >it for the textfield:
    >
    >class MyAction implements java.awt.event.ActionListener{
    > public void actionPerformed(java.awt.event.ActionEvent event){
    > Object object = event.getSource();
    > if(object == MyTextField){
    > MyTextField_actionPerformed(event);
    > }
    > }
    >}
    >
    >Then, register the event with an instance of your event class:
    >MyAction aMyAction = new MyAction();
    >MyTextField.addActionListener(aMyAction);
    >
    >Finally, create the MyTextField_actionPerformed() event to actually do the
    >work:
    >
    >void MyTextField_actionPerformed(java.awt.event.ActionEvent event){
    > //do some useful stuff
    >}
    >
    >Many IDEs generate something like this for you. I use Visual Cafe and this
    >is a more generic example of the stuff that Cafe will generate for you.
    >
    >Hope this helps,
    >
    >Tom Duffy
    >
    >
    >
    >
    >
    >"penny" <penny336@sinaman.com> wrote:
    >>
    >>Hi,
    >>
    >>i have a JTextField instance,
    >>i enter some texts in the component.
    >>i want to know how get the "press enter" event.
    >>it is very convenience to do it vb or vc.
    >>but how to do in java.
    >>please give me a example...
    >>
    >>Really thanks

    >



  4. #4
    Andrew Guest

    Re: get press enter event


    "penny" <penny336@sinaman.com> wrote:
    >
    >Hi,
    >
    >i have a JTextField instance,
    >i enter some texts in the component.
    >i want to know how get the "press enter" event.
    >it is very convenience to do it vb or vc.
    >but how to do in java.
    >please give me a example...
    >
    >Really thanks


    Add this listener to your JTextField:

    JTextField text = new JTextField();
    text.addKeyListener(new KeyListener());

    ...

    public class KeyListener extends KeyAdapter {

    public void keyPressed(KeyEvent evt) {
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {

    // whatever you want to happen when ENTER is pressed
    System.out.println("press enter");
    }
    }

    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links