AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Text Selection

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
eNc^
Guest





PostPosted: Wed Jun 14, 2006 3:52 am    Post subject: Text Selection Reply with quote



Hi i'm trying to make a button's action method perform a 'delete' on
some text in a JTextArea(). The idea is that I do this

textArea.setText(textArea.getText(0,textArea.getSelectionStart) + "" +
textArea.getText(textArea.getSelectionEnd,textArea.getDocument.getLength());

Basically if the user has seleted text in a paragraph, and the user
pushes this button (delete). The function will then remove the selected
text from the document.

The code which (above) has been shown by me doesn't work (gives
outOfBoundsException)and I would like to know if there is a way to
remove selected text from a JTextArea on the push of the button.
Back to top
Atif
Guest





PostPosted: Wed Jun 14, 2006 7:10 am    Post subject: Re: Text Selection Reply with quote



eNc^ wrote:
Quote:
Hi i'm trying to make a button's action method perform a 'delete' on
some text in a JTextArea(). The idea is that I do this

textArea.setText(textArea.getText(0,textArea.getSelectionStart) + "" +
textArea.getText(textArea.getSelectionEnd,textArea.getDocument.getLength());

Basically if the user has seleted text in a paragraph, and the user
pushes this button (delete). The function will then remove the selected
text from the document.

The code which (above) has been shown by me doesn't work (gives
outOfBoundsException)and I would like to know if there is a way to
remove selected text from a JTextArea on the push of the button.

took me a while to figure out, next time post a small code snippet to
work off!

remember it's " getText(offset, LENGTH) "

textArea.getDocument.getLength() gives you the length of the ENTIRE
document. you only want the length of the string after the Selection
end. that's why you were getting out of bounds errors, because you
wanted the offset plus the length of the entire document, so that's
obviously out of bounds.

do this:

<code>
int a = textArea.getSelectionStart() ;
int b = textArea.getSelectionEnd();
int c = textArea.getDocument().getLength();

String beforeS = textArea.getText(0,a ) ;
String afterS = textArea.getText(b,c-b );

textArea.setText(null);
textArea.setText(beforeS+""+afterS);
</code>

lesson learned: read parameters carefully Smile. 'cause it would've been
correct if the 2nd parameter was asking for the 2nd offset.
Back to top
eNc^
Guest





PostPosted: Thu Jun 15, 2006 3:42 am    Post subject: Re: Text Selection Reply with quote



Thank you so much, it worked.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.