Set caret location to a specific line in a text control - java

I have an input screen where the user types into an SWT Text control. As the user types I take the text, process it and show it in a separate read_only result text control. The length of the result text might be several characters longer than the input text.
I would like to position the resultant text control display so that it always shows the line where the user is currently typing in the input control.
I can get the input caret line using ivTextInput.getCaretLineNumber(), but there is no corresponding method to set the caret line number.
I can set the position of the caret in the result using ivTextResult.setSelection(ivTextInput.getCaretPosition()) but the processing I do on the input text might "push" the result text down to the point where it is not visible without scrolling.
Ideally the result text display would marry-up with the input text display so that what/where the user types always lines up with the result text.
I have tried keeping track of the extra characters the processing adds, but the user can add text anywhere in the text control, and the resulting mess is un-usable, plus the way SWT text control handles new lines throws calculations off.

Text has a setTopIndex method to set the line visible at the top of the control:
public void setTopIndex(int index)

Related

Filling Progress Bar with text entered in TextField in java

How to fill up progress bar by entering the text in textfield. The progress bar should increment double when user enter numbers or special characters. The aim is to set up the strength of password field.
Use a DocumentListener. An event will be generated whenever text is added or removed from the text field.
So then you just need to get the current text and check the entire string for your special characters and reset the value of your progress bar.
Read the section from the Swing tutorial on Listening For Changes on a Document for more information and working examples.
The tutorial also contains a section on How to Use Progress Bars.

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.

Find invisible text in iText

I am creating a PDF document of multiple pages using iText. I am adding some unique text on one of the pages in the middle of this document but making it invisible as-
Chunk chunk = new Chunk("invisible text here");
chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE, 0f, null);
com.lowagie.text.Document iTextDoc.add(new Paragraph(Element.ALIGN_JUSTIFIED, chunk));
The reason for adding this invisible text is to identify this particular page at the time of onEndPage(). But it is failing.
To achieve in the onEndPage(), I have the following code -
boolean b = (pdfWriter.getDirectContent().toString()).contains("invisible text here");
I get the value of b as false.
If I compare any other text on that page(which is visible) results b as true.
I tried to manually search the invisible text in the PDF reader and it finds the text.
What could I modify to achieve this?
It is never a good idea to assume you can recognize text in the content without elaborate parsing. The text may be split into multiple segments, encoding might not be platform's default character encoding, etc... Thus don't try something like
boolean b = (pdfWriter.getDirectContent().toString()).contains("invisible text here");
You can achieve your goal
The reason for adding this invisible text is to identify this particular page at the time of onEndPage().
much more easily. Simply add a member to your PdfPageEvent implementation, i.e. the class with your onEndPage() method, and set it where you used to add the invisible page content to the text you used to add to the page.
Now you can test that member variable directly in your onEndPage(). Don't forget to reset the variable afterwards, preferably in onEndPage() itself!

Java Swt Text (SWT.MULTI) append text without scroll

I have a Java SWT GUI with a multiline Text control. I want to append lines of text to the Text control without affecting the position of the cursor within the text box. In particular, the user should be able to scroll and select text at the top of the Text control while new text lines are appended to the bottom.
Is this possible?
I switched to using a StyleText control to fix flickering issues when adding text. With this control I found the following code fixed the issue of appending text without scrolling to the new location.
textOutput.setRedraw(false);
int scrollP = textOutput.getTopIndex();
Point selectionP = textOutput.getSelection();
textOutput.append(traceText);
textOutput.setSelection(selectionP);
textOutput.setTopIndex(scrollP);
textOutput.setRedraw(true);

Selecting specified text in an HTML formatted JEditorPane

I am displaying text in a Java JEditorPane using HTML to fomrat the text. I am also designing a search function that finds text in the JEditorPane selects the text and then scrolls to it. My problem is creating an algorithim that will actually specify the beginning and ending position for the selection.
If I simply retrieve the text using myeditorpane.getText(), then find the search string in the result, the wrong selection start and end positions are calculated with the wrong text being selected (the tags are throwing the calculation off). I tried removing the html tags by executing a replace all function text.().replaceAll("\<.*?>","") before searching for the text (this replace all removes all text in between the tags) but still the wrong selection points are calculated (although I'm getting close :-)).
Does anyone have an easy way to do this?
Thanks,
Elliott
You probably want to be working with the underlying Document, rather than the raw text, as suggested in this HighlightExample.
You need to find the start location of the text. I guess something like:
int offset = editorPane().getDocument().getText().indexof(...);
Then to scroll you can use:
editorPane.scrollRectToVisible( editorPane.viewToModel(offset) );
Read up on Text and New Lines for more info.

Categories