How to read an image from text pane? - java

Say a plain text and an image are entered by a user into a text pane. How to separately read the text and the image from such text pane?

If you have HTMLEditorKit set as the default editor kit, then get all the elements of the document. Then iterate through those elements and look for image element. Then separate them from the text elements. Following links may come to your help:
Element to string in HTMLDocument
How to get current html element(tag) in JTextPane?

To get the plain text from a JTextPane, you can use:
myJTextPane.getDocument().getText(...)
To get the image, you need to use StyleConstants.getIcon(...). For instance, to get the image at offset i:
StyleConstants.getIcon(((DefaultStyledDocument)myJTextPane.getDocument()).getCharacterElement(i).getAttributes()))

Related

How can I display the full content rich text in read mode via xp:inputTextarea?

On an xpage I want to display the full content of a rich text field with help of the xp:inputTextarea control. How can I do this?
The content of the rich text is nothing more than text. There is no rich text content in the fields, so no attachments or formatted (html) text.
I have been able to bind the content of the rich text field to a String field in my java object but when I display it on my xpage via an input text area it gets formatted with so many elements that does not represent the content of the rich text field.
Should I apply a convertor to the input textarea control and how should this look like? or can I use somehow the computed text control?
Did you try just changing the display type to html? That is on the first tab.

JEditorPane in html mode cuts last empty space when using setText

I have set my JEditorPane to use the text/html kit. If I try to setText("hey ") for example (note the space after 'hey'), the editor pane displays just "hey", there is no space after. Does somebody know how I can solve this?
Why are you worried about this?
If you want simple unformatted text to be displayed, then use a JTextArea or JTextPane.
A JEditorPane is used to "format" text so it will parse the data and may remove unwanted characters.
If you want to force a space when using HTML then you can use:
editor.setText("hello&nbsp");

How to save styled text in Database and retrieve the same styled text onto JEditor Pane

Can anybody give me any idea, if i can store a text attribute (like bold, italic etc) modified in JEditorPane in MSSQL SERVER Database?
The user should be able to modify the text attribute, then store that in DB and again retrieve them from DB when in need, in the same attribute style.
You have to define proper EditorKit. E.g. HTMLEditorKit will allow you to store text as HTML with all styles info. Just use getText() and setText() to work with formatted content.
ALternatively you can write own Reader/Writer see for exmple http://java-sl.com/editor_kit_tutorial_reader_writer.html

Java - Swing setting colour to text in JTextArea

I have a JTextArea which has its text set to a string of information. In this string of information I have a variable which I would like coloured red, to do this I edit the string as follows:
"Result: <html><font color=red>" + negativeValue + "</font></html>"
I would expect this to give Result: ## where the number is red. However it just puts the following into the text area:
Result: <html><font color=red>##</font></html>
I'm not really sure how to get this working, so could someone offer advice as to how to do so?
JTextArea is not a component designed for styled text. If the text can be all one color, call setForeground(Color).
Otherwise use a styled text component such as a JEditorPane or JTextPane. For more info. on using them, see How to Use Editor Panes and Text Panes.
Also as pointed out by others, the entire String must start with <html> .
You cannot use HTML in a JTextArea, but you can use it with a JEditorPane
JTextArea doesn't support styled text area, in order to render HTML you need an instance of JEditorPane or JTextPane. See the tutorial on Using Text Components
.
Sample code here
If memory serves, JTextArea is for plain text display only. For HTML, you must use JEditorPane or one of its subclasses.
Java renders html code only if it starts with <html>. You should try it like
"<html>Result: <font color=red>" + negativeValue + "</font></html>"

Making words different colors in JTextField/JTextPane/?

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.

Categories