I have this Desktop email client program that I'm currently working on in Java and I want to show the incoming mail content inside a JEditorPane.
I've created an HTMLEditorKit and set it as an editor kit for the JEditorPane control like this:
editorPane.setEditorKit(htmlEditorKit);
And for some messages, it works fine but for some others, it shows its background as a blue-colored one. I tried to add a stylesheet rule to the kit, but it didn't work.
What could have been the problem?
Any comments will be welcome.
EDIT:
JEditorPane txtContent = new JEditorPane();
txtContent.setEditable(false);
txtContent.setBackground(Color.WHITE);
txtContent.setBounds(316, 110, 598, 545);
kit = new HTMLEditorKit();
txtContent.setEditorKit(kit);
HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
txtContent.setDocument(doc);
txtContent.setText("HTML content here.....");
When I put "HTML content here....." part inside an html file with Notepad, it shows the right thing.
But when I put the exact same content inside the JEditorPane like the above, it shows different styling(blue background).
Related
I wanted to create a scrollable jtextpane with some html text. Inside, there are some image elements. If I open the file with a browser the image show up correctly and everything is fine.
My problem is that I cant just simply modify the text with some java code because its located inside a properties file, so Im kind of forced to get the right location path.
Does someone know a trick to still use the html text with an properties file with working images?
text=<html>
<head></head>
<body>
<h3>Seer:</h3>
<img src="icons/roles/seer_icon.png" align="left" height="64"/>
</body>
</html>
Because src="icons/roles/seer_icon.png" does not start with http: or https:, it is a relative URL. A relative URL’s true location depends on the URL which acts as the base context for the HTML document.
If you load HTML content from a URL using setPage, the base is that URL. But if you load from a String, as you’re probably doing since you are getting the content from a properties file, there is no base URL. Which means src="icons/roles/seer_icon.png" makes no sense—there is no base to resolve it against.
But you can specify the base yourself:
HTMLDocument doc = (HTMLDocument) textPane.getStyledDocument();
doc.setBase(MyApplication.class.getResource("/"));
Note that HTML may not be the best way to show content, unless you need to give the user the ability to copy and paste it. You can always create the content yourself:
// Properties file contains:
// text=Seer:
// icon=/icons/roles/seer_icon.png
String headingText = properties.getProperty("text");
String iconPath = properties.getProperty("icon");
JLabel headingLabel = new JLabel(headingText);
Font font = headingLabel.getFont();
font = font.deriveFont(24f);
headingLabel.setFont(font);
headingLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 12, 0));
Icon icon = new ImageIcon(MyApplication.class.getResource(iconPath));
JLabel iconLabel = new JLabel(icon);
JPanel content = new JPanel(new BorderLayout());
content.add(headingLabel, BorderLayout.PAGE_START);
content.add(iconLabel, BorderLayout.CENTER);
I would like to get a JEditorPane to display RTF images like microsoft wordpad. I have a simple program I got from an example off of the web. I created a demo file in wordpad with all of the styles wordpad supports. Strikethrough, super-script, sub-script and highlight are not being read or displayed when reading the file. Highlight would be nice other than that I don't really need the others. The part that is critical is the images in the RTF, they show in wordpad but on in java. I am using:
RTFEditorKit rtf = new RTFEditorKit();
JEditorPane editor = new JEditorPane();
editor.setEditorKit(rtf);
rtf.read(fi, editor.getDocument(), 0);
If RTFEditorKit cannot support images is there a editor kit that can? I can't be the only person to try to get an RTF to work properly. I cannot use an HTML editor kit because I need the images to be embeded like the RTF format.
I'm building a small conversation agent, where the text looks like follows:
I would like to set the System's text to always be red. The text is all placed in a JTextPane.
How can I accomplish this? I have tried doing the following:
agentTextPane.setForeground(Color.red); after the system's text is added, and then switching back to black, however that changes all the text in the JTextPane.
This is how the system's text is added:
//'output' is a stringBuilder
output.append("\nSystem: ").append(tempOutput).append("\n");
agentTextPane.setText(output.toString());
As shown here, you can define an attribute set representing a desired style. For example,
StyledDocument doc = (StyledDocument) jtp.getDocument();
SimpleAttributeSet system = new SimpleAttributeSet();
StyleConstants.setFontFamily(system, "Serif");
StyleConstants.setForeground(system, Color.red);
doc.insertString(doc.getLength(), "...", system);
The styles can be progressive, as shown here.
See Text Component Features for more examples.
You may want to use HTML tags to format your Strings in terms of colour. The following reference may be useful. setting JTextPane to content type HTML and using string builders
output.append("<font color=\"red\">");
output.append("\nSystem: ").append(tempOutput).append("\n");
output.append("</font>");
Basically I have a file called FlowLayout.html and I want to read it into the JEditorPane. I have all the code up to that point but I don't know how to open the file in the pane.
JEditorPane jtfInfo = new JEditorPane();
jtfInfo.setEditable(false);
p3.add(jtfInfo);
public void getFlowLayout(){
jftInfo.setContentType("text/html");
//I dont know this step and setContentType gives me an error in netbeans Cannot find symbol
}
You can just call the setPage() method with URL pointing at the FlowLayout.html file:
public void getFlowLayout(){
jftInfo.setPage(new URL("path to file"));
}
From the Java Tutorial: How to Use Editor Panes and Text Panes:
"The setPage method opens the resource pointed to by the URL and figures out the format of the text (which is HTML in the example). If the text format is known, the editor pane initializes itself with the text found at the URL. A standard editor pane can understand plain text, HTML, and RTF. Note that the page might be loaded asynchronously, which keeps the GUI responsive but means that you should not count on the data being completely loaded after the call to setPage returns."
How can I open an HTML page and show it in a text area? (If I choose the HTML file with the JFileChooser, how can I open that page and show it in the text area?)
URL url = new URL(String s);
JEditorPane pane = JEditorPane(url);
But how can I find the link of the HTML file for inserting as s, here!?
A TextArea is for displaying/editing text, not for showing formatted HTML.
JEditorPane supports HTML markup, but only a rather limited subset.
For full HTML support, you're going to need third-party components. Look at the answers to this question for links.
Format the HTML with a <pre> tag
I guess you can use any properly formatted URL a browser would use e.g.
http://stackoverflow.com/questions/1239454/how-can-i-open-an-html-page-and-show-it-in-a-text-area
But then again Java is very keen on security and you might not be allowed to use certain URLs in your environment.
And like Michael Borgwardt said - the JEditorPane's support of HTML is very limited and some tags (i think <div> is one of them) as well as JavaScript are not supported.
For implementation of a simple browser have a look at this
JEditorPane Tutorial