Java JToolBar showTextOnToolbar(javax.swing.JToolBar t, boolean show)

Here you can find the source of showTextOnToolbar(javax.swing.JToolBar t, boolean show)

Description

This method shows/hides the text of the buttons on 't' according to the value of 'show'.

License

Open Source License

Declaration

public static void showTextOnToolbar(javax.swing.JToolBar t, boolean show) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2006-2013 AITIA International, Inc.
 * //w w w . jav a2  s. com
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.lang.reflect.Method;

import javax.swing.AbstractButton;

public class Main {
    /** This method shows/hides the text of the buttons on 't' according to the
     *  value of 'show'.
     */
    public static void showTextOnToolbar(javax.swing.JToolBar t, boolean show) {
        for (int i = t.getComponentCount() - 1; i >= 0; --i) {
            if (t.getComponent(i) instanceof javax.swing.AbstractButton) {
                javax.swing.AbstractButton button = (javax.swing.AbstractButton) t.getComponent(i);
                Object hide = button.getClientProperty("hideActionText");
                javax.swing.Action a = button.getAction();
                try {
                    // in the case of using JRE6
                    Method m = AbstractButton.class.getMethod("setHideActionText", Boolean.TYPE);
                    m.invoke(button, !show);
                    button.setAction(null);
                    button.setAction(a);
                } catch (Exception e) {
                }
                if (hide instanceof Boolean && (Boolean) hide != (!show)) {
                    button.putClientProperty("hideActionText", !show);
                    // in the case of using JRE5
                    if (a != null) {
                        String name = a.getValue(javax.swing.Action.NAME).toString();
                        a.putValue(javax.swing.Action.NAME, null);
                        a.putValue(javax.swing.Action.NAME, name);
                    }
                }
            }
        }
    }
}

Related

  1. makeToolButton(URL iconURL, String cmd, String tooltip, String alt, ActionListener listener)
  2. resizeToolBarButtons(JComponent toolbar)
  3. searchFakeToolBarRecursive(Container c)
  4. setToolIcons(AbstractButton b, Icon[] icons)
  5. setToolIcons(AbstractButton b, Icon[] icons)
  6. styleToolBarButton(final AbstractButton btn, final Font font, final boolean addPadding)