Arrow Button : JButton « Swing « Java Tutorial






import javax.swing.*;
import java.awt.*;

/**
 * A button which paints on it one or more scaled arrows in one of the cardinal directions.
 * @author Adrian BER
 */
public class ArrowButton extends JButton {

    /** The cardinal direction of the arrow(s). */
    private int direction;

    /** The number of arrows. */
    private int arrowCount;

    /** The arrow size. */
    private int arrowSize;

    public ArrowButton(int direction, int arrowCount, int arrowSize) {
        setMargin(new Insets(0, 2, 0, 2));
        setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        this.direction = direction;
        this.arrowCount = arrowCount;
        this.arrowSize = arrowSize;
    }

    /** Returns the cardinal direction of the arrow(s).
     * @see #setDirection(int)
     */
    public int getDirection() {
        return direction;
    }

    /** Sets the cardinal direction of the arrow(s).
     * @param direction the direction of the arrow(s), can be SwingConstants.NORTH,
     * SwingConstants.SOUTH, SwingConstants.WEST or SwingConstants.EAST
     * @see #getDirection()
     */
    public void setDirection(int direction) {
        this.direction = direction;
    }

    /** Returns the number of arrows. */
    public int getArrowCount() {
        return arrowCount;
    }

    /** Sets the number of arrows. */
    public void setArrowCount(int arrowCount) {
        this.arrowCount = arrowCount;
    }

    /** Returns the arrow size. */
    public int getArrowSize() {
        return arrowSize;
    }

    /** Sets the arrow size. */
    public void setArrowSize(int arrowSize) {
        this.arrowSize = arrowSize;
    }

    public Dimension getPreferredSize() {
        return getMinimumSize();
    }

    public Dimension getMinimumSize() {
        return new Dimension(
                arrowSize * (direction == SwingConstants.EAST
                        || direction == SwingConstants.WEST ? arrowCount : 3)
                + getBorder().getBorderInsets(this).left
                + getBorder().getBorderInsets(this).right
                ,
                arrowSize * (direction == SwingConstants.NORTH
                        || direction == SwingConstants.SOUTH ? arrowCount : 3)
                + getBorder().getBorderInsets(this).top
                + getBorder().getBorderInsets(this).bottom
                );
    }

    public Dimension getMaximumSize() {
        return getMinimumSize();
    }

    protected void paintComponent(Graphics g) {
        // this will paint the background
        super.paintComponent(g);

        Color oldColor = g.getColor();
        g.setColor(isEnabled() ? getForeground() : getForeground().brighter());

        // paint the arrows
        int w = getSize().width;
        int h = getSize().height;
        for (int i = 0; i < arrowCount; i++) {
            paintArrow(g,
                    (w - arrowSize * (direction == SwingConstants.EAST
                            || direction == SwingConstants.WEST ? arrowCount : 1)) / 2
                            + arrowSize * (direction == SwingConstants.EAST
                            || direction == SwingConstants.WEST ? i : 0),
                    (h - arrowSize * (direction == SwingConstants.EAST
                            || direction == SwingConstants.WEST ? 1 : arrowCount)) / 2
                            + arrowSize * (direction == SwingConstants.EAST
                            || direction == SwingConstants.WEST ? 0 : i),
                            g.getColor());
        }

        g.setColor(oldColor);
    }

    private void paintArrow(Graphics g, int x, int y, Color highlight) {
        int mid, i, j;

        Color oldColor = g.getColor();
        boolean isEnabled = isEnabled();

        j = 0;
        arrowSize = Math.max(arrowSize, 2);
        mid = (arrowSize / 2) - 1;

        g.translate(x, y);

        switch (direction) {
            case NORTH:
                for (i = 0; i < arrowSize; i++) {
                    g.drawLine(mid - i, i, mid + i, i);
                }
                if(!isEnabled)  {
                    g.setColor(highlight);
                    g.drawLine(mid-i+2, i, mid+i, i);
                }
                break;
            case SOUTH:
                if (!isEnabled) {
                    g.translate(1, 1);
                    g.setColor(highlight);
                    for (i = arrowSize - 1; i >= 0; i--) {
                        g.drawLine(mid - i, j, mid + i, j);
                        j++;
                    }
                    g.translate(-1, -1);
                    g.setColor(oldColor);
                }
                j = 0;
                for (i = arrowSize - 1; i >= 0; i--) {
                    g.drawLine(mid - i, j, mid + i, j);
                    j++;
                }
                break;
            case WEST:
                for (i = 0; i < arrowSize; i++) {
                    g.drawLine(i, mid - i, i, mid + i);
                }
                if(!isEnabled)  {
                    g.setColor(highlight);
                    g.drawLine(i, mid-i+2, i, mid+i);
                }
                break;
            case EAST:
                if(!isEnabled)  {
                    g.translate(1, 1);
                    g.setColor(highlight);
                    for(i = arrowSize-1; i >= 0; i--)   {
                        g.drawLine(j, mid-i, j, mid+i);
                        j++;
                    }
                    g.translate(-1, -1);
                    g.setColor(oldColor);
                }
                j = 0;
                for (i = arrowSize - 1; i >= 0; i--) {
                    g.drawLine(j, mid - i, j, mid + i);
                    j++;
                }
                break;
        }

        g.translate(-x, -y);
        g.setColor(oldColor);
    }
}








14.5.JButton
14.5.1.Creating a JButton
14.5.2.Creating a JButton from an Action objectCreating a JButton from an Action object
14.5.3.Using MnemonicsUsing Mnemonics
14.5.4.Displaying Mnemonics in Button textDisplaying Mnemonics in Button text
14.5.5.Adding Icon to JButtonAdding Icon to JButton
14.5.6.Adding a Rollover and Pressed Icon to a JButton Component
14.5.7.Adding a Disabled Icon to a JButton Component
14.5.8.Set pressed icon
14.5.9.Setting the Gap Size Between the Label and Icon in a JButton Component
14.5.10.Change button Horizontal Text Position
14.5.11.Set Rollover Icon and set Rollover Enabled
14.5.12.Button with user-draw Image iconButton with user-draw Image icon
14.5.13.Button icons, a default button, HTML in a button,and button mnemonics.
14.5.14.Placing Text and Icon togetherPlacing Text and Icon together
14.5.15.Displaying HTML on JButtonDisplaying HTML on JButton
14.5.16.Creating a Multiline Label for a JButton Component
14.5.17.Lines are left justified. This label text will center the lines
14.5.18.Label text italicizes the second line
14.5.19.Register two event listeners to JButtonRegister two event listeners to JButton
14.5.20.Setting the default buttonSetting the default button
14.5.21.Set tooltip for buttonSet tooltip for button
14.5.22.Call button doClick method to simulate a click action
14.5.23.Dynamically update the appearance of a component
14.5.24.Customizing a JButton Look and Feel
14.5.25.Buttons used in toolbars.
14.5.26.Transfer focus from button to button with help of arrows keys.
14.5.27.Arrow Button