Java JComponent Properties getProperties(JComponent component)

Here you can find the source of getProperties(JComponent component)

Description

Convenience method for obtaining most non-null human readable properties of a JComponent.

License

Open Source License

Parameter

Parameter Description
component the component whose proerties are to be determined

Return

the class and value of the properties

Declaration

public static Map<Object, Object> getProperties(JComponent component) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public class Main {
    /**// w  ww.java  2 s . c  o m
     * Exclude methods that return values that are meaningless to the user
     */
    static Set<String> setExclude = new HashSet<String>();

    /**
     * Convenience method for obtaining most non-null human readable properties
     * of a JComponent.  Array properties are not included.
     * <P>
     * Implementation note:  The returned value is a HashMap.  This is subject
     * to change, so callers should code against the interface Map.
     *
     * @param component the component whose proerties are to be determined
     * @return the class and value of the properties
     */
    public static Map<Object, Object> getProperties(JComponent component) {
        Map<Object, Object> retVal = new HashMap<Object, Object>();
        Class<?> clazz = component.getClass();
        Method[] methods = clazz.getMethods();
        Object value = null;
        for (Method method : methods) {
            if (method.getName().matches("^(is|get).*") && method.getParameterTypes().length == 0) {
                try {
                    Class returnType = method.getReturnType();
                    if (returnType != void.class && !returnType.getName().startsWith("[")
                            && !setExclude.contains(method.getName())) {
                        String key = method.getName();
                        value = method.invoke(component);
                        if (value != null && !(value instanceof Component)) {
                            retVal.put(key, value);
                        }
                    }
                    // ignore exceptions that arise if the property could not be accessed
                } catch (IllegalAccessException ex) {
                } catch (IllegalArgumentException ex) {
                } catch (InvocationTargetException ex) {
                }
            }
        }
        return retVal;
    }
}

Related

  1. buildComponentPanel(JComponent component, int hgap, int vgap, boolean isOpaque)
  2. buildComponentPanelRight(JComponent component, int hgap, int vgap, boolean isOpaque)
  3. disableAll(JComponent component)
  4. disposeParentWindow(JComponent component)
  5. doDisable(JComponent component)
  6. getProperties(JComponent component)
  7. getProperties(JComponent component)
  8. getVisibleBoundsOnScreen(JComponent component)
  9. invert(JComponent c)