The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > UI Default Values  [3 examples]

e871. Setting a UI Default Value That Is Created When Fetched

When a UI default value is fairly large and may never be used, the value should be lazily created. This means that the value should be created only when the value is fetched. The UIDefaults table allows for such values.

For values that are created every time they are fetched, see e872 Setting a UI Default Value That Is Created at Every Fetch.

This example declares a lazy value (a JPanel) that is created only when fetched.

    // Create a lazy value
    Object lazyValue = new UIDefaults.LazyValue() {
        // This method is called once, when the value is fetched.
        // If this method can be called no more than once, it must be synchronized.
        public Object createValue(UIDefaults table) {
            // The returned value will be permanently stored in the UI default table
            return new JPanel();
        }
    };
    
    // Add the lazy value to the UI defaults table
    UIManager.put("key", lazyValue);
    
    // Fetch the value; this causes the value to be created
    Object value = UIManager.get("key");

 Related Examples
e870. Getting the Default Values for a Look and Feel
e872. Setting a UI Default Value That Is Created at Every Fetch

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips   


© 2002 Addison-Wesley.