Here is how to set notepad icon for JFrame in Swing. Yes, you heard it right. But is it simple. Dead simple. Just see it below.
FileSystemView.getFileSystemView(): This method gets the FileSystemView object which contains the getSystemIcon() method which takes java.io.File as parameter. The file object is corresponded to the notepad.exe. To work with this program, make sure that you have notepad in your system.
setIconImage(i.getImage()): This method takes java.awt.Image which can be obtained using getImage() in javax.swing.ImageIcon class.
Related: How to set default system icons for JFileChooser?
import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.swing.filechooser.*;
class WindowsIconForJFrame extends JFrame
{
public WindowsIconForJFrame()
{
createAndShowGUI();
}
private void createAndShowGUI()
{
setTitle("Windows Icon");
setDefaultCloseOperation(EXIT_ON_CLOSE);
ImageIcon i=(ImageIcon)FileSystemView.getFileSystemView().getSystemIcon(new File(System.getenv("windir")+"\\notepad.exe"));
setIconImage(i.getImage());
setSize(400,400);
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String args[])
{
new WindowsIconForJFrame();
}
}
FileSystemView.getFileSystemView(): This method gets the FileSystemView object which contains the getSystemIcon() method which takes java.io.File as parameter. The file object is corresponded to the notepad.exe. To work with this program, make sure that you have notepad in your system.
setIconImage(i.getImage()): This method takes java.awt.Image which can be obtained using getImage() in javax.swing.ImageIcon class.
Related: How to set default system icons for JFileChooser?
No comments:
Post a Comment