Search code examples
javaswingiconsjlabel

How resize javax.swing.Icon


I try to resize IUManager icon. But i cant do it correct. My code looks like:

   // label
   ErrorDetails = new javax.swing.JLabel();

   // icon
   Icon icon = UIManager.getIcon("OptionPane.informationIcon");

   BufferedImage bi = new BufferedImage(
        205,
        250,
        BufferedImage.SCALE_DEFAULT);
    Graphics2D g = bi.createGraphics();
    g.scale(205,205);

    // paint new graphics
    icon.paintIcon(null,g,250,250);
    g.dispose();

    // set resized UIManage icon
    ErrorDetails.setIcon(icon);

but icon have still same size


Solution

  • You are attempting to paint the Icon onto the BufferedImage. Therefore you would need to create a new Icon using the BufferedImage>

    ImageIcon scaled = new ImageIcon(bi);
    ErrorDetails.setIcon(scaled);
    

    Also follow Java naming conventions. Variable names should NOT start with an upper case character. "ErrorDetails" should be "errorDetails".