Reputation: 381
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:
But when my mouse is clicked (not moved around) into the button, there is a "dot" border around the button, like this:
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)?
Upvotes: 36
Views: 51411
Reputation: 29
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.
Upvotes: 1
Reputation: 334
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)));
Upvotes: 4
Reputation: 10528
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.
Upvotes: 11
Reputation: 1739
That is not border. It's focus. You can remove it using:
jButton1.setFocusPainted(false);
Upvotes: 121
Reputation: 20783
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.
Upvotes: 1
Reputation: 135
see if this can help you out Remove border
or maybe this
Border emptyBorder = BorderFactory.createEmptyBorder();
yourButton.setBorder(emptyBorder);
Upvotes: 0