All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.sfac.gui.cmp.ToolbarButton Maven / Gradle / Ivy

Go to download

This project is the core part of the Swing Framework and Components (SFaC). It contains the Swing framework classes and the graphical component classes.

The newest version!
/*-------------------------------------------------------------------------
 Copyright 2009 Olivier Berlanger

 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.
 -------------------------------------------------------------------------*/
package net.sf.sfac.gui.cmp;


import java.awt.Insets;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;


/**
 * Button using only an icon and with roll-over effect on the border (as windows toolbar buttons). This button is designed to be
 * used in a toolbar so you cannot set text on it and it cannot take focus.
 * 
 * @author Olivier Berlanger
 */
@SuppressWarnings("serial")
public class ToolbarButton extends JButton {


    /**
     * Create a new ToolbarButton with the given icon.
     * 
     * @param ico
     *            the button icon.
     */
    public ToolbarButton(String text) {
        super.setText(text);
        init();
    }


    /**
     * Create a new ToolbarButton with the given icon.
     * 
     * @param ico
     *            the button icon.
     */
    public ToolbarButton(Icon ico) {
        super(ico);
        init();
    }


    /**
     * Create a new ToolbarButton from the given action.
     * 
     * @param act
     *            Action bound to this button.
     */
    public ToolbarButton(Action act) {
        setAction(act);
        init();
    }


    private void init() {
        setMargin(new Insets(2, 2, 2, 2));
        super.setRolloverEnabled(true);
        setRequestFocusEnabled(false);
    }


    /**
     * Override the ancestor method so a text cannot be set on this button.
     * 
     * @param text
     *            ignored.
     */
    public void setText(String text) {
    }


    // /**
    // * Override the ancestor method because a text cannot be set on this button.
    // *
    // * @return null
    // */
    // public String getText() {
    // return null;
    // }


    /**
     * Override the ancestor method so roll-over cannot be disabled by the look-and-feel.
     * 
     * @param enabled
     *            ignored.
     */
    public void setRolloverEnabled(boolean enabled) {
        super.setRolloverEnabled(true);
    }


    /**
     * Override the ancestor method to return true only if roll-over is currently true.
     * 
     * @return true iff the border should be painted.
     */
    public boolean isBorderPainted() {
        return isEnabled() && getModel().isRollover();
    }


    /**
     * Return always false as a toolbar button cannot take the focus.
     * 
     * @return false.
     */
    public boolean isFocusable() {
        return false;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy