Java/Swing JFC/UI

Материал из Java эксперт
Версия от 06:46, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Displays the contents of the UIDefaults hash map for the current look and feel.

  
/*
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.LookAndFeel;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;
/**
 * Simple application which displays the contents of the UIDefaults hash map
 * for the current look and feel.
 *
 * @author aim
 */
public class DefaultsDisplay extends JPanel {
    private static final int rowHeight = 32;
    
    public static void main(String[] args) {
        
        EventQueue.invokeLater(new Runnable() {            
            public void run() {
                JFrame frame = new JFrame("UIDefaults Display");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new DefaultsDisplay());
                frame.pack();
                frame.setVisible(true);
            }
        });        
    }
    
    private String OSXLookAndFeelName; // hack for look-and-feel name mismatch on OS X
    
    private Map<String,String> lookAndFeelsMap; 
    private final Map<String,JComponent> defaultsTablesMap;
    
    private JComboBox lookAndFeelComboBox;
    private JCheckBox onlyVisualsCheckBox;
    private JTabbedPane tabPane;
    
    private RowFilter<UIDefaultsTableModel,Integer> visualsFilter;
    
    
    /** Creates a new instance of DefaultsDisplayer */
    public DefaultsDisplay() {
        defaultsTablesMap = new HashMap<String,JComponent>();    
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            if (System.getProperty("os.name").equals("Mac OS X")) {
                OSXLookAndFeelName = UIManager.getLookAndFeel().getName();
            }
        } catch (Exception ex) {
            // better work :-)
        }
        
        setLayout(new BorderLayout());
                
        JPanel controls = new JPanel();        
        controls.add(createLookAndFeelControl());
        controls.add(createFilterControl());
        add(controls, BorderLayout.NORTH);
                
        tabPane = new JTabbedPane();
        add(tabPane, BorderLayout.CENTER);
        
        addDefaultsTab();
        
    }
    
    protected JComponent createLookAndFeelControl() {
        JPanel panel = new JPanel();
        
        JLabel label = new JLabel("Current Look and Feel");
        lookAndFeelComboBox = new JComboBox();
        label.setLabelFor(lookAndFeelComboBox);
        panel.add(label);
        panel.add(lookAndFeelComboBox);
        
        // Look for toolkit look and feels first
        UIManager.LookAndFeelInfo lookAndFeelInfos[] = UIManager.getInstalledLookAndFeels();
        lookAndFeelsMap = new HashMap<String,String>();
        for(UIManager.LookAndFeelInfo info : lookAndFeelInfos) {
            String name = info.getName();
            // workaround for problem where Info and name property don"t match on OS X
            if (name.equals("Mac OS X")) {
                name = OSXLookAndFeelName;
            }
            // workaround for bug where Nimbus classname is incorrect
            lookAndFeelsMap.put(name, name.equals("Nimbus")? 
                "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" :
                          info.getClassName());
            lookAndFeelComboBox.addItem(name);
        }
        lookAndFeelComboBox.setSelectedItem(UIManager.getLookAndFeel().getName());
        lookAndFeelComboBox.addActionListener(new ChangeLookAndFeelAction());
        
        return panel;
    }
    
    protected JComponent createFilterControl() {
              
        onlyVisualsCheckBox = new JCheckBox("Show Only Visual Defaults");
        onlyVisualsCheckBox.setSelected(true);
        onlyVisualsCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                boolean showOnlyVisuals = onlyVisualsCheckBox.isSelected();
                for(int i = 0; i < tabPane.getTabCount() ; i++) {
                    JScrollPane scrollpane = (JScrollPane)tabPane.getComponentAt(i);
                    JTable table = (JTable)scrollpane.getViewport().getView();
                    TableRowSorter sorter = (TableRowSorter)table.getRowSorter();
                    sorter.setRowFilter(showOnlyVisuals? visualsFilter : null);                    
                }
            }
        });
        return onlyVisualsCheckBox;
    }
    
    protected boolean hasDefaultsTab(String lookAndFeelName) {
        return defaultsTablesMap.get(lookAndFeelName) != null;
    }
    
    protected void addDefaultsTab() {
        LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
        JScrollPane scrollPane = new JScrollPane(createDefaultsTable());
        tabPane.addTab(lookAndFeel.getName() + " Defaults", scrollPane);
        defaultsTablesMap.put(lookAndFeel.getName(), scrollPane);
    }
    
    protected JTable createDefaultsTable() {
        JTable table = new JTable(new UIDefaultsTableModel());
        table.setRowHeight(rowHeight);
        table.setShowHorizontalLines(false);
        table.setShowVerticalLines(false);
        table.setIntercellSpacing(new Dimension(0,0));
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        initFilters(table);
        
        DefaultTableColumnModel columnModel = new DefaultTableColumnModel();
        
        Color rowColors[] = new Color[2];
        rowColors[0] = UIManager.getColor("Table.background");
        rowColors[1] = new Color((int)(rowColors[0].getRed() * .90),
                                 (int)(rowColors[0].getGreen() * .95), 
                                 (int)(rowColors[0].getBlue() * .95));
        
        int width = 0;
        
        TableColumn column = new TableColumn();
        column.setCellRenderer(new KeyRenderer(rowColors));
        column.setModelIndex(UIDefaultsTableModel.KEY_COLUMN);
        column.setHeaderValue("Key");
        column.setPreferredWidth(250);
        columnModel.addColumn(column);
        width += column.getPreferredWidth();
        
        column = new TableColumn();
        column.setCellRenderer(new RowRenderer(rowColors));
        column.setModelIndex(UIDefaultsTableModel.TYPE_COLUMN);
        column.setHeaderValue("Type");
        column.setPreferredWidth(250);
        columnModel.addColumn(column);
        width += column.getPreferredWidth();
        
        column = new TableColumn();
        column.setCellRenderer(new ValueRenderer(rowColors));
        column.setModelIndex(UIDefaultsTableModel.VALUE_COLUMN);
        column.setHeaderValue("Value");
        column.setPreferredWidth(300);
        columnModel.addColumn(column);
        width += column.getPreferredWidth();
        
        table.setColumnModel(columnModel);
        
        table.setPreferredScrollableViewportSize(new Dimension(width, 12 * rowHeight));
        
        return table;
        
    }
    
    protected void initFilters(JTable table) {
        TableRowSorter sorter = new TableRowSorter(table.getModel());
        table.setRowSorter(sorter);
        
        if (visualsFilter == null) {
            visualsFilter = new RowFilter<UIDefaultsTableModel,Integer>() {
                public boolean include(Entry<? extends UIDefaultsTableModel, ? extends Integer> entry) {
                    UIDefaultsTableModel model = entry.getModel();
                    Object defaultsValue = model.getValueAt(entry.getIdentifier().intValue(),
                            UIDefaultsTableModel.VALUE_COLUMN);
                    return defaultsValue instanceof Color ||
                            defaultsValue instanceof Font ||
                            defaultsValue instanceof Icon;
                }
            };
        }
        
        if (onlyVisualsCheckBox.isSelected()) {
            sorter.setRowFilter(visualsFilter);
        }
    }
    
    private class ChangeLookAndFeelAction extends AbstractAction {
        
        public ChangeLookAndFeelAction() {
            super("Change LookAndFeel");
        }
        
        public void actionPerformed(ActionEvent event) {
            String lookAndFeelName = (String)lookAndFeelComboBox.getSelectedItem();
            try {
                UIManager.setLookAndFeel(lookAndFeelsMap.get(lookAndFeelName));
                SwingUtilities.updateComponentTreeUI(DefaultsDisplay.this.getTopLevelAncestor());
                if (!hasDefaultsTab(lookAndFeelName)) {
                    addDefaultsTab();
                }
                tabPane.setSelectedComponent(defaultsTablesMap.get(lookAndFeelName));
            } catch (Exception ex) {
                System.err.println("could not change look and feel to " + lookAndFeelName);
                System.err.println(ex);
            }
        }
    }
    
    private static class UIDefaultsTableModel extends AbstractTableModel {
        private static final int KEY_COLUMN = 0;
        private static final int TYPE_COLUMN = 1;
        private static final int VALUE_COLUMN = 2;
        
        private UIDefaults defaults;
        private List<Object> keys;
        
        public UIDefaultsTableModel() {
            // make a local copy of the defaults table in case the look and feel changes
            defaults = new UIDefaults();
            keys = new ArrayList<Object>();
            UIDefaults realDefaults = UIManager.getDefaults();
            Enumeration keysEnum = realDefaults.keys();
            while (keysEnum.hasMoreElements()) {
                Object key = keysEnum.nextElement();
                if (!defaults.containsKey(key)) {
                    keys.add(key);
                    defaults.put(key, realDefaults.get(key));
                } else {
                    System.out.println("found duplicate key:"+key);
                }
            }
        }
        
        public int getColumnCount() {
            return 3;
        }
        
        public int getRowCount() {
            return defaults.size();
        }
        
        public Class getColumnClass(int column) {
            Class klass = null;
            switch(column) {
                case KEY_COLUMN: {
                    klass = String.class;
                    break;
                }
                case TYPE_COLUMN: {
                    klass = String.class;
                    break;
                }
                case VALUE_COLUMN: {
                    klass = Object.class;
                }
            }
            return klass;
        }
        
        public Object getValueAt(int row, int column) {
            Object value = null;
            switch(column) {
                case KEY_COLUMN: {
                    value = keys.get(row);
                    break;
                }
                case TYPE_COLUMN: {
                    Object key = keys.get(row);
                    Object defaultsValue = defaults.get(key);
                    value = defaultsValue != null? defaultsValue.getClass().getName() : "";
                    break;
                }
                case VALUE_COLUMN: {
                    value = defaults.get(keys.get(row));
                }
            }
            return value;
        }
        
    }
    
    private static class RowRenderer extends JLabel implements TableCellRenderer {
        private final Color[] rowColors;
        private final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); 
        
        public RowRenderer(Color colors[]) {
            rowColors = colors;
        }
        
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            
            setText(value != null? value.toString() : "unknown");
            setIcon(null);
            setColors(table, row, isSelected);
            setBorder(this, hasFocus, isSelected);
            return this;
        }
        
        protected void setColors(JTable table, int row, boolean isSelected) {
            if (!isSelected) {
                setBackground(rowColors[row % rowColors.length]);
                setForeground(table.getForeground());
            } else {
                setBackground(table.getSelectionBackground());
                setForeground(table.getSelectionForeground());
            }
        }
        
        protected void setBorder(JComponent renderer, boolean hasFocus, boolean isSelected) {
        
            if (hasFocus) {
                Border border = null;
                if (isSelected) {
                    border = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
                }
                if (border == null) {
                    border = UIManager.getBorder("Table.focusCellHighlightBorder");
                }
                renderer.setBorder(border);
                                
            } else {
                renderer.setBorder(noFocusBorder);
            }
        }
    }
    
    private static class KeyRenderer extends RowRenderer {
        
        public KeyRenderer(Color colors[]) {
            super(colors);
            setFont(getFont().deriveFont(Font.BOLD));
        }
    } 
    
    private static class ValueRenderer extends RowRenderer {
        private final JButton buttonIconRenderer;
        private final JRadioButton radioIconRenderer;
        private final JMenuItem menuItemIconRenderer;
        private final JCheckBox checkboxIconRenderer;
        
        public ValueRenderer(Color colors[]) {
            super(colors);
            buttonIconRenderer = new JButton();
            buttonIconRenderer.setBorderPainted(false);
            buttonIconRenderer.setContentAreaFilled(false);
            buttonIconRenderer.setText("(for AbstractButtons only)");
            radioIconRenderer = new JRadioButton();
            radioIconRenderer.setBorderPainted(false);
            radioIconRenderer.setText("for JRadioButtons only)");
            checkboxIconRenderer = new JCheckBox();
            checkboxIconRenderer.setBorderPainted(false);
            checkboxIconRenderer.setText("for JCheckBoxes only)");
            menuItemIconRenderer = new JMenuItem();
            menuItemIconRenderer.setBorderPainted(false);
            menuItemIconRenderer.setText("(for JMenuItems only)");
            
        }
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            JComponent renderer = null;
            
            if (value instanceof Icon) {
               Icon icon = (Icon)value;
               // Hack to fix problem where some plaf Icons cast the component to
               // various component classes. yikes!  not very re-usable icons :(
               String className = (String)table.getValueAt(row, UIDefaultsTableModel.TYPE_COLUMN);
               if (className.contains("BasicIconFactory$RadioButtonMenuItemIcon") ||
                   className.contains("BasicIconFactory$CheckBoxMenuItemIcon") ||
                   className.contains("OceanTheme$IFIcon") ||
                   className.contains("MotifIconFactory$RadioButtonIcon") ||
                   className.contains("MotifIconFactory$CheckBoxIcon") ||
                   className.contains("MotifIconFactory$MenuArrowIcon") ||
                   className.contains("WindowsIconFactory$FrameButtonIcon") ||
                   className.contains("WindowsIconFactory$RadioButtonIcon")) {
                   buttonIconRenderer.setIcon(icon);
                   buttonIconRenderer.setSelected(true);
                   renderer = buttonIconRenderer;
                   
               } else if (className.contains("MetalIconFactory$RadioButtonIcon")) {
                   radioIconRenderer.setIcon(icon);
                   renderer = radioIconRenderer;
                   
               } else if (className.contains("MetalIconFactory$RadioButtonMenuItemIcon") ||
                          className.contains("MetalIconFactory$CheckBoxMenuItemIcon") ||
                          className.contains("MetalIconFactory$MenuArrowIcon") ||
                          className.contains("MetalIconFactory$MenuItemArrowIcon")) {
                   menuItemIconRenderer.setIcon(icon);
                   menuItemIconRenderer.setSelected(true);
                   renderer = menuItemIconRenderer;
                   
               } else if (className.contains("MetalIconFactory$CheckBoxIcon") ||
                       className.contains("WindowsIconFactory$CheckBoxIcon")) {
                   checkboxIconRenderer.setIcon(icon);
                   checkboxIconRenderer.setSelected(true);
                   renderer = checkboxIconRenderer;
               } 
            }
            
            if (renderer != null) {
                // special hack renderer for icons needs to be colorized because
                // it doesn"t extend RowRenderer
                setColors(table, row, isSelected);
                setBorder(renderer, hasFocus, isSelected);
            
            } else {
                // renderer == this
                renderer = (JComponent)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if (value instanceof Color) {
                    Color color = (Color)value;
                    float[] hsb = Color.RGBtoHSB(color.getRed(), 
                            color.getGreen(), color.getBlue(), null);
                    
                    setIcon(ColorIcon.getIcon(color));
                    setText("RGB=" + color.getRed() + ","+ color.getGreen() + ","+ color.getBlue() + "  " +
                            "HSB=" + String.format("%.0f%n",hsb[0]*360) + "," + 
                            String.format("%.3f%n",hsb[1]) + "," + 
                            String.format("%.3f%n", hsb[2]));
                    
                } else if (value instanceof Font) {
                    Font font = (Font)value;
                    setFont(font);
                    setText(font.getFontName() + " size="+ font.getSize2D());
                    setIcon(null);
                    
                } else if (value instanceof Icon) {
                    setIcon((Icon)value);
                    setText("");
                }
            }
           return renderer;
        }
            
        @Override
        protected void paintComponent(Graphics g) {
            try {
                super.paintComponent(g);
            } catch (ClassCastException ex) {
                System.out.println("cast exception:" + ex.getMessage());
            }
        }
    }
        
    private static class ColorIcon implements Icon {
        private static HashMap<Color,ColorIcon> icons;
    
        private Color color;
        private int size = DefaultsDisplay.rowHeight - 3;
        
        public static ColorIcon getIcon(Color color) {
            if (icons == null) {
                icons = new HashMap<Color,ColorIcon>();
            }
            ColorIcon icon = icons.get(color);
            if (icon == null) {
                icon = new ColorIcon(color);
                icons.put(color, icon);
            }
            return icon;
        }
    
        protected ColorIcon(Color color) {
            this.color = color;
        }
    
        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.setColor(color);
            g.fillRect(x, y, size, size);
            g.setColor(Color.black);
            g.drawRect(x, y, size, size);
        }        
        
        public int getIconWidth() {
            return size;
        }
        
        public int getIconHeight() {
            return size;
        }        
    }    
}





Get default values for Swing-based user interface

  

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.swing.UIManager;
public class Main {
  public static void main(String[] args) throws Exception {
    Set defaults = UIManager.getLookAndFeelDefaults().entrySet();
    for (Iterator i = defaults.iterator(); i.hasNext();) {
      Map.Entry entry = (Map.Entry) i.next();
      System.out.print(entry.getKey() + " = ");
      System.out.println(entry.getValue());
    }
  }
}





Getting the Default Values for a Look and Feel

  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import javax.swing.Icon;
import javax.swing.InputMap;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.border.Border;
public class Main {
  public static void main(String[] argv) {
    UIDefaults uidefs = UIManager.getLookAndFeelDefaults();
    String[] keys = (String[]) uidefs.keySet().toArray(new String[0]);
    for (int i = 0; i < keys.length; i++) {
      Object v = uidefs.get(keys[i]);
      if (v instanceof Integer) {
        int intVal = uidefs.getInt(keys[i]);
      } else if (v instanceof Boolean) {
        boolean boolVal = uidefs.getBoolean(keys[i]);
      } else if (v instanceof String) {
        String strVal = uidefs.getString(keys[i]);
      } else if (v instanceof Dimension) {
        Dimension dimVal = uidefs.getDimension(keys[i]);
      } else if (v instanceof Insets) {
        Insets insetsVal = uidefs.getInsets(keys[i]);
      } else if (v instanceof Color) {
        Color colorVal = uidefs.getColor(keys[i]);
      } else if (v instanceof Font) {
        Font fontVal = uidefs.getFont(keys[i]);
      } else if (v instanceof Border) {
        Border borderVal = uidefs.getBorder(keys[i]);
      } else if (v instanceof Icon) {
        Icon iconVal = uidefs.getIcon(keys[i]);
      } else if (v instanceof javax.swing.text.JTextComponent.KeyBinding[]) {
        javax.swing.text.JTextComponent.KeyBinding[] keyBindsVal = (javax.swing.text.JTextComponent.KeyBinding[]) uidefs
            .get(keys[i]);
      } else if (v instanceof InputMap) {
        InputMap imapVal = (InputMap) uidefs.get(keys[i]);
      } else {
        System.out.println("Unknown type"); 
      }
    }
  }
}





How to change the look and feel of Swing applications

  

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class Main extends JFrame {
  private String strings[] = { "Metal", "Motif", "Windows" };
  private UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();
  private JRadioButton radio[] = new JRadioButton[strings.length];
  private JButton button = new JButton("JButton");
  private JLabel label= new JLabel("This is a Metal look-and-feel", SwingConstants.CENTER);
  private JComboBox comboBox = new JComboBox(strings);
  public Main() {
    JPanel northPanel = new JPanel();
    northPanel.setLayout(new GridLayout(3, 1, 0, 5));
    northPanel.add(label);    
    northPanel.add(button);    
    northPanel.add(comboBox);
    add(northPanel, BorderLayout.NORTH);
    JPanel southPanel = new JPanel();
    
    ItemHandler handler = new ItemHandler();
    southPanel.setLayout(new GridLayout(1, radio.length));
    ButtonGroup group = new ButtonGroup();
    for (int i = 0; i < radio.length; i++) {
      radio[i] = new JRadioButton(strings[i]);
      radio[i].addItemListener(handler);
      group.add(radio[i]);
      southPanel.add(radio[i]);
    }
    add(southPanel, BorderLayout.SOUTH);
    
    setSize(300, 200);
    setVisible(true);
    radio[0].setSelected(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  private void changeTheLookAndFeel(int value) {
    try {
      UIManager.setLookAndFeel(looks[value].getClassName());
      SwingUtilities.updateComponentTreeUI(this);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public static void main(String args[]) {
    Main dx = new Main();
  }
  private class ItemHandler implements ItemListener {
    public void itemStateChanged(ItemEvent e) {
      for (int i = 0; i < radio.length; i++)
        if (radio[i].isSelected()) {
          label.setText("This is a " + strings[i] + " look-and-feel");
          comboBox.setSelectedIndex(i);
          changeTheLookAndFeel(i);
        }
    }
  }
}





Replaces the standard scrollbar UI with a custom one

  
//An example of modifying an existing L&F. 
//This example replaces thestandard scrollbar UI with a custom one.
//
import java.awt.Container;
import java.awt.GridLayout;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
public class MetalModExample {
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (Exception e) {
      System.err.println("Metal is not available on this platform?!");
      System.exit(1);
    }
    JComponent before = makeExamplePane();
    UIManager.put("ScrollBarUI", "MyMetalScrollBarUI");
    JComponent after = makeExamplePane();
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = f.getContentPane();
    c.setLayout(new GridLayout(2, 1, 0, 1));
    c.add(before);
    c.add(after);
    f.setSize(450, 400);
    f.setVisible(true);
  }
  public static JComponent makeExamplePane() {
    JTextArea text = new JTextArea();
    try {
      text.read(new FileReader("MetalModExample.java"), null);
    } catch (IOException ex) {
    }
    JScrollPane scroll = new JScrollPane(text);
    return scroll;
  }
}





Retrieve information of all available UIManager defaults

  
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
public class Main {
  public static void main(String[] args) {
    System.out.println("  " + UIManager.getLookAndFeel().getName());
    UIManager.LookAndFeelInfo[] inst = UIManager.getInstalledLookAndFeels();
    for (int i = 0; i < inst.length; i++) {
      System.out.println("  " + inst[i].getName());
    }
    LookAndFeel[] aux = UIManager.getAuxiliaryLookAndFeels();
    if (aux != null) {
      for (int i = 0; i < aux.length; i++) {
        System.out.println("  " + aux[i].getName());
      }
    } else {
      System.out.println("  <NONE>");
    }
    System.out.println("  " + UIManager.getCrossPlatformLookAndFeelClassName());
    System.out.println("  " + UIManager.getSystemLookAndFeelClassName());
  }
}





Setting a UI Default Value That Is Created at Every Fetch

  
import java.util.Date;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
public class Main {
  public static void main(String[] argv) {
    Object activeValue = new UIDefaults.ActiveValue() {
      public Object createValue(UIDefaults table) {
        return new Date();
      }
    };
    UIManager.put("key", activeValue);
    Date d1 = (Date) UIManager.get("key");
    Date d2 = (Date) UIManager.get("key");
    boolean b = d1.equals(d2); // false
  }
}





Setting a UI Default Value That Is Created When Fetched

  
import javax.swing.JPanel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
public class Main {
  public static void main(String[] argv) {
    Object lazyValue = new UIDefaults.LazyValue() {
      public Object createValue(UIDefaults table) {
        return new JPanel();
      }
    };
    UIManager.put("key", lazyValue);
    Object value = UIManager.get("key");
  }
}





UIManager defaults

  
//An informational utility to print the various UIManager defaults.
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
public class UIManagerDefaults {
  public static void main(String[] args) {
    System.out.println("Default L&F:");
    System.out.println("  " + UIManager.getLookAndFeel().getName());
    UIManager.LookAndFeelInfo[] inst = UIManager.getInstalledLookAndFeels();
    System.out.println("Installed L&Fs: ");
    for (int i = 0; i < inst.length; i++) {
      System.out.println("  " + inst[i].getName());
    }
    LookAndFeel[] aux = UIManager.getAuxiliaryLookAndFeels();
    System.out.println("Auxiliary L&Fs: ");
    if (aux != null) {
      for (int i = 0; i < aux.length; i++) {
        System.out.println("  " + aux[i].getName());
      }
    } else {
      System.out.println("  <NONE>");
    }
    System.out.println("Cross-Platform:");
    System.out.println(UIManager.getCrossPlatformLookAndFeelClassName());
    System.out.println("System:");
    System.out.println(UIManager.getSystemLookAndFeelClassName());
    System.exit(0);
  }
}





UIManager resources to tweak the look of applications

  
//An example of using UIManager resources to tweak the look of applications.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.rupoundBorder;
public class ResourceModExample {
  public static void main(String[] args) {
    Border border = BorderFactory.createRaisedBevelBorder();
    Border tripleBorder = new CompoundBorder(new CompoundBorder(border,border), border);
    UIManager.put("Button.border", tripleBorder);
    UIManager.put("InternalFrame.closeIcon", new ImageIcon("close.gif"));
    UIManager.put("InternalFrame.iconizeIcon", new ImageIcon("iconify.gif"));
    UIManager.put("InternalFrame.maximizeIcon", new ImageIcon("maximize.gif"));
    UIManager.put("InternalFrame.altMaximizeIcon", new ImageIcon("altMax.gif"));
    UIManager.put("InternalFrame.titleFont", new Font("Serif", Font.ITALIC,12));
    UIManager.put("ScrollBar.width", new Integer(30));
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = f.getContentPane();
    JDesktopPane desk = new JDesktopPane();
    c.add(desk, BorderLayout.CENTER);
    JButton cut = new JButton("Cut");
    JButton copy = new JButton("Copy");
    JButton paste = new JButton("Paste");
    JPanel p = new JPanel(new FlowLayout());
    p.add(cut);
    p.add(copy);
    p.add(paste);
    c.add(p, BorderLayout.SOUTH);
    JInternalFrame inf = new JInternalFrame("MyFrame", true, true, true,true);
    JLabel l = new JLabel(new ImageIcon("luggage.jpeg"));
    JScrollPane scroll = new JScrollPane(l);
    inf.setContentPane(scroll);
    inf.setBounds(10, 10, 350, 280);
    desk.add(inf);
    inf.setVisible(true);
    f.setSize(380, 360);
    f.setVisible(true);
  }
}