ProAnswers.org

Creating a custom button in Java with JButton?

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).

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
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);

        }

    });