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 3 of 3

Thread: Simple Question (hopefully)

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Simple Question (hopefully)

    I have searched for a little while ('cause i find that before my thread is answered sometimes i find the answer either on my own or from Google or something) for how to set the background text for a JTextField. I have found results for how to do it on javascript... though that's not what i want. i have also looked around how to use TextFields in Oracle's tutorials.

    What i mean by background text (dont read if you know what i'm talking about):

    Say i have a TextField, and before i type anything, theres text already there(not the default text you can set when u initiate it), and when i click the cursor that text is gone....

    any help appreciated


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Simple Question (hopefully)

    How I would do this:

    Start with the JTextField already containing your background text.

    Add a focus listener to the JTextField, and when someone focuses on the JTextField, clear the default text and allow the user to type as they want.

    Some things to keep in mind:

    1. Do you want the default text re-displayed if the JTextField loses focus and doesn't contain any text? In this case, you need to add the reverse operation (insert the default text when the JTextField loses focus).

    2. Use an external boolean flag to determine if the JTextField really is just displaying the background text, not actual text the user has typed in which matches the background text

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    KILL3RTACO (October 11th, 2011)

  4. #3
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Simple Question (hopefully)

    Much thanks. I like how you explain in english. It helps understand what I need to do instead of trying to figure out what the code does so I can replicate it.

    +thanks

Similar Threads

  1. Simple I/O Question...well could be simple for you?
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 30th, 2011, 06:44 AM
  2. Simple question
    By white97 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 11th, 2011, 06:30 PM
  3. Help with a simple question
    By allea in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 28th, 2011, 07:46 AM
  4. Just a simple question
    By newbie in forum Java Theory & Questions
    Replies: 10
    Last Post: December 6th, 2010, 08:19 PM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM