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; /* //from w w w . ja v a 2s. co m * Copyright 2016 Dmitry Avtonomov. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 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<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<>(); 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 | IllegalArgumentException | InvocationTargetException ex) { } } } return retVal; } }