Moving Text Cursor in TextArea Java - java

Is there possible to move text cursor in TextArea (or other text component)? How? is there a function to do that?
Assume, we have a TextArea and 2 button. Each time we press the first button, the text cursor will go to the position between second letter and third letter. If only two letter, the text cursor will go to the end of string. Each time we press the second button, the text cursor will go to the beginning of string.
Could anybody help me? Thx before :D

Is there possible to move text cursor in TextArea
Its called a caret in Java. Check out the API for methods with caret in the method name. For example:
textArea.setCaretPosition( textArea.getCaretPosition() + 1 );

Related

Android get focused word from edittext

Lets say I have an Android edittext with sentence: "I like Stackoverflow very much". Then I use my keyboard to be focused on the word Stackoverflow and now I click the button in my app that adds / removes for example bold effect for this specific word.
Is it possible to do that?
Thank you in advance.
if you are talking about position of cursor then use
int cursorPosition = et.getSelectionStart();
after taking position of cursor you should take whole text and apply desired effect. for example split text by white characters (spaces, enters), calculate on which word cursor is pointing and add/remove bold spannable
note that et.getText() returns Editable, not just usual String. So you can do some work on this class, e.g. check if word with cursor inside is already bold or other spannable applied

Eclipse, use template to surround all the lines in the selection with something

I'm using eclipse for java development.
I have something like this:
text
text
text
text
and I want to change it in something like this:
(some text)text(some text)
(some text)text(some text)
(some text)text(some text)
(some text)text(some text)
I can easly do that using templates on a single row, but there is a way to do the same thing for all the rows, in one shot? Is it possible?
Use block selection mode as the search key is same and has constant width.
Keep the caret on start of first text word. Press Shift + Ctrl + A. Notice a long blinking caret. copy replace key word (some text) to clipboard. Press Ctrl + V to paste or just type the characters. Repeat this after keeping the caret at the end of the first text word.
Use Find and replace dialog and regular expressing to do it.
Select the range of text then Press Ctrl + F. Make sure in the scope group selected lines is selected. In Find text box enter \b(text).* in Replace with text box enter (some text)$0(some text)

How to know the line number when you select on line on a JTextArea? [JAVA]

I have done a GUI in Java with a JTextArea. It is filled with the content of a file.
When I select words with the mouse on the textarea, a new frame pops up on which I do some operations on the selected words. To do these operations, I need to know the line number of the selected text...
Does someone know how to get the line number?
(I look to some methods on the classes JTextArea and MouseListener, but i dont know how to do that...)
Thanks ;)
Check out the Text Utilities. The getLineAtCaret() method is close to what you need. It uses the offset of the caret to get the line number. In you case you will need to use the start offset of the selected text.

String-based cursor positioning

How do I locate the cursor position in a Java textarea where the text is held in a StringBuffer? I am trying to implement the backspace function in a text editor. The API lists how to set data. I need to get the cursor position, to know at which character position the cursor is, not the onscreen global coordinates of the cursor.
Any assistance is sincerely appreciated.
You have to write KeyListeners for keypress events in the TextArea and keep a virtual cursor of where you are in the TextArea. Then map that offset into your StringBuffer, there is no magic way to do it.
If you are using a Swing JTextArea, use JTextArea.getCaretPosition().
If you are using an AWT TextArea, see Jarrod Roberson's answer.

Scrolling TextView in a ScrollView to a specific substring of text

For starters, I'm a C++/Qt Developer jumping into Android/Java development, so please don't assume I know anything. :) So, I have an application with a TextView contained with in a ScrollView. The text contained in the TextView is the results of a web query from my web server application. Depending on the query, the text can be pretty long. I want to implement a search feature on the text where the user enters some text string to search for, and if the text string is found, the ScrollView will scroll the TextView to ensure the entered substring is visible, and I want the text highlighted. I think I know how to highlight the text substring, I know how to tell the ScrollView to scroll to a specific line number in the TextView, but for the life of me, I can't figure out how to find out what line number to scroll to to guarantee my substring is visible. Ie, I want to query the text view to give me the line number that the substring first occurs at. Eventually, I want to also implement a find next that goes to the next occurrence of the substring. Kind of like the Find feature in Firefox. Any assistance is greatly appreciated. Thanks in advance.
You need to find the position of the substring in the text yourself, e.g. with String.indexOf(). Once you know that position, you can look up the correct line in the TextView using the Layout.getLineForOffset() function:
int offset=myString.indexOf("search string");
int line = myTextView.getLayout().getLineForOffset(offset);

Categories