Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display system icon for a file in SWT?

Tags:

I want to display a file tree similarly to java2s.com 'Create a lazy file tree', but include the actual system icons - especially for folders. SWT does not seem to offer this (Program API does not support folders), so I came up with the following:

public Image getImage(File file)
{
    ImageIcon systemIcon = (ImageIcon) FileSystemView.getFileSystemView().getSystemIcon(file);
    java.awt.Image image = systemIcon.getImage();

    int width = image.getWidth(null);
    int height = image.getHeight(null);
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = bufferedImage.createGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();
    int[] data = ((DataBufferInt) bufferedImage.getData().getDataBuffer()).getData();
    ImageData imageData = new ImageData(width, height, 24, new PaletteData(0xFF0000, 0x00FF00, 0x0000FF));
    imageData.setPixels(0, 0, data.length, data, 0);
    Image swtImage = new Image(this.display, imageData);
    return swtImage;
}

However, the regions that should be transparent are displayed in black. How do I get this working, or is there another approach I should take?

Update:

I think the reason is that PaletteData is not intended for transparency at all.

For now, I fill the BufferedImage with Color.WHITE now, which is an acceptable workaround. Still, I'd like to know the real solution here...