mattbdean
mattbdean

Reputation: 2552

How to get the icon of another application?

So in this application I'm making, the user clicks on a button, and that button launches a program. This button will have the title of the application and the icon. I just need to know how to get the icon, kind of like this: Windows http://goo.gl/5WjdT

So what I want to know is this:

  1. Is there any REAL way to do this in Java
  2. If so, how would you do it?

Thanks in advance!

Upvotes: 12

Views: 5091

Answers (2)

Guillaume Polet
Guillaume Polet

Reputation: 47608

You can simply use FileSystemView for that purpose:

public static void main(String[] args) {
    Icon icon = FileSystemView.getFileSystemView()
         .getSystemIcon(new File("C:\\Windows\\regedit.exe"));
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JLabel(icon));
    frame.pack();
    frame.setVisible(true);
}

Upvotes: 9

aleroot
aleroot

Reputation: 72626

Do you have to get the icon associated to the exe file of the application ? If so it is possible in java, take a look at this tutorial, that explain several ways of extracting app icon from an executable binary file from a Java application.

Take a look at this code :

String s = "c:/windows/regedit.exe";
File file = new File(s);

// Get metadata and create an icon
sun.awt.shell.ShellFolder sf =
        sun.awt.shell.ShellFolder.getShellFolder(file);
Icon icon = new ImageIcon(sf.getIcon(true));

Upvotes: 9

Related Questions