-2

Trying to make a jButton with an icon. How do I get rid of the button outline behind the icon and how do i make the icon clickable? The only way my action Listener is being activated is if the button outline behind the icon is clicked. Not the actual icon.

public class RoundButton extends JButton {
    String iconPath = "/Users/Desktop/SNN/snn_emro_ui/gui_emro/gui_emro copy/src/resources/cross.png";
    JButton exitButton;
    public RoundButton() {
        ImageIcon icon = new ImageIcon(iconPath);
        exitButton = new JButton(icon);
        add(exitButton);
    }
}

the inner grey square with the X is the icon, not clickable and the white out part is the only part that is 'clickable'

  • 1
    Some code would help; we have no idea how you are drawing this. – Elliott Frisch Jan 31 '20 at 00:11
  • (1-) You were asked in your last question: https://stackoverflow.com/questions/59957640/java8-swing-making-round-button to post an [mre]. You didn't there and you still haven't here either so we can't offer any help. The answer provided by WJS is an example of an "MRE". That is we can copy/paste/compile/test to see exactly what the code is and what happens when the code is executed. – camickr Jan 31 '20 at 00:32
  • One way to use image(s) in an MRE is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Jan 31 '20 at 02:24

1 Answers1

0

Try this example.

The key statement is changing the button size to match the Icon. So you may want to scale the Icon to an appropriate size.

Other options are to:

  1. Change the insets of the Button to all 0's using setMargins.
  2. Setting the Button's border to null. This doesn't give any indication that the button has been pressed.

I prefer the resizing or insets option.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JButtonExample extends JPanel {

    final static int height = 500;
    final static int width = 500;
    JFrame frame = new JFrame();

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                () -> new JButtonExample().start());
    }

    public void start() {
        File file = new File("your image file name here");
        try {
        Image img = ImageIO.read(file);
        ImageIcon icon = new ImageIcon(img);
        JButton button = new JButton(icon);
        add(button);
        setBackground(Color.white);
        button.addActionListener((ae)-> System.out.println("Button Clicked"));
        button.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }

    public JButtonExample() {
        frame.setDefaultCloseOperation(
                JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        setPreferredSize(
                new Dimension(width, height));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}
WJS
  • 36,363
  • 4
  • 24
  • 39
  • `button.setPreferredSize(new Dimension(..));` this is not necessary. See [this example](https://stackoverflow.com/questions/10861852/add-a-complex-image-in-the-panel-with-buttons-around-it-in-one-customized-user/10862262#10862262) which features buttons the exact same size at their icon. (The example has 4 buttons and 5 labels.) – Andrew Thompson Jan 31 '20 at 02:34