I wanted to know if there is a way that I can apply basic HTML to a text pane or text editor using a Highlighter. My objective is to basically do a text editor using basic HTML. For example a user highlights a part of some text and clicks on a button and I want that to become bold for example, using html but without showing tags or anything, just the selected text in bold. Also I wanted to know if it is better to use a textpane for this or a editorpane and why. Thank you.
anyway, you have to set a HTMLDocument to your JTextPane. Then, you can use the features of HTMLEditorKit and HTMLDocument to achieve what you want. For instance you can use :
insertHTML(jtp.getDocument(), jtp.getSelectionStart(), "<b>"+getSelectedText()+"</b>", 0, 0, HTML.Tag.B);
From the HTMLEditorKit. Something like that (I have not try the code)
Related
What i'm trying to do is really simple: render html tag in TextField or TextArea.
But after googling for a long time i'm surprised to find out that Vaadin seems not to support it.Vaadin ComboBox or ListBox provides .setRender() method to render content in whatever the way i want. But TextField or TextArea do not have functions like that.I wonder is there any way to do this?
Vaadin TextField supports custom theme , but it didn't work when rendering.
textArea.addThemeVariants(TextAreaVariant.MATERIAL_ALWAYS_FLOAT_LABEL);
TextField and TextArea are fields to edit plain text.
If you want to display html content, there is Html component in Flow to do that.
If you want to edit html content there is separate RichTextEditor for doing that.
Also if you allow user to input the html, remember to sanitize the html before displaying it with e.g. Jsoup in order to avoid xss i.e. JavaScript injection issues.
I'm currently developing JTextArea with a Notepad++ like function (currently indentation function finished).
Now I'm trying to add a function which in my JTextArea will show a dotted line for an indentation with same level, and change specific color of a word.
Thanks in advance :)
You will not able to achieve your target with JTextArea as it is a plain text support component.You have to use a Styled text supported JEditorPane or JTextPane.And try to use a HTML document and achieve it.Look at
Styled supported components
and
some more examples
I am designing a chat system..i am making use of JText area to display chat, and JTextField to enter text.
My question is how to recognize smiley's like ":)" and replace it by corresponding image
on text area? i found no method which will append image on text area..Please help.
http://java-sl.com/tip_autoreplace_smiles.html
You'll have to use a read-only JEditorPane to display HTML instead of a JTextArea in that case.
JTextArea was made to display only multiple rows of text, but correct me if I'm wrong. To display Images you could use the JEditorPane control that will allow you to use html, with simple <img /> tags which will point to a image.
Regarding how to recognize smileys you could create a file / list of common patterns that you would like to support and then simple check if the text contains the pattern with the .contains , or even Regular Expressions.
Update
And with the JEditorPane you will also be to do addition things like say scan for emails or links and automatically convert them so the user can click them, always a nice feature to have.
I am trying to make a simple email client in Java Swing.
I want to allow users to format their email in any way they want, like making some parts of the text bold, other parts italic, etc. In other words, I am trying to make a WYSIWYG editor. The formatting is done in HTML. I am using JEditorPane to display the text.
I have tried adding tags myself to the text directly by using setText and getText methods of JEditorPane. I could make it work for basic formatting, but it is quite difficult to handle complex formatting. (trying to remove tags from multi-tagged elements, for example)
Is there an easier way to accomplish this? I have looked at HTMLEditorKit but it seems like it does not support adding tags to and/or replacing a specific string.
Thanks in advance.
The HTMLEditorKit comes with some default Actions that allow you to do some basic styling of the text with the click of a menu item (or button). Take a look at the example in the section from the Swing tutorial on Text Component Features.
I have a JTextPane (or JEditorPane, I can use either no problem). How can I change the font of a selected area to a specific font?
textpane.getSelectedText().setFont() won't work. (Even with font-family)
You can change JTextPane's font only as a whole, it doesn't do rich text.
There's a Document underneath JEditorPane (and apparently JTextPane too), which you get a hold of with getDocument(). You want to cast that to a StyledDocument if you can, and then you can do things like setCharacterAttributes to a given run of characters.
There are some (hopefully) helpful examples in the Java tutorial at http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html .
You can do this by using JTextPane. It is impossible to do this using JTextArea. . Here is a best example on how to use JTextPane.
Sample: http://download.oracle.com/javase/tutorial/uiswing/components/editorpane.html
Code: http://download.oracle.com/javase/tutorial/uiswing/examples/components/TextSamplerDemoProject/src/components/TextSamplerDemo.java