Java tutorial
//package com.java2s; /* * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code 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 General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import javax.swing.*; import java.util.*; public class Main { /** * Returns an integer from the defaults table. If <code>key</code> does * not map to a valid <code>Integer</code>, or can not be convered from * a <code>String</code> to an integer, the value 0 is returned. * * @param key an <code>Object</code> specifying the int. * @return the int */ public static int getUIDefaultsInt(Object key) { return getUIDefaultsInt(key, 0); } /** * Returns an integer from the defaults table that is appropriate * for the given locale. If <code>key</code> does not map to a valid * <code>Integer</code>, or can not be convered from a <code>String</code> * to an integer, the value 0 is returned. * * @param key an <code>Object</code> specifying the int. Returned value * is 0 if <code>key</code> is not available, * @param l the <code>Locale</code> for which the int is desired * @return the int */ public static int getUIDefaultsInt(Object key, Locale l) { return getUIDefaultsInt(key, l, 0); } /** * Returns an integer from the defaults table. If <code>key</code> does * not map to a valid <code>Integer</code>, or can not be convered from * a <code>String</code> to an integer, <code>default</code> is * returned. * * @param key an <code>Object</code> specifying the int. Returned value * is 0 if <code>key</code> is not available, * @param defaultValue Returned value if <code>key</code> is not available, * or is not an Integer * @return the int */ public static int getUIDefaultsInt(Object key, int defaultValue) { return getUIDefaultsInt(key, null, defaultValue); } /** * Returns an integer from the defaults table that is appropriate * for the given locale. If <code>key</code> does not map to a valid * <code>Integer</code>, or can not be convered from a <code>String</code> * to an integer, <code>default</code> is returned. * * @param key an <code>Object</code> specifying the int. Returned value * is 0 if <code>key</code> is not available, * @param l the <code>Locale</code> for which the int is desired * @param defaultValue Returned value if <code>key</code> is not available, * or is not an Integer * @return the int */ public static int getUIDefaultsInt(Object key, Locale l, int defaultValue) { Object value = UIManager.get(key, l); if (value instanceof Integer) { return ((Integer) value).intValue(); } if (value instanceof String) { try { return Integer.parseInt((String) value); } catch (NumberFormatException nfe) { } } return defaultValue; } }