Java TextArea isSelected() question!!

TJN23

Golden Member
May 4, 2002
1,670
0
0
:);):(:eek::disgust::D:|:Q:|

i need to know how to test if some text in a JTextArea is selected or not...wondering why the isSelected() method doesn't work

JTextArea textArea = new JTextArea( 10, 15 );
//I type some text on it

if( theText.isSelected() )
String selectedText = getSelectedText();
else
String theText = textArea.getText();

WHY WON'T THIS WORK?!

Thanks
 

Gooberlx2

Lifer
May 4, 2001
15,381
6
91
ummm...looking at the 1.3 API I don't even see a .isSelected() method. Atleast not for JTextArea, nor any of its parent classes. Where are you finding this method? Or did you make it?
 

Gooberlx2

Lifer
May 4, 2001
15,381
6
91
since i do see the getSelectedText() you could always try

if( !( textArea.getSelectedText() ).equals(null) ){
selectedText = textArea.getSelectedText();
}
else{
theText = textArea.getText();
}
 

TJN23

Golden Member
May 4, 2002
1,670
0
0
thanks for the help, i will try that...i know there is a isSelected() method but it very well may not work for JTextAreas, however i know it should work with a JList as I am looking at it right now in the API specification.

Will let ya know how it turns out

thanks
 

TJN23

Golden Member
May 4, 2002
1,670
0
0
no such luck...getSelectedText() is a method that returns a string, but can also return a boolean value if it equals null, which means that none of the text is selected...therein lies my problem, i can't get it to distinguish between whether or not the text is selected...

i can use textArea.getText() and textArea.getSelectedText(), but i still cant get java to tell whether or not it is selected

anyone got any bright ideas!!??

thanks
 

Gooberlx2

Lifer
May 4, 2001
15,381
6
91
so what was your code in the end? I'm curious if I was right...or rather how wrong I was.
 

TJN23

Golden Member
May 4, 2002
1,670
0
0
code in the end

if( textArea.getSelectedText() == null )
{
outStream.write( textArea.getText() );
outStream.close();
}
else
{
outStream.write( textArea.getSelectedText() );
outStream.close();
}

u were on the money with checking the textArea.getSelected() for null, but i believe you accidently said the following:

if (the text is not selected)
write the selected text
else
write the whole text

WHEN IT SHOULD HAVE BEEN:

if (the text is not selected)
write the whole text
else
write the selected text


thanks though you just helped me big time! :D