1

I'm studying JSF 2.0 and I have an ImageIcon in a class. Can I show this ImageIcon to JSF page?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Is there any particular reason why you tagged JSP? It's a [deprecated](http://docs.oracle.com/javaee/6/tutorial/doc/giepx.html) view technology since JSF 2.0 and succeeded by Facelets (XHTML). I hope that it's just ignorance and that you're actually not using it? Otherwise you miss a lot of nice JSF 2.0 features such as `` and so on. – BalusC Jan 10 '13 at 01:59

2 Answers2

3

There's some major conceptual misunderstanding going on.

Java/JSF runs on webserver and produces HTML. HTML runs on webbrowser and can reference CSS/JS/image files by (relative) URLs in <link>, <script> and <img> elements. ImageIcon is part of Swing and has nothing to do with this all.

Swing does not produce any HTML at all. When you still attempt to run it "the Swing way" in a Java class which is invoked by a HTTP request, such as a JSF backing bean class or even a "plain vanilla" servlet class, then it would only be shown to the screen which is attached to the physical machine where the webserver runs, not the one where the webbrowser runs (which is exactly where HTML runs).

Ultimately, you need to show the icon by normal HTML means. That is, using the <img> element, or perhaps on a block element with a CSS background-image property. Either way, they have to point a valid URL which returns an image (that is, when the enduser enters exaclty that URL in the browser's addressbar, it should see the image in its entirety). Normally, this is to be achieved by placing the image file in the public webcontent (there where you also store your JSF (XHTML/JSP) pages.

In JSF means, you can procude a HTML <img> element using <h:graphicImage> component. Provided the following webcontent folder structure

WebContent
 |-- META-INF
 |-- WEB-INF
 |-- resources
 |    `-- images
 |         `-- icon.png
 `-- index.xhtml

and the following component in index.xhtml

<h:graphicImage name="images/icon.png" />

then the enduser should be able to see the desired image by just opening index.xhtml.

An alternative is to create a Swing based applet or webstart (JNLP) application which displays the image as an ImageIcon and finally embed it via HTML <applet> or <object> element in the webpage. But that's plain clumsy.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank BalusC! I am following you and I will upload images to my server and show it on JSF page by url, right? – Phạm Ngọc Kháng Jan 10 '13 at 03:07
  • You're welcome. Yes, just make them available by URL so that you can just reference it in HTML ``. In this related answer you can find some hints how to save an uploaded image outside webapp: http://stackoverflow.com/a/14214223 and in this related answer you can find an explanation why you should save uploaded images outside webapp and how to serve it back by URL: http://stackoverflow.com/a/8889096 – BalusC Jan 10 '13 at 03:09
2

Short of rendering it to an image on the server side, I'd say an applet would be the only other way to get an ImageIcon into a web page.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    [tag:jfreechart] [`ChartUtilities`](http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/ChartUtilities.html) is an example of methods that can stream the rendered image in a headless environment. – trashgod Jan 10 '13 at 01:43