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 />";
}
Related
I have a lot of text (more than 10000 words) which I would like to display on my android app. The text contains bullets, paragraphs and line breaks. I want to know how can I display on my app without manual editing. It is not possible to convert every line break to \n and every tab to \t. I have also tried parsing data in HTML and then using Html.fromHtml(getString(R.string.text)). However, the display that I get looks completely unformatted.
This is how I want the text to look like in the app:
HTML tags are easy solutions for simple problems, like making a text bold, italic, or even displaying bullet points. To style text containing HTML tags, call Html.fromHtml method. Under the hood, the HTML format is converted into spans. Please note that the Html class does not support all HTML tags and CSS styles like making the bullet points another color.
val text = "My text <ul><li>bullet one</li><li>bullet two</li></ul>"
txtview.text = Html.fromHtml(text)
Spans allow you to implement multi-style text with finer-grained customization. For example, you can define paragraphs of your text to have a bullet point by applying a BulletSpan. You can customize the gap between the text margin and the bullet and the color of the bullet. Starting with Android P, you can even set the radius of the bullet point. You can also create a custom implementation for the span. Check out the "Create custom spans" section below to find out how.
val spannable = SpannableString("My text \nbullet one\nbullet two")
spannable.setSpan(
BulletPointSpan(gapWidthPx, accentColor),/* start index */ 9, /* end index */ 18,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan(BulletPointSpan(gapWidthPx, accentColor),/* start index */ 20, /* end index */ spannable.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
txtview.text = spannable
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.
I have a display text area in my JavaFX app.
It has several methods for appending strings to it.
What I want is for any newly appended text to have some kind of feedback so the user is more visually aware of the new text being displayed.
Ideally what I would like is one of the following, but am very open to other suggestions:
The appended text is 'typed out' as if someone were typing it into the display text area itself. Or..
Some kind of highlighting of the new text as it appears.
Although again, I'm open to suggestions!
Thank you.
As suggested in Highlighting Strings in JavaFX TextArea, you can select the new text.
Assuming the text area is not editable, and you are only ever appending to the end of the existing text, you can do:
textArea.textProperty().addListener((obs, oldText, newText) -> {
if (newText.length() > oldText.length()) {
Platform.runLater(() ->
textArea.selectRange(oldText.length(), newText.length()));
}
});
If your text area is editable, or you are using it more generally, then you can do something similar each time you append text. For example:
String message = ... ;
textArea.positionCaret(textArea.getLength());
textArea.appendText(message);
textArea.extendSelection(textArea.getLength());
Maybe try to use RichTextArea (https://github.com/TomasMikula/RichTextFX), then is many possibilites to show your text in different ways.
I have textarea which show file data content. I have table view which contains line numbers and when double click on table view I can able to retrieve line number so I need to change the color of particular line number in textarea using JavaFx controller. How to do that? Please share your code. Thanks.
Please provide some code.
With the TextArea UI-Control you can only add color to the whole containing text.
For your purpose you should use HTMLEditor to style your text content individually.
See also: Using JavaFX UI Controls - HTML Editor
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())