ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Hacking Swing with Undocumented Classes and Properties
Pages: 1, 2

Get Large File Icons

Not to leave Windows lonely, here is an undocumented class that will let you obtain large desktop icons and other file information. The FileSystemView only provides access to file icons of a "default" size, which usually means 16 by 16 pixels. If you look at your operating system desktop, however, you may see icons that are much bigger, with more detail. There is no standardized way of getting to the larger icons, but on Windows you can use the hidden class called sun.awt.shell.ShellFolder to retrieve larger (32 by 32) desktop file icons. This class is only available in Sun's JVM, so it won't work with other vendors, or on other platforms.



The code below will take a filename and show the file's large icon in a window.


public class LargeIconTest {

    public static void main(String[] args) throws Exception {
        // Create a File instance of an existing file
        File file = new File(args[0]);
        
        // Get metadata and create an icon
        sun.awt.shell.ShellFolder sf =
                sun.awt.shell.ShellFolder.getShellFolder(file);
        Icon icon = new ImageIcon(sf.getIcon(true));
        System.out.println("type = " + sf.getFolderType());
    
        // show the icon
        JLabel label = new JLabel(icon);
        JFrame frame = new JFrame();
        frame.getContentPane().add(label);
        frame.pack();
        frame.show();
    
    }
}

ShellFolder is a wrapper for the metadata of the selected file. From this object, you can retrieve both the icon and a text description of the file's type. If you run this on an MP3 file, it might print the string "type = MPEG Layer 3 Audio" and show a 32 by 32 pixel iTunes MP3 icon.

Change the Look and Feel of an App from the Command Line

Sometimes, Swing applications don't provide a way to change a look and feel at runtime. When using an application like this, you can override the default look and feel with your own setting from the command line using the swing.defaultlaf property.

java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel 
   myapp.MainClass

You can also use this technique to apply a new look and feel that the original developer never thought of.

Hacks for the Metal Look and Feel

There are a variety of undocumented properties that control the look of Metal, Swing's cross-platform look and feel. JTree nodes can have lines connecting children to their parent, but depending on your current setup, the lines may or may not be showing. You can control the lines with a client property called JTree.lineStyle. Add this code after you create your JTree.

// show the lines in a JTree
tree.putClientProperty("JTree.lineStyle", "Angled");
// hide the lines in a jtree
tree.putClientProperty("JTree.lineStyle", "None");

One of the big complaints about Metal is that the menus and labels use bold fonts. With another quick undocumented system property, you can turn that off.

java -Dswing.boldMetal=false myapp.MainClass

You can also turn on the rollover for JToolBar buttons by using a secret property. A rollover is useful because it gives the user visual feedback that the mouse cursor is over the right place. Given the size of the typical toolbar button, this feedback is essential.

toolbar.putClientProperty("JToolBar.isRollover",Boolean.TRUE);

There is a longer (though, by no means comprehensive) list of properties on this wiki page. Swing, and Metal in particular, has lots of undocumented system properties. As you discover them, please add your own comments to the wiki page. Also remember that these are undocumented for a reason, and could easily change or go away in the future. Use them at your own risk.

Conclusion

Swing is a powerful toolkit with lots of hidden features that you can use to bring out the best in your application. This article documents just a few interesting techniques. Swing Hacks from O'Reilly covers another 100 hacks to improve your software. None of the techniques I've shown are required features, but they can add a level of polish that will make your apps stand out from the rest. And in a crowded software marketplace, anything that makes your program better than the competition is a good thing.


In June 2005, O'Reilly Media, Inc., released Swing Hacks.

Joshua Marinacci is a blogger and co-author of "Swing Hacks" and "Building Mobile Apps with Java" for O'Reilly.


Return to ONJava.com.