Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: JTextField ActionListener

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default JTextField ActionListener

    Dear forum,

    I am currently working on a code that will allow an employee to add an integer value to one of many text fields.

    I have a list of text fields that indicate times (5 AM - 5 PM) i have set them to not editable. How ever i would like to have a new frame pop up when a user clicks on the JTextField... at the moment if you add a normal actionlistener such as the button ones it will only execute the code if the enter key is pressed.

    And no i do not wish to replace them with buttons as the design will look terrible if so. So in the short run i need to know how to execute data if the JTextField is clicked and not the enter key pressed.


  2. #2
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: JTextField ActionListener

    I'm curious how to do this as well. I'd like it if you could just auto-update simply because new text was put into the field at all.

  3. #3
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: JTextField ActionListener

    So I checked my book and it looks like you can do a KeyListener for the frame..

    import java.awt.event.*;
     
    addKeyListener( this );
     
     
    public void keyPressed(KeyEvent event)
    {
    line1 =String.Format("%s", KeyEvent.getKeyText(event.getKeyCode()));
    setLines2and3(event);
    }
     
    also event.getKeyChar()

    yup just a bunch of random code I know, but maybe you get the idea. I'll try it more in the morning.

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: JTextField ActionListener

    im pretty sure for the auto update it wouldnt be actionlistener. u'd have to implement keylistener and give that a try

  5. #5
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: JTextField ActionListener

    Wouldn't that work for what your trying to do?

  6. #6
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: JTextField ActionListener

    nah i need the code to execute if somebody clicks on the text field. not presses a key

  7. #7
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: JTextField ActionListener

    well with a mouse listener you can't use event.isMetaDown() or event.isAltDown()? or whatever to tell if they are clicking on the field? or even use cordinates to make sure if they click in the right spot? You don't need buttons for that.

  8. #8
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: JTextField ActionListener

    i think im going to add a new button at the bottom to add the schedualed data.. I believe it will be much easier for the moment cheers for trying though.

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JTextField ActionListener

    How ever i would like to have a new frame pop up when a user clicks on the JTextField
    A MouseListener would allow you to listen for any sort of mouse click and act accordingly.

    How to Write a Mouse Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

  10. The Following User Says Thank You to copeg For This Useful Post:

    macko (May 30th, 2011)

  11. #10
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: JTextField ActionListener

    thanks mate that seemed to work " why oh why didnt i think of that @! lol "

Similar Threads

  1. drawString and JTextField
    By that_guy in forum AWT / Java Swing
    Replies: 9
    Last Post: January 29th, 2011, 02:43 AM
  2. Jtextfield Validation
    By nimishalex in forum AWT / Java Swing
    Replies: 8
    Last Post: December 11th, 2010, 02:42 AM
  3. JTextField clearing problem
    By rushhour in forum AWT / Java Swing
    Replies: 1
    Last Post: October 24th, 2010, 12:55 PM
  4. Get a certain line in a JTextField
    By FlamingDrake in forum Java Theory & Questions
    Replies: 2
    Last Post: May 14th, 2010, 03:21 PM
  5. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 07:37 AM