Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By sehudson

Thread: JTextField ???

  1. #1
    Nhatmeister is offline Member
    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Default JTextField ???

    Hi All,
    Can someone answer this for me ?

    I have 3 JTextField

    text1
    text2
    text3

    they are all registered to the same addMouseListener. I want to know which text field is click.
    How do I do this in the mouseClicked(MouseEvent e) ?

    Thanks

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: JTextField ???

    e.getSource() ?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Nhatmeister is offline Member
    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Default Re: JTextField ???

    Quote Originally Posted by PhHein View Post
    e.getSource() ?
    If I setup the JTextField as follow:

    JTextField text1 = new JTextField("text1", 30);

    Is there a way for me to get "text1" in mouseClickEvent() ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: JTextField ???

    I moved this thread because there's nothing advanced about it; simply compare the source of the event with the 'text1' reference.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: JTextField ???

    Like phHein said, inside of your mouseClicked() method,you can get the source of the MouseEvent using the getSource() method. This will return to you the Object on which the event occurred. You can then compare this object to the 3 JTextFields that you have added the MouseListener to in order to see which one was the source of the click. That will tell you which JTextField you need to get the text from.

    Since you are adding this MouseListener to a JTextField, and your goal seems to be to get the text from the JTextField
    when the JTextField is clicked, you would need to cast the object returned by the call to getSource() to a JTextField. Then, you can call the getText() method to retrieve the text.
    Last edited by sehudson; 11-22-2013 at 08:05 PM.

  6. #6
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    101
    Rep Power
    0

    Default Re: JTextField ???

    can you set the source with the jtext object?
    Java Code:
    my jtextobject.setSource("name");

  7. #7
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: JTextField ???

    A JTextField does not have a setSource() method, nor does it inherit from any class that does. getSource() and setSource() are defined in the EventObject class. Normally you would only setSource() if you are trying to retarget an event to a child component, but in this example, he could simply call getSource() on the ActionEvent within the mouseClicked method to determine which object(JTextField) the event came from.
    JosAH likes this.

Similar Threads

  1. add a JTextField
    By Sutured in forum New To Java
    Replies: 3
    Last Post: 06-26-2012, 12:51 AM
  2. using only a-z in JTextField
    By JultomteN in forum AWT / Swing
    Replies: 2
    Last Post: 01-09-2012, 08:26 PM
  3. Replies: 3
    Last Post: 11-14-2011, 05:13 AM
  4. Replies: 0
    Last Post: 08-30-2011, 09:18 AM
  5. Replies: 1
    Last Post: 01-30-2009, 06:44 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
  •