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);
Related
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)
I'm new to javafx and am trying to display large amount of text in textflow. It displays fine, but I just cannot figure out how to delete the text.
So what I'm trying to do is delete all Text nodes from textFlow like so
textFlow.getChildren().removeAll();
But when I do this and add something to textFlow, it shows after the text that was already displayed there. I would like the text that was there removed and show the added text from the beginning of the textflow.
I guess I have to somehow rerender the view of textflow, but I don't know how. So how do I delete everything and add Text anew?
removeAll(...) will remove all the values you pass as parameters: in your case there are none, so it doesn't remove anything. Use
textFlow.getChildren().clear();
Has anyone seen the text box in Evernote that shows list of tags the user has entered? Each tag is sorounded in a circle with a x mark to be able to remove it. Or another example is the very stackoverflow when you put a tag you can see they are in small box rather than a comma separated text.
How can I do this with java swt?
Thanks
I'm trying and failing to understand how to use Java's text editor components to colorize text as you insert it. I don't want or need a fully featured syntax highlighting library.
Basically, I have a JTextField (or some other JText... component), and a list of words. I want any words in the field that appear in the list to be red, and the rest of the words be green. So for example, if "fire" is in the list, "fir" would appear green and "fire" would appear red.
I've tried using a JTextPane and a DefaultStyledDocument, using a KeyListener to go over the text in the document and using AbstractStyledDocument.replace to replace the existing words with versions that have the correct attributes. This didn't do anything. What am I doing wrong?
Neither JTextPane nor JTextField isn't able to present formatted text, i.e text having more than one format. For text-editor-like capabilities like you'd find in WordPad or HTML, the component to use is the JEditorPane or its descendant, JTextPane.
The simplest thing you can do is set the ContentType of the JEditorPane to "text/html" and simply set its text to a string containing HTML. The Java structured text components are surprisingly competent with HTML; you can display tables and/or DIVs, and there is support for much of CSS2. Simplest to do your styles inline, but you can even do external style hrefs.
If you want to get fancy programmatically, you can access the DocumentModel and create text from spans of text each having their own formatting. The DocumentModel works essentially like a programmable text editor.
EDIT: Re-reading your question, I see my answer doesn't quite address it. Since you want multi-colored text JEditorPane is your only option; but rather than just piping in pre-colored text via HTML or such, you'll have to put a listener on your document model to catch changes introduced when you type; and after every document change you'll want to examine the text (again from the Document model) for text that should or should not be highlighted, and you'll want to apply formatting to certain runs of text.
There are devils in the details, but this should get you started.
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.