1

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

1
  • Is there any particular reason why you tagged JSP? It's a deprecated 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 <f:ajax> and so on. Commented Jan 10, 2013 at 1:59

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank BalusC! I am following you and I will upload images to my server and show it on JSF page by url, right?
You're welcome. Yes, just make them available by URL so that you can just reference it in HTML <img>. In this related answer you can find some hints how to save an uploaded image outside webapp: 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: stackoverflow.com/a/8889096
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.

1 Comment

jfreechart ChartUtilities is an example of methods that can stream the rendered image in a headless environment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.