-2

I want to create button without any borders or shadow, but an icon instead using java swing component. How can I accomplish this?

image

Ashish Asodiya
  • 91
  • 1
  • 10
Sophart
  • 67
  • 1
  • 10

1 Answers1

1

Real Button

JButton btnNewButton = new JButton("");
btnNewButton.setContentAreaFilled(false);
btnNewButton.setBorderPainted(false);
btnNewButton.setBorder(null);
btnNewButton.setIcon(new ImageIcon(path));

This will give you a real button without any borders around the given image to work with. Note that in this state the button doesn't have a "click animation" anymore. For such an animation you could use the .setSelectedIcon(selectedIcon);

Clickable Image

ImageIcon img = new ImageIcon(path);
JLabel button = new JLabel(img);
button.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        //Set pressed or something else
    }
});

But this one just provide you a clickable Image and should only be used when a clickable Image without any other intentions is needed. Note that this way is just a workaround.

Marvin
  • 55
  • 11
  • 3
    The 1st way is +1 but the 2nd way is -1. – Andrew Thompson Sep 14 '17 at 07:44
  • 1
    @Marvin: Without a `ButtonModel`, `ButtonUI` etc, how does the user guess it's a button? – Catalina Island Sep 14 '17 at 09:56
  • 1
    @Marvin Buttons have far more functionality than just a mouse interactions - you can supply `Action`s which provide keyboard shortcuts and reusable functionality – MadProgrammer Sep 14 '17 at 11:09
  • @CatalinaIsland The user won't be able to see any difference between the first and the second. In this state they look exactly the same, BUT the first example has some boarders that can't be removed with `.setBorder` or `.setBorderPainted` – Marvin Sep 14 '17 at 11:32
  • @MadProgrammer that's no reason. All of the things you said can be done relativly identical with Lables too. Of course it's not a button but I thought I should mention a way to work around – Marvin Sep 14 '17 at 11:34
  • 1
    To expand on the comment of @CatalinaIsland, a button is designed to be focusable, it can be activated using the keyboard, it supports showing different icons for pressed, focused, etc. It support an action listener and can be constructed from an [action](https://docs.oracle.com/javase/8/docs/api/javax/swing/AbstractAction.html) which might also be used I a toolbar or menu. If that action is disabled, the relevant controls are also disabled, showing 'grayed out' (including any icon) .. There are a dozen reasons where if the program calls for an actionable button, a `JButton` is better. – Andrew Thompson Sep 14 '17 at 14:11
  • *"BUT the first example has some boarders that can't be removed with .setBorder or .setBorderPainted"* Beyond [`AbstractButton.setFocusPainted(boolean)`](https://docs.oracle.com/javase/8/docs/api/javax/swing/AbstractButton.html#setFocusPainted-boolean-) I cannot think of any. – Andrew Thompson Sep 14 '17 at 14:16
  • @Marvin Except you’d need to reinvent the wheel, which is our primary complaint (of your second example) – MadProgrammer Sep 14 '17 at 19:36
  • If you don't want to, don't use the second one. Thats why I wrote the first one. But sometimes it's not about what is best or not, but about working around a problem to solve your task. Maybe OP don't want anything more than a clickable Image. To accomplish this the second one could be lot shorter. – Marvin Sep 15 '17 at 07:33