JLabel get Image

Sp0tlight picture Sp0tlight · Jun 29, 2013 · Viewed 7.4k times · Source

Is there any better way to get the Icon of a JLabel in a container as a BufferedImage whithout multiple casts?

Component[] components = container.getComponents();
BufferedImage image = ((BufferedImage) ((ImageIcon) ((JLabel) components[i]).getIcon()).getImage());

Answer

nook picture nook · Oct 11, 2013

In order to get a buffered image from a JLabel, you do the following (which is what your original answer asked):

Icon icon = label.getIcon();
BufferedImage bi = new BufferedImage(icon.getIconWidth(),
                icon.getIconHeight(),BufferedImage.TYPE_INT_RGB);