The Java Program: SetLAF.java

  1 import java.io.*;
  2 import java.awt.*;
  3 import java.awt.image.*;
  4 import java.awt.event.*;
  5 import javax.swing.*;
  6 import javax.imageio.*;
  7 
  8 public final class SetLAF extends JFrame implements ActionListener {
  9 
 10    void screenShot () {
 11       try {
 12          final String filename = "window.png";
 13          final Point p = getLocationOnScreen();
 14          final Rectangle rectangle = new Rectangle((int)p.getX(), (int)p.getY(), getWidth(), getHeight());
 15          final Robot robot = new Robot();
 16          final BufferedImage image = robot.createScreenCapture (rectangle);
 17          ImageIO.write (image, "png", new File (filename));
 18       } catch (Exception ex) {
 19          ex.printStackTrace (System.err);
 20       }
 21    }
 22 
 23    static class Item {
 24       private final UIManager.LookAndFeelInfo laf;
 25       Item (UIManager.LookAndFeelInfo i) {laf=i;}
 26       void setLAF () {
 27          try {
 28             UIManager.setLookAndFeel (laf.getClassName());
 29          } catch (Exception ex) {
 30             ex.printStackTrace (System.err);
 31          }
 32       }
 33       public String toString () {
 34          return laf.getName();
 35       }
 36    }
 37    final static Item[] items;
 38    static {
 39       UIManager.LookAndFeelInfo[] installedLAF = UIManager.getInstalledLookAndFeels();
 40       items = new Item [installedLAF.length];
 41       for (int i=0; i<installedLAF.length; i++) {
 42          items[i] = new Item (installedLAF[i]);
 43       }
 44    }
 45    private SetLAF () {
 46       super ("Set LAF");
 47       final JPanel panel = new JPanel ();
 48       final JButton button = new JButton ("Take Screen Shot");
 49       button.addActionListener (new ActionListener () {
 50             public void actionPerformed (ActionEvent e) {
 51                screenShot ();               
 52             }
 53          });
 54       final JComboBox combo = new JComboBox (items);
 55       combo.addActionListener(this);
 56       panel.add (button);
 57       panel.add (combo);
 58       panel.setBorder (BorderFactory.createEmptyBorder(10,10,10,10));
 59       getContentPane().add(panel);
 60       setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
 61    }
 62 
 63    public void actionPerformed (ActionEvent e) {
 64       final JComboBox combo = (JComboBox) e.getSource();
 65       final Item item = (Item) combo.getSelectedItem();
 66       item.setLAF();
 67       SwingUtilities.updateComponentTreeUI (this);
 68       pack();
 69    }
 70 
 71    public static void createAndShowGUI () {
 72       JFrame.setDefaultLookAndFeelDecorated(true);
 73       final JFrame frame = new SetLAF ();
 74       frame.pack();
 75       frame.setVisible(true);
 76    }
 77 
 78    public static void main (String[] args) {
 79       SwingUtilities.invokeLater (new Runnable () {
 80           public void run() { createAndShowGUI();}
 81       });
 82    }
 83 }