Java JButton Create createToolBarButton(String text, Icon icon)

Here you can find the source of createToolBarButton(String text, Icon icon)

Description

create Tool Bar Button

License

Open Source License

Declaration

public static JButton createToolBarButton(String text, Icon icon) 

Method Source Code

//package com.java2s;
/*/*from  w  w w. j  ava2  s  . c  o m*/
 * TV-Browser
 * Copyright (C) 04-2003 Martin Oberhauser (martin_oat@yahoo.de)
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * CVS information:
 *  $RCSfile$
 *   $Source$
 *     $Date: 2010-11-14 17:44:44 +0100 (Sun, 14 Nov 2010) $
 *   $Author: bananeweizen $
 * $Revision: 6823 $
 */

import java.awt.Insets;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;

import javax.swing.JButton;

public class Main {
    public static final Insets ZERO_INSETS = new Insets(0, 0, 0, 0);

    public static JButton createToolBarButton(String text, Icon icon) {
        final JButton btn;
        if (icon == null) {
            btn = new JButton(text);
        } else {
            btn = new JButton(icon);
            btn.setToolTipText(text);
            btn.setBorderPainted(false);
            btn.setMargin(ZERO_INSETS);

            btn.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseEntered(MouseEvent e) {
                    if (btn.isEnabled()) {
                        btn.setBorderPainted(true);
                    }
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    btn.setBorderPainted(false);
                }
            });
        }
        btn.setFocusPainted(false);

        return btn;
    }
}

Related

  1. createSmallButton(Icon icon, String tooltip, ActionListener listener)
  2. createSquareButton(String text, int size)
  3. createStandartButton(String name)
  4. createToggleButton(ImageIcon icon, int dimension, String tooltipText, java.awt.event.ActionListener action)
  5. createToolBarButton(Action action)
  6. createToolBarToggleButton(ImageIcon ic, String toolTip, ActionListener al)
  7. createTransparentButton(Action action, boolean withText)
  8. getCreateButton()
  9. makeButton(Action action)