Creating a custom button in Java with JButton
Asked Answered
D

4

43

I am trying to create a button that has a custom shape (hexagon), but otherwise acts like a normal JButton would (that is, works with an ActionListener).

I have created a class that extends AbstractButton, but it doesn't seem to be sending events to the ActionListener when I click it. If I change the class to extend JButton it works perfectly, but it screws up the way the button is displayed. I'm assuming that there is a method that I need to override to get it to fire events, but I can't figure out what it is.

Duhon answered 22/4, 2011 at 0:52 Comment(1)
A
19

You will have to extend JButton class not AbstractButton. Try the following things and you will get idea.

The first move is to subclass JButton.

Then, in your subclass, start by redefining the paintComponent(Graphics) method. If you want any changes.

Then, override paintBorder(Graphics) to give it a shape of hexagon.

Atmospherics answered 22/4, 2011 at 1:2 Comment(4)
I had already redefined paintComponent and paintBorder, but it still paints a square border if you click one of the buttons.Duhon
Are you calling super.paintXXX() method after your customized logic or before?Atmospherics
I'm not calling super.paint at all.Duhon
Just figgured it out... Did exactly what you said but also in the constructor added this.setContentAreaFilled(false);Duhon
S
54

If you want to create a CustomButtonUI then you have to look

Note there isn't paintComponent(). It's wrong, just use the paint() method,

Below is just a simple example if that is possible (for Metal JButton). Note just for Metal LaF, I'm so lazy and there isn't something about override paintText, paintIcon, paintFocus, paintBorder (for all funcionalities you have to check available methods from BasicButtonUI), and something I put to the ButtonModel, just for my enjoyment.

test button image

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.MetalButtonUI;

public class TextAreaInButton {

    private JFrame frame = new JFrame("sssssssss");
    private JButton tip1Null = new JButton(" test button ");

    public TextAreaInButton() {
        Border line, raisedbevel, loweredbevel, title, empty;
        line = BorderFactory.createLineBorder(Color.black);
        raisedbevel = BorderFactory.createRaisedBevelBorder();
        loweredbevel = BorderFactory.createLoweredBevelBorder();
        title = BorderFactory.createTitledBorder("");
        empty = BorderFactory.createEmptyBorder(1, 1, 1, 1);
        final Border compound;
        Color crl = (Color.blue);
        compound = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl));
        Color crl1 = (Color.red);
        final Border compound1;
        compound1 = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl1));
        Color crl2 = (Color.black);
        final Border compound2;
        compound2 = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl2));
        tip1Null.setFont(new Font("Serif", Font.BOLD, 14));
        tip1Null.setForeground(Color.darkGray);
        tip1Null.setPreferredSize(new Dimension(50, 30));
        tip1Null.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
        tip1Null.setBorderPainted(true);
        tip1Null.setFocusPainted(false);
        tip1Null.setBorder(compound);
        tip1Null.setHorizontalTextPosition(SwingConstants.CENTER);
        tip1Null.setVerticalTextPosition(SwingConstants.BOTTOM);
        tip1Null.setUI(new ModifButtonUI());

        tip1Null.getModel().addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                ButtonModel model = (ButtonModel) e.getSource();
                if (model.isRollover()) {
                    tip1Null.setBorder(compound1);
                } else {
                    tip1Null.setBorder(compound);
                }
                if (model.isPressed()) {
                    tip1Null.setBorder(compound2);
                }
            }
        });

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(tip1Null, BorderLayout.CENTER);
        frame.setLocation(150, 150);
        frame.setPreferredSize(new Dimension(310, 75));
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                TextAreaInButton taib = new TextAreaInButton();
            }
        });
    }
}

class OldRoundedBorderLine extends AbstractBorder {

    private final static int MARGIN = 5;
    private static final long serialVersionUID = 1L;
    private Color color;

    OldRoundedBorderLine(Color clr) {
        color = clr;
    }

    public void setColor(Color clr) {
        color = clr;
    }

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        ((Graphics2D) g).setRenderingHint(
            RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(color);
        g.drawRoundRect(x, y, width, height, MARGIN, MARGIN);
    }

    @Override
    public Insets getBorderInsets(Component c) {
        return new Insets(MARGIN, MARGIN, MARGIN, MARGIN);
    }

    @Override
    public Insets getBorderInsets(Component c, Insets insets) {
        insets.left = MARGIN;
        insets.top = MARGIN;
        insets.right = MARGIN;
        insets.bottom = MARGIN;
        return insets;
    }
}

class ModifButtonUI extends MetalButtonUI {

    private static final ModifButtonUI buttonUI = new ModifButtonUI();

    ModifButtonUI() {
    }

    public static ComponentUI createUI(JComponent c) {
        return new ModifButtonUI();
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        final Color color1 = new Color(230, 255, 255, 0);
        final Color color2 = new Color(255, 230, 255, 64);
        final Color alphaColor = new Color(200, 200, 230, 64);
        final Color color3 = new Color(
            alphaColor.getRed(), alphaColor.getGreen(), alphaColor.getBlue(), 0);
        final Color color4 = new Color(
            alphaColor.getRed(), alphaColor.getGreen(), alphaColor.getBlue(), 64);
        super.paint(g, c);
        Graphics2D g2D = (Graphics2D) g;
        GradientPaint gradient1 = new GradientPaint(
            0.0F, (float) c.getHeight() / (float) 2, color1, 0.0F, 0.0F, color2);
        Rectangle rec1 = new Rectangle(0, 0, c.getWidth(), c.getHeight() / 2);
        g2D.setPaint(gradient1);
        g2D.fill(rec1);
        GradientPaint gradient2 = new GradientPaint(
            0.0F, (float) c.getHeight() / (float) 2, color3, 0.0F, c.getHeight(), color4);
        Rectangle rec2 = new Rectangle(0, c.getHeight() / 2, c.getWidth(), c.getHeight());
        g2D.setPaint(gradient2);
        g2D.fill(rec2);
    }

    @Override
    public void paintButtonPressed(Graphics g, AbstractButton b) {
        paintText(g, b, b.getBounds(), b.getText());
        g.setColor(Color.red.brighter());
        g.fillRect(0, 0, b.getSize().width, b.getSize().height);
    }

    public void paintBorder(Graphics g) {
    }

    @Override
    protected void paintFocus(Graphics g, AbstractButton b,
        Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
    }
}
Swedish answered 22/4, 2011 at 11:53 Comment(7)
Errrrgh, can me someone explain me, what wrong char(s) split code to this creature, just copy + paste from IDESwedish
Just indent four spaces, e.g. NetBeans' control-shift-right. In the SO editor, select and click the {} icon. +1 example, btw.Courage
I tried all knows woodoo from another Forums, maybe Stars Imports "*" ... something wrong between hands and my head, anyway thanksSwedish
Reformatted code to restore lines lost in previous edit; please revert if incorrect. On Mac OS X, I used -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel to see the full effect. Thanks for this very nice example. +1Sevenfold
I took the liberty of adding an image of your ButtonUI.Sevenfold
-1 a per-component ui delegate is always a bad idea (will not survive a change of LAF), especially if the effect can be achieved without tweaking the delegate. Plus the visual outcome is ... ehem .. suboptimal (have to admit not digging into the why): f.i. let the button be displayed in a parent with green background and a layout which respects pref: the text is truncated and the button fills its complete rectangular areaMascot
@Mascot I'm sure you forgot to look here, this is the sameSwedish
A
19

You will have to extend JButton class not AbstractButton. Try the following things and you will get idea.

The first move is to subclass JButton.

Then, in your subclass, start by redefining the paintComponent(Graphics) method. If you want any changes.

Then, override paintBorder(Graphics) to give it a shape of hexagon.

Atmospherics answered 22/4, 2011 at 1:2 Comment(4)
I had already redefined paintComponent and paintBorder, but it still paints a square border if you click one of the buttons.Duhon
Are you calling super.paintXXX() method after your customized logic or before?Atmospherics
I'm not calling super.paint at all.Duhon
Just figgured it out... Did exactly what you said but also in the constructor added this.setContentAreaFilled(false);Duhon
A
15

I know this question has been answered, but you might want to look at using the built-in methods, and using images to draw your button in different states.

Here is a bit of code I used to generate a custom button.

BufferedImage startButton = ImageIO.read(getClass().getResource("/icons/standard/buttons/start_backup.png"));
BufferedImage startButtonHover = ImageIO.read(getClass().getResource("/icons/standard/buttons/start_backup_hover.png"));
BufferedImage startButtonActive = ImageIO.read(getClass().getResource("/icons/standard/buttons/start_backup_active.png"));

JButton startBackupButton = new JButton(new ImageIcon(startButton));
startBackupButton.setRolloverIcon(new ImageIcon(startButtonHover));
startBackupButton.setPressedIcon(new ImageIcon(startButtonActive));
startBackupButton.setBorder(BorderFactory.createEmptyBorder());
startBackupButton.setContentAreaFilled(false);
startBackupButton.setFocusable(false);

You can then add an action listener to it as normal.

Adulteration answered 13/8, 2012 at 9:28 Comment(0)
C
9

try a Jlabel and use an image for any shape!!

    JLabel lbl = new JLabel("");
    lbl.setIcon(new ImageIcon("shape.png"));
    lbl.setBounds(548, 11, 66, 20);
    contentPane.add(lbl);

    lbl.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            System.exit(0);
        }
    });
Coleorhiza answered 22/4, 2011 at 0:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.