Results 1 to 20 of 30
- 04-28-2010, 01:06 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
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!
- 04-28-2010, 01:10 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
JTextArea (Java 2 Platform SE v1.4.2) try setSelectionStart() and setSelectionEnd()
- 04-28-2010, 01:13 AM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Look at setSelectionStart() and setSelectionEnd() in the API (inherited from JTextComponent).
-Gary-
EDIT: (like iluxa says) :)
- 04-28-2010, 01:43 AM #4
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
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!
- 04-28-2010, 03:06 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
If you need more help post your SSCCE demonstrating the problem.
- 04-28-2010, 10:34 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
You could use a focus listener, so that when the field gets focus it clears itself the first time?
- 04-28-2010, 04:29 PM #7
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
How do I add an focus listener? Do you have any examples?
- 04-28-2010, 04:34 PM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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?
-
- 04-28-2010, 04:35 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Did you look for anything based on what I said?
Maybe, say, a Focus Listener, or some way to add one to a component...
- 04-28-2010, 04:36 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Good lord...that was a bit of a pile-on...poor chap.
- 04-29-2010, 12:27 PM #12
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
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!
- 04-29-2010, 12:31 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
equals().
Not ==.
- 04-29-2010, 01:25 PM #14
along with equals() use trim() also
Ramya:cool:
- 04-29-2010, 02:38 PM #15
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
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!
- 04-29-2010, 02:51 PM #16
trim() - Returns a String value by removal of leading and trailing whitespace .
Please post your code with codetagRamya:cool:
- 04-29-2010, 02:59 PM #17
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
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>
- 04-29-2010, 03:27 PM #18
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
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?
- 04-29-2010, 03:39 PM #19
this has always allowed me to compare, because it is now a string for use, but why not do thisJava Code:if (reset !=1) { String compare=[COLOR="Cyan"](string)[/COLOR]jTextArea1.getText(); if (compare.equals("Enter your text here!")) { jTextArea1.setText(""); reset=1; } }
,
Java Code:if(jTextArea1.getText().equals("Enter your text here!"){ jTextArea1.setText(""); reset=1; } }
- 04-29-2010, 04:12 PM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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).
Similar Threads
-
How to Highlight a String in JTextArea
By ramvaidhya in forum AWT / SwingReplies: 6Last Post: 12-31-2008, 07:23 AM -
How to select/highlight an entire row in JTextPane
By Maladict in forum AWT / SwingReplies: 6Last Post: 08-15-2008, 09:21 AM -
How to highlight text by drag and selection
By Java Tip in forum java.awtReplies: 0Last Post: 06-25-2008, 10:35 AM -
How to select/highlight an entire row in JTextArea
By Valeriano in forum AWT / SwingReplies: 2Last Post: 05-28-2007, 11:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks