Here you can find the source of getProperties(JComponent component)
Parameter | Description |
---|---|
component | the component whose proerties are to be determined |
public static Map<Object, Object> getProperties(JComponent component)
//package com.java2s; /*/* w w w.j av a2s .c o m*/ * This file is part of McIDAS-V * * Copyright 2007-2014 * Space Science and Engineering Center (SSEC) * University of Wisconsin - Madison * 1225 W. Dayton Street, Madison, WI 53706, USA * http://www.ssec.wisc.edu/mcidas * * All Rights Reserved * * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and * some McIDAS-V source code is based on IDV and VisAD source code. * * McIDAS-V is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * McIDAS-V is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser Public License for more details. * * You should have received a copy of the GNU Lesser Public License * along with this program. If not, see http://www.gnu.org/licenses. */ import java.awt.Component; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import javax.swing.JComponent; public class Main { /** * Exclude methods that return values that are meaningless to the user */ static Set<String> setExclude = new HashSet<>(10); /** * 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) { Class<?> clazz = component.getClass(); Method[] methods = clazz.getMethods(); Map<Object, Object> retVal = new HashMap<>(methods.length); 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; } }