JLabel with HTML inline image - java

I'm trying to create a JLabel with mixed content: text and inline images.
The component later should contain dynamically count of images and text (with dynamically order).
My first steps: Create an JLabel like follows:
JLabel label = new JLabel("<html>wow! <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEgAAABICAMAAABiM0N1AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAFQUExURUxpcbdSB+dzD+yFFQsEAPSHFhwMABkMAAAAAMpkC9l2EspgCoRHCUMiA+mEFPOOFcBpD/CDFPB9FOh+E08nA+99E+h2E9prDrRgDa5fDrVhC7FiD//JOv/LOf/adv/PKv/IKvuqIf/LPf/OMP/qufuxJP/aLP/GNf7CKPqkIP/UK//tw//glvy1J//bgP/fjv/ehvueHf+/M//jnP/ko//mqf28JvmSGP/or//RVf/Xa//uzPr6++fn6JRfEP/TXcN/Ftzb2v25LqhtEvuZG//WZFkgAKBmEX1OColVDMmJGf/gLbt6Ff/OScjJyciEF7JxE3Q8B/7yNGcqAv3pMf/x1quAG/7TQWQ9BUsyBIx4ZL6cJ7GLIcuqJNi0I/3hTOzcLlQ1BHRKHcSKGbSuqP/FTv7PdPjMaebMT6KWiVZBH3pdQdKzT93PK/m/Qfrx4U/NK0MAAAAcdFJOUwAWeucu/iIFEkmxK3NT3/mc9t/NQ9acZYOTkJKEXR2kAAAGSElEQVRYw+2Y61vaSBSHyz2AXBW1bQRRrlHcha1EZRsKpCYpaImwgNy1ICDq//9xz0wCJAUB20/7PPsTA5k5eZ9zzswcZnj37n/9rjRWqxlktWp+C7Kzvel2GUAu9+b2zq/BNOZtj4umWTaOxbI07fJsm9/K0mg9BpqNR6P+qaLROEsbPFrNmzBuRIGnsy/9wQNo0H/JIhiw3GujTDsemgVK8OXhOJD6R1YqcPzwEgQWS3t2TOtwrBYDwmQHnxAkNRW6+TTIIpTBYl3NMbvpuD+YfQgoGApa4CEb9Mdpt3kVR+sCd4L901TqaKIU/B1NblOp034QnHJpl6dHC2GBO0evKgD/4BSEp12WKK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+DfHlGrN3eAus6o6RIqMz1qP54e1cTm3kMivlZJ6He02zpokEJ1OYF+Pq4AxgMD+Aycmog+ePHW6DwgiB0Kdkh3dZPOxvJJZi5VLkioNoo8LXaHeDg0cdH6R084QUG6qTQkhxKzjmEXLJjlygq1GW4YrEIFRu+foAnS8CIHPQUmUoZraB9lKGtua0WsUtPXHpmcoXCl0KhUMRALPSxgFXkuG4eQChD9C4xv/PTo7mEXSq3mWLhyysqIA5a0+BQ3KJfsPdD+Y6h4EKhmzaXA9L3OSFOrluGCoAD020t3LQTG3hWAih/85wD0vfv31QCTpGryBwIzLFBLD4/GD/QQPJhUrfJQXg/1JxCjmvdTDn0B+MrpwiT3kljnwBVvnmWUEjf0AUwuUb3Jj/lOPWvnkYmJOwUoBoVGC55rHK5SquL3VnNQaSPDj9edBKq/IwKGqjZaHdvpphk0O/4qF96jDTpN3RxaflSIcyCx8voelPOYwxyBwr+hn7FcdRktFlov4yipFqdl2s3JS8wP22xGVcfa4mtXXscl3AZJWuKidt3t4h1TseE0ebUwYErKdVMhENXVMcQRue0GYk1z+uE3ua003C2jQFMVjIZg7MsbXfa9IRp7V8QEGrDYnegHxGCWOjHA4fdsvEmjIQyAuu9ZVPnoFmWdug2Le+BYnwjZsrS22wbWDab/tcoUxhBGEHw9uuQ/7r+BdVmcTguphkzAAAAAElFTkSuQmCC"></html>");
If I add the label to a component, it looks like this:
-> missing image!
But if I put the html code from the label inside an .html file and open it with chrome, it looks good:
What am I doing wrong?
Regards,
Florian

Try to add your image with the setIcon() method of Jlabel :
ImageIcon icon = new ImageIcon(YOUR_PATH);
JLabel thumb = new JLabel();
thumb.setIcon(icon);
By replacing YOUR_PATH by the absolute oath of your image.
Hope it will help !
EDIT : (If you really want to use html image tag, Java may encounter some difficulties in finding your image : use this to have a correct path usable by your program (supposing your image is in the current directory) :
URL url = HTMLLabel.class.getResource( "/image.png" );
Then access your image like this :
"...<img src =" + url + "/> ..."

Related

Display HTML text in SWT Label

I would like to display the HTML text in a Java SWT Label.
Below is my code for creating a label.
Label theLabel = new Label(controls,SWT.WRAP);
theLabel.setSize(100,500);
theLable.setText("<html><ol><li>Hello</li><li>welcome</li></ol></html>");
When I run the application as Eclipse Application I get the output as:
<html><ol><li>Hello</li><li>welcome</li></ol></html>
What is the mistake? Why I am not getting the html formatted output in my label? I am using Eclipse plugin with a view.
To show HTML with SWT you will have to use the Browser widget instead.
Browser browser = new Browser( parent, SWT.NONE );
browser.setText( "<html><ol><li>Hello</li><li>welcome</li></ol></html>" );
If you don't mind the extra dependency on org.eclipse.ui.forms you can also use FormText. But be aware that the control does only understand a subset of HTML (<p>, <b>, <li>, <img>, <br>, <span>) to render simple formatted text.

Displaying text in SVG using Java

I am successfully drawing a line in an svg using java. The line tag or name is shown using a GWT label in a div containing the svg. Is there a way to display tag using svg itself? (without using label and div's)
OMSVGTextElement element;
private OMSVGSVGElement svgElement;
svgElement = doc.createSVGSVGElement();
element=doc.createSVGTextElement();
element.getElement().setInnerText("link label"); //the label
svgElement.appendChild(element);
This should do.

How to add icon near text in TextArea in GWT?

I have text area with text (some help tip) and I want to add info icon before text in order it looks like:
icon -----
----------
----------
-----.
*icon: help icon
*---: help text
If it is impossible to add icon to TextArea, maybe there're any other solutions on how to do it (in a label?)?
You can't add an icon to the TextArea. You can show an icon on top of a TextArea in a specified location (by adding both icon and TextArea to a div and using relative positioning), but then it will obscure the text under it as users type it in.
You only need CSS directives :
.yourClass {
background-image : url('yourImage.png');
background-repeat : no-repeat;
padding-left : 30px;
}
.yourClass is the CSS class to set on your TextArea.
30px corresponds to the image width to avoid the text to be positioned on it.

How to render image within an html dynamically?

I am having some html content with images(html+images) embedded inside it on my local drive. When i open it by double clicking on browser directly, i am able to see the content with embedded images in it.
Now i have to render this content on web browser using java apis.I am able to render html content without any issue but while rendering , image is not getting rendered , instead alt text is getting rendered on browser.
I got to know that i can not set two mime types in java code to render image as well as text on same page.I want to know how can i render html content with image inside it so that it can be rendered.
Regards.
Code added:
public void readFile() throws FileNotFoundException{
String lsFilePath = "D:\\alf3.3\\deployment\\target\\";
//String lsFilename = "/WEB-INF/message.properties";
String lsFilename = "spcontent2.html";
File loNewFile = new File(lsFilePath + lsFilename);
FileInputStream loFileInputStream = new FileInputStream(loNewFile);
FileOutputStream loFilOutputStream = new FileOutputStream(loNewFile);
}
It is fine till now but i need to know how can i render an image that is referred to in file mentioned.
For embedding an Image into html source page by adding content id into image source. This is usually practised while sending HTML emails with embedded images
Please refer following document for more info.
http://www.faqs.org/rfcs/rfc2111.html

can JLabel have img tags

I am trying to display a JLabel which has a few lines of text and an image as follows:
String html = "<html> hello </br> <img src = \"/absolute/path/here\" height = \"30\" width =\"40\"/> </html>";
JLabel l = new JLabel(html);
For the image all I get is a broken image, is it possible to nest img tags inside a JLabel?
EDIT:
I want to add multiple images to the JLabel so I don't think the use of an ImageIcon will do here.
Thanks
File f = new File("C:\image.jpg");
jLabel1.setText("<html><img src=\"file:"+f.toString()+"\">");
This works for me. It's simple and gives possibility to put any number of images you want, not just one image icon. It's not working without quotation marks.
For the image all I get is a broken image, is it possible to nest img tags inside a JLabel
It is possible to display image(s) in a JLabel's text. You are getting broken images because the path is not correct. You need to either prefix your path with file: or preferably get java to do it for you with class.getResource("/your/path"). Here is a working example, just insert valid resource paths.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MultipleImagesExample
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JLabel label = new JLabel(
"<html>"
+ "<img src=\""
+ MultipleImagesExample.class.getResource("/resource/path/to/image1")
+ "\">"
+ "<img src=\""
+ MultipleImagesExample.class.getResource("/resource/path/to/image2")
+ "\">"
+ "The text</html>");
frame.add(label, BorderLayout.CENTER);
frame.setBounds(100, 100, 200, 100);
frame.setVisible(true);
}
}
For more complex HTML in java, I recommend xhtmlrenderer.
Unless you're happy with JEditorPane, you're basically looking at a full webbrowser inside of Swing.
Ideally, you would use JWebPane which would be a WebKit view as a Swing component, but it isn't out yet. The most recent information I could find was this blog post.
The DJ project allows embedding the platform's native browser in Swing. It uses Internet Explorer on Windows and XULRunner on Linux. It does not have any support for Mac.
Rather then try to have multiple images on a single JLabel why not simply have many JLabels, each with one image (as uthark described) and then group all the labels together on a single JPanel. That should give you the effect you are looking for with only minimal additional complexity.
Use a JEditorPane to display the HTML. You can change the background, forground, font etc so it looks like a label.
The above approaches don't seem to work anymore.
It seems that you now have to use an actual URI in the img tag.
Things work for me with "<img src=\"" + new File(...).toURI() + "\">".
Embedded images are not supported in the HTML. Thus, you have to use setIcon or supply an ImageIcon to the JLabel constructor; the HTML cannot have an IMG tag.
JLabel imageLabel =
new JLabel(labelText,
new ImageIcon("path/to/image.gif"),
JLabel.CENTER);
In your case you need to use JTextPane to display HTML. See tutorial here

Categories

Resources