JEditorPane unexpected behavior with new lines - java

I am trying to highlight text in a JEditorPane text field and save the index of the selected text. However I keep running into problems when I save the selections. The JEditorPane seems to save new lines in a different way each time. It will write a "\n", a "\r" and sometimes even a "\r\n" when the user presses the enter key. And sometimes the JEditorPane will even ignore carriage returns which causes wrong selection indices.
Is it possible to get a consistent behavior when the user presses the enter key?

Why are you using a JEditorPane. That is for displaying HTML and HTML generally doesn't have line breaks. You use the "br" tag in HTML. For normal text you should be using either a JTextPane or JTextArea.
I am trying to highlight text
The text in a string depends on the component you are using and whether you get the text from the Document or the component.
In general you should get the text from the Document since the Document only stores the "\n" so you should be able to calculate the offset correctly and highlight the text.
When you get the text from the component, the platform new line String is inserted in the text which would cause your offset problems.
See Text and New Lines for more information.

Related

Jtextpane copy coloured text and paste

I have a JTextPane which displays colored text . I use the followong piece of code to get text from JTextPane.
String temp = pane.getDocument().getText(0,pane.getDocument().getLength());
However when i try to set the temp variable content to pane again,
pane.select(0,pane.getDocument().getLength());
pane.replaceSelection(temp);
by this way i lose the color and get white text. Is there anyway where i can maintain the color of text without copying the content to clipboard.
Please help.
Actually it depends on EditorKit you use. The first part returns text (with styled info) of the selected fragment. E.g. in the RTFEditorKit it would be rtf content of the document's fragment.
The second part is not correct. Replace selection can't process the content properly. I guess in case of the RTFEditorKit it would be all the text with formatting inserted in the pane.
I would use
pane.setText(temp);
instead. If you need to insert the styled fragment use kit.read(...) passing the temp in the call
You can try the Kit as alternative of the default RTFEditorKit and see what happens
UPDATE: Sorry original comment was incorrect a bit. The code should be
pane.getEditorKit().read(
new StringReader(temp),
pane.getDocument(),
pane.getDocument().getLength())

JTextArea get user typed text

I use a JTextArea in which I use setText method to have some text while opening GUI.
Once text area is opened with the text I set, I typed some text, my intention is to get whatever text user types.
dataField = new JTextArea();
dataField.setText("sample#");
..
...
If I type "hello world"
sample#hello world
in text area and press enter I need to get only hello world in a string and not sample#hello world. I have tried with key listeners and
appended the input characters to a string builder but backspace also creates a unreadable character an appends
to it.
Simply put, I need to get user typed text from text area.
sample# is the last line in text area , so what ever i type after the last token i need to get that text.
Read the JTextArea API:
getLineCount() so you know the number of lines
getLineStartOffset() and getLineEndOffset() to know the offsets of the text you need to get from text area. Add 7 to the start since you don't want to get the "sampler#" text.
getText() to get the text using the offsets from above.
String a="sample#"+datafield.getText();
Try this
String old = "sample#";
String s = dataField.getText().subString(old.length());
Good luck

Automatic line break in Java html text editor

I am creating a text editor in java that saves the text as html, but displays as plain text to the user. The user has the ability to change the color, alignment and style (bold and underline) of the text. The entire body of text is stored in the database as html in order to save the style adjustments. I am having an issue with newlines not being saved. So when the user is entering text, presses enter and puts text on a new line, it all gets put on one line after be saved and re-displayed. All the text is just being put inside on paragraph tag without any line breaks. I'm wondering if there's a way to tell the text editor to automatically insert line breaks for new lines?
The way I have my editor set up is a JTextPane using a HTMLEditorKit with content type set to text/html. I am using StyledEditorKit actions for changing the color and style (bold, underline) of the text and StyleConstants.setAlignment for changing the text alignment (I had some issues with the StyledEditorKit.AlignmentAction). Let me know if you need any specific source code.
try encapsulating your text inside an html < p > tag - it should maintain line breaks.
I assume there is a key down event for this kit? Assuming you have a KeyEvent handler you should be able to do something like this:
if(e.getKeyCode() == KeyCodes.KEY_ENTER){
editor.Text += "<br />";
}

Turn fonts to Monospaced

I am trying to make a bizarre text editor for people with reading problems with Netbeans. You load the text you like and the editor starts highlighting it word by word with bold letters. The change from plain to bold constantly change the word dimensions and moves the line. One solution was the Monospaced Font but I would like to add a few more fonts available for the user to choose. Is there any way to do this with Arial for example by giving some orders to the JTextPane?
You can manually split the String with <br/> by counting characters and splitting at the right spot to keep the width under your desired character width. Give some leeway so if you get a big word, it won't still go to the next line.
Alternatively, you could use a JList to display your lines (instead of using <br/>). That way, there's no way the line would split to the next line. However, if you do it that way, the user will click on the list like a list and not be able to select text like in a normal text pane.

In a JTextPane, how can I change the value attribute of an input element?

I have an HTML document being displayed by a JTextPane that works very nicely. I have some buttons that I interact with using ActionListeners for a ButtonModel, and I hook into state links (#foo) to handle simple internal app links.
My problem is that I have a INPUT element that I need to change the "value" of so the text changes in the box.
JTextPane's getText()and setText() methods give you full access to the text displayed by the component. If that happens to be HTML, then the text you're dealing with is a HTML document and you need to change that HTML text just as you would if you were displaying HTML directly.
If you have an INPUT with type="text" or default type, then you display a text field, and its displayed value is controlled by the value= attribute.
To do that, you need to do some in-code text editing of the text value of your JTextPane. You can use IndexOf() to find the offset of your INPUT tag, then again to find the value or the closing angle bracket, and then you insert your desired value at the location you wanted.
When you have your new String all set up, put it back in the JTextPane using setText(). Done.
HTMLDocument has a getElement method for getting the javax.swing.text.Element with a given id attribute. Use this to get your input element, then call
htmlDocument.setOuterHtml(
inputElement,
"<input id=\"foo\" value=\"" + escapeHtml(newValue) + "\">"
)
to replace the value of your inputElement with the new value.
Note, I haven't tried this. Let us know if it works!

Categories

Resources