How to render html content in Vaadin TextField - java

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.

Related

Format an editable textarea

At the moment I try to find a method to create a TextArea that I can edit and simply (without cheating) apply syntax highlighting to. Is that possible without the need of a custom Component? I already managed to format a text using a JEditorPane, but I am not sure how to implement that the text is dynamicly highlighted... And that efficiently. Is that possibly without an enormous amount of coding?
You can't use a JTextArea since it does not support text attributes.
Instead you can use a JTextPane. Read the section from the Swing tutorial on Text Component Features for more information and examples.

Insert ICON/PNG inside TextArea

Without resorting to wrapping a GWT TextArea in a separate DIV, how to add a search icon/magnifier icon inside the text area?
No, Not possible.Text area is not a normal Widget.
GWT's TextArea maps directly to a HTML TextArea thats why you can only set text to it. The only thing you can do ist create a Grid/VerticalPanel/HorizontalPanel and put in each cell a separate TextArea.
Google Groups
Similar Question
Use a Composite wrapping a FlowPanel, put a text area and an image inside, then in CSS position the icon absolutely so it's over the text field.

Java apply HTML to a selected text

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)

Replacing smiles with images in Swing text area

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.

Formatting text with HTML in a JEditorPane?

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.

Categories

Resources