Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the icon of another application?

Tags:

java

windows

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!

like image 851
mattbdean Avatar asked May 21 '12 21:05

mattbdean


2 Answers

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));
like image 186
aleroot Avatar answered Oct 13 '22 19:10

aleroot


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);
}
like image 36
Guillaume Polet Avatar answered Oct 13 '22 19:10

Guillaume Polet