jtextpane html image doesnt load - java

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);

Related

eclipse plugin: add image to browser

I have created an Eclipse plugin which displays a browser with a URL as follows:
browser.setBounds(0, 0, 100, 100);
browser.setUrl("https://www.stackoverflow.com/");
Also I am able to get the animated image from the bundle as follows:
Image image = ImageDescriptor.createFromURL(FileLocator.find(bundle, new Path("icons/sample.gif"), null)).createImage();
How could I add the Image to the browser on run time execution?
Since you're setting the Browser widget to display a URL, the widget will display the contents of that URL.
If you want to display your own content, you'll have to use the setText() method.
Perhaps you can create html that includes the URL you want in an IFRAME and compose the rest of the html with your own content?
As for the image, I'm sure there are ways to include the image in the constructed HTML ... or you can base64 encode the data and include it in the constructed HTML. See Can I embed a .png image into an html page?

The HTML content inside JEditorPane has blue background in Java

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).

How I use a custom font with the Batik SVG library?

I'm working on a Java program which creates templates for clothes. The user enters the word they want to see on the item of clothing and the system creates a PDF template. To create the template I create an SVG document programatically then use Batik to transcode the SVG to the PDF format.
My client now wants to be able to use custom fonts to create the templates. I was wondering if it's possible to use fonts like a TTF with the Batik transcoder? If so how do you go about setting up the SVG?
First you need to transform the font file from TTF to SVG with batik's ttf2svg, once you have the converted file you have to add reference in the 'defs' section of your SVG document.
this is how i did it:
Element defs = doc.createElementNS(svgNS, "defs");
Element fontface = doc.createElementNS(svgNS, "font-face");
fontface.setAttributeNS(null, "font-family", "DroidSansRegular");
Element fontfacesrc = doc.createElementNS(svgNS, "font-face-src");
Element fontfaceuri = doc.createElementNS(svgNS, "font-face-uri");
fontfaceuri.setAttributeNS(svgNS, "xlink:href", "fonts/DroidSans-webfont.svg#DroidSansRegular");
Element fontfaceformat = doc.createElementNS(svgNS, "font-face-format");
fontfaceformat.setAttributeNS(svgNS, "string", "svg");
fontfaceuri.appendChild(fontfaceformat);
fontfacesrc.appendChild(fontfaceuri);
fontface.appendChild(fontfacesrc);
defs.appendChild(fontface);
svgRoot.appendChild(defs);
when creating the text element set the font family just like any other font
Element txtElem = doc.createElementNS(svgNS, "text");
txtElem.setAttributeNS(svgNS, "style", "font-family:DroidSansRegular;font-size:" + fontsize + ";stroke:#000000;#fill:#00ff00;");
txtElem.setTextContent("some text");
svgRoot.appendChild(txtElem);
i got the info from this article: http://frabru.de/c.php/article/SVGFonts-usage hope it helps.
Just add the following element as a child of <svg/>: <style type="text/css"> Then, similar to HTML...

How do I read an HTML file in a JEditorPane?

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."

LWUIT HtmlComponent render local image

I want to render content that i have created locally using html component and put image in this html also by putting image in res folder in jar, i tried
<img src='images/down.png'></img>
<img src='res/images/down.png'></img>
<img src='./images/down.png'></img>
but nothing worked, any suggestion?
[EDIT]
here is my code, i have no idea how to implement DocumentRequestHandler that is why i used DefaultDocumentRequestHandler
DocumentRequestHandler handler = new DefaultDocumentRequestHandler();
HTMLComponent component = new HTMLComponent(handler);
component.getStyle().setBorder(Border.createLineBorder(1));
component.getSelectedStyle().setBorder(Border.createLineBorder(1));
component.setBodyText("<div><b>nirmal:</b>" +
"<img src='res://images/down.png' /></div>");
tried res://images/down.png but nor worked
my image is in res/images
You need to explain how you loaded the HTML, images are loaded relatively to the base URL so you need to define the base URL when creating the HTML (its implicitly detected when loading via URL).
If you created the HTML via setHTML(String) then you need to give absolute paths depending on your DocumentRequestHandler implementation e.g. res://myImage.png or file://myImage.png .

Categories