35

I have a problem with JButton in Java. Basically, I want to disable the button's border (the button is added to JDesktopPane ).

Here is my code :

 JButton j = new JButton("BUTTON");
 j.setIcon(icon1); //icon1 : icon//
 j.setFocusable(true);
 j.setContentAreaFilled(false);
 j.setBounds(90, 20, 130, 30);
 dtp.add(j); //dtp : JDesktopPane//

It could let the border disappear like in this image:

enter image description here

But when my mouse is clicked (not moved around) into the button, there is a "dot" border around the button, like this:

enter image description here

So, how could I set the button so that when I don't move the mouse around the button area, it's still set like the first image, but when I move the mouse around, there's a square around the button (with a light-blue background)?

0xCursor
  • 2,242
  • 4
  • 15
  • 33
Cocc Cocc
  • 371
  • 1
  • 5
  • 7
  • 2
    *"let the border disappeared like this image"* That looks very non-intuitive. What *else* are you doing to confound & confuse the poor end user? – Andrew Thompson Feb 20 '12 at 14:24
  • So, which words do u think best in this case? Btw, English isn't my first language. – Cocc Cocc Feb 20 '12 at 14:29
  • I was referring to the borderless button as being 'non-intuitive', not what you wrote. What you wrote is quite clear. Sorry for any confusion. – Andrew Thompson Feb 20 '12 at 14:38

6 Answers6

120

That is not border. It's focus. You can remove it using:

jButton1.setFocusPainted(false);
brimborium
  • 9,362
  • 9
  • 48
  • 76
zari
  • 1,709
  • 1
  • 12
  • 18
11

This may be old thread, but I solved mine with button.setFocusable(false)

hope it helps for those whose still looking for some answer. cheers.

ralphgabb
  • 10,298
  • 3
  • 47
  • 56
  • Important to note that this is the correct default behavior only for ToolBars. In Visual Studio with Windows Forms a ToolStrip has TabStop set to false by default. Use my UI fix above to get rid of the dotted borders on Toolbar buttons and ToggleButtons which never have them in Windows with or without focus. – Goombert Feb 02 '16 at 18:01
  • Relevant answer for `Component`s other than `JButton` (how I ended up here) – jkmartindale Jun 03 '22 at 21:59
4

After digging I have come up with a better solution. This is also a common UI bug in GTK applications on Windows, one of them being GIMP. It greatly annoys me so I wanted to find a better fix global fix than setting it on every control manually.

The workaround sets the color for the focus on all the common controls that it's not supposed to occur to transparent. I went and tested extensively with Windows Forms in Visual Studio. Toolbar buttons and ToggleButtons should never have the dotted border even when focused. Toolbars are supposed to be setFocusable(false); by default as they are in Windows. Every other control should have the dotted border as in Swing, but only when being focus cycled.

        // Removes the dotted border around controls which is not consistent with Windows
        UIManager.put("Button.focus", new ColorUIResource(new Color(0, 0, 0, 0)));
        UIManager.put("ToggleButton.focus", new ColorUIResource(new Color(0, 0, 0, 0)));

        // ways to remove it from other controls...
        UIManager.put("CheckBox.focus", new ColorUIResource(new Color(0, 0, 0, 0)));
        UIManager.put("TabbedPane.focus", new ColorUIResource(new Color(0, 0, 0, 0)));
        UIManager.put("RadioButton.focus", new ColorUIResource(new Color(0, 0, 0, 0)));
        UIManager.put("Slider.focus", new ColorUIResource(new Color(0, 0, 0, 0)));

        // figure out combobox
        UIManager.put("ComboBox.focus", new ColorUIResource(new Color(0, 0, 0, 0)));
Goombert
  • 314
  • 3
  • 13
1

I do not think doing this with usual JButton is a good idea. If nothing, it will not show similar in different platforms ( Mac & Linux) in case you plan to show this button in different platforms. For all practical purposes button.setFocusPainted(false); should take care of your current requirement.

Consider using an extended JLabel with button like behavior(with action listeners) to avoid behavior differences.

ring bearer
  • 20,383
  • 7
  • 59
  • 72
0
JButton button = new JButton();
button.setFocusPainted(false); //To don't paint borders while 
                              //JButton is in focus.

button.setBorderPainted(false);//To don't paint JButton's borders.
0

see if this can help you out Remove border

or maybe this

Border emptyBorder = BorderFactory.createEmptyBorder();
yourButton.setBorder(emptyBorder);
Rbn
  • 135
  • 11
  • When i set "Border emptyBorder = BorderFactory.createEmptyBorder(); yourButton.setBorder(emptyBorder)"; it gave me the normal button without any effect. – Cocc Cocc Feb 20 '12 at 13:22