Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default Is it possible to highlight/select text in a JTextArea

    Hi. I have a JTextArea for people to input text.
    I have a default line in the JTextArea "Enter your text here!", however, what I want to do is to have this text highlighted so when the user clicks on to it all they have to do to get rid of the text is type a single character.

    Is this possible (with simple coding)? I have not been able to find anything on the API.

    Many thanks!

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    JTextArea (Java 2 Platform SE v1.4.2) try setSelectionStart() and setSelectionEnd()

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Look at setSelectionStart() and setSelectionEnd() in the API (inherited from JTextComponent).

    -Gary-

    EDIT: (like iluxa says) :)

  4. #4
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    Hi. I have tried lines like:

    jTextArea1.setText("Enter your text here!");
    jTextArea1.setEditable(true);
    jTextArea1.setLineWrap(true);
    jTextArea1.setWrapStyleWord(true);
    jTextArea1.setSelectionStart(0);
    jTextArea1.setSelectionEnd(21);
    jftInput.setViewportView(jTextArea1);

    However, it does not work! The text appears as normal.

    Have I done anything wrong?

    Many thanks!

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    If you need more help post your SSCCE demonstrating the problem.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    You could use a focus listener, so that when the field gets focus it clears itself the first time?

  7. #7
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    How do I add an focus listener? Do you have any examples?

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    The Swing tutorial has a section on "How to Write a Focus Listener".

    You can also search the forum or the web for existing examples. What search keywords did you use before posting your last question?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by AJArmstron@aol.com View Post
    How do I add an focus listener? Do you have any examples?
    Google does! But the first place I'd (and you should) look would be the Sun Swing tutorials.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    Did you look for anything based on what I said?
    Maybe, say, a Focus Listener, or some way to add one to a component...

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    Good lord...that was a bit of a pile-on...poor chap.

  12. #12
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Cool

    Thank you for your emails. I have looked at the webpages and have seen on Netbeans that it has some FocusGain functions. what I want to do is if the text in the box is at its preset "Enter your text here!" then reset the box to "".

    I am trying by using code along the following lines.

    private void jTextArea1FocusGained(java.awt.event.FocusEvent evt) {

    if (jTextArea1.getText()=="Enter your text here!")
    {
    jTextArea1.setText("");
    System.out.println("HELLOGOOD!!");
    }

    However, the program fails to make the comparison (and will not print out HELLOGOOD). It just does not like the if comparison.

    Does anyone have any ideas?

    Many thanks!

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    equals().
    Not ==.

  14. #14
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    along with equals() use trim() also
    Ramya:cool:

  15. #15
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Smile

    Thank you Tolls and RamyaSivakanth. I was really stuck there! I was also interested to learn about the trim() function.

    This part of the program now works!

    I note that when I enter JTextArea and type in the same original message (Enter your text here!) and then leave the JTextArea and then click back into it the JTextArea the program does not automatically delete the text. Is there a reason why?

    Many thanks!

  16. #16
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    trim() - Returns a String value by removal of leading and trailing whitespace .

    Please post your code with codetag
    Ramya:cool:

  17. #17
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    Hi. This is the code for the listener Gained. I note the program does what I want it to. I was just wondering why the program did not automatically deleted the same text when it was entered into the jTextArea1. It could be that somehow a white space is added.

    <code>
    private void jTextArea1FocusGained(java.awt.event.FocusEvent evt) {
    // This code removes the default text in JTextArea1 when the mouse
    // is clicked in it.

    String compare=jTextArea1.getText();
    if (compare.equals("Enter your text here!"))
    {
    jTextArea1.setText("");
    }
    }
    </code>

  18. #18
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    Now it is myseriously working (I don't know why) - in that the program now does delete the text "Enter your text here!" if one types in the same text.

    I now need to stop this from happening!! Is there a way around this?
    I tried to create a variable (reset) using the below code.

    private void jTextArea1FocusGained(java.awt.event.FocusEvent evt) {
    // This code removes the default text in JTextArea1 when the mouse

    if (reset !=1)
    {
    String compare=jTextArea1.getText();
    if (compare.equals("Enter your text here!"))
    {
    jTextArea1.setText("");
    reset=1;
    }
    }

    I tried creating a variable int reset in other parts of the program (working out where they needed to be). However, when the program reaches the private void jTextArea1FocusGained - the program does not recognise the variable reset as being in existance (despite having initialised it in many other parts of the program). Can anyone help?

  19. #19
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    195
    Rep Power
    5

    Default

    Java Code:
    if (reset !=1)
    {
    String compare=[COLOR="Cyan"](string)[/COLOR]jTextArea1.getText();
    if (compare.equals("Enter your text here!"))
    {
    jTextArea1.setText("");
    reset=1;
    }
    }
    this has always allowed me to compare, because it is now a string for use, but why not do this
    ,
    Java Code:
    if(jTextArea1.getText().equals("Enter your text here!"){
    jTextArea1.setText("");
    reset=1;
    }
    }

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    The reset flag (it should probably be a boolean) should be an attribute of whatever class that focusGained method is in.

    Your code won't compile unless that's visible in any case. I would say don't initialise it "in many other parts of the program". That sounds like a way to break your code (or a sign of flailing round a problem).

Page 1 of 2 12 LastLast

Similar Threads

  1. How to Highlight a String in JTextArea
    By ramvaidhya in forum AWT / Swing
    Replies: 6
    Last Post: 12-31-2008, 08:23 AM
  2. How to select/highlight an entire row in JTextPane
    By Maladict in forum AWT / Swing
    Replies: 6
    Last Post: 08-15-2008, 10:21 AM
  3. How to highlight text by drag and selection
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-25-2008, 11:35 AM
  4. How to select/highlight an entire row in JTextArea
    By Valeriano in forum AWT / Swing
    Replies: 2
    Last Post: 05-28-2007, 12:20 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
  •