How can I print a RTF formatted String to a JLabel? - java

I have my custom JComponent which do a lot of drawing operations.
They also include drawStrings for text paragraphs, but now Id like to format the text (in a seperate window with jtextpane or another RTF editor) (bold, font-size, align, font-color, lists) and show this formatted text in my own component.
Id like to do that without bufferedimages because if i have a big jcomponent full with images I would get a overflow error (RAM)
Something like with AttributedString could work, but how?
I don't really know how to get the RTF-Text out of the Jtextpane (or if there is a better editor plz tell me) and draw it in the "paintComponent(Graphics g)" method.
The Editor is just used to format the text easily, not for the presentation (which is done with my own component)
Can anyone help me please?
Thanks for your help,
so i create an AttributeString and then apply the RTF-formattings in it?
do you know how I can extrude the RTF information to an adequate AttributedString? e.g. a function which transforms the formatted text to an AttributedString like
public AttributedString getAttrString(String plainRTF){
...
}
Thanks a lot

Try this custom AdvancedRTFEditorKit
http://java-sl.com/advanced_rtf_editor_kit.html

You can parse the structure of the underlying Document to get to the actual elements. Each Element has a set of attributes you can access with getAttributes().
You can build AttributedString using document.getText() and applying corresponding styles using AttributedString.addAttribute() method.
Then draw the string on Graphics. Check out this tutorial that uses TextLayout to render the AttributedString.
Take a look at styledDocumentToAttributedString() method here. It may be already implements what you need; it depends on the complexity of your string. Also, this discussion may be useful.

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.

Swing: non input text field

I'm new in Swing, and don't want to learn it. I just want to write a simple frame divided into two equal parts: top and bottom. The top part of frame should be a simple immutable text field.
Problem: As I understand, to show text with beautiful font I should use JTextPane. But JTextPane:
Doesn't support vertical text alignment; (I haven't got any desire to write something like that)
I don't know how to switch off editing.
Quedtion I believe there is a simpler solution for my purpose. Is there?
The top part of frame should be a simple immutable text field.
Use a JLabel. It supports HTML which might help with your formatting.
I don't know how to switch off editing.
setEditable( false );

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.

Scroll through JLabel held in JScrollPane

I have a JScrollPane that holds a JLabel using the following code:
//Create TEXT LOG JPanel
textLogPane = new JScrollPane(logLabel);
textLogPane.setPreferredSize(textLogPaneDim);
//textLogPane.setOpaque(true);
textLogPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
textLogPane.getViewport().setBackground(Color.DARK_GRAY);
The JLabel, logLabel, is represented by a string with an HTML encoding using for carriage returns. I display certain images based on the contents of certain lines and I would like to be able to scroll the JScrollPane, textLogPane, to show that line whenever I am displaying that graphic. I know the contents of the line that I want to display but I can't seem to figure out how to get it to scroll down(or up) to the relevant line.
If need be I can change to something other than a JLabel as long as I can keep the HTML encoding and have it look just like multiple lines of text.
Sorry if this is a repeat I tried searching but couldn't find any results.
Thanks
You can do some custom maths and use scrollRectToVisible() in your viewport. I don't know how to compute the rect of a specific line in your JLabel. A better solution would be to stick your strings into a JList instead, perhaps with a custom renderer for the html, and use
list.ensureIndexIsVisible(list.getSelectedIndex());
You don't use "carriage returns" in HTML, you use "br" tags.
I would suggest you should probably be using a JTextPane for multiline text. I also find it easier to not use HTML, but instead to add strings with attributes. You can also insert icons into a JTextPane.
Read the section from the Swing tutorial on Using Text Components for a working example.

Java JTextPane Change Font of Selected Text

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

Categories

Resources