Search code examples
javaimageicon

Java: string to ImageIcon?


Is there anyway to convert a String to an ImageIcon?

Sort of like the code here: http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/Createaniconinmemory.htm

Instead of the red rectangle, I would like to display a String as ImageIcon.

My intention is to display this dynamically created ImageIcon besides the Jtree nodes.


Solution

  • You can use the drawString on Graphics2D object to draw the string to a graphic. Then, its straightforward to build an ImageIcon using the Graphics2D object. For more details on how to draw a string to graphics, look here.