Disable JButton focus border

前端 未结 5 1044
说谎
说谎 2020-12-15 16:38

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 :

          


        
相关标签:
5条回答
  • 2020-12-15 17:11

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

    jButton1.setFocusPainted(false);
    
    0 讨论(0)
  • 2020-12-15 17:14

    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.

    0 讨论(0)
  • 2020-12-15 17:19

    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)));
    
    0 讨论(0)
  • 2020-12-15 17:22

    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.

    0 讨论(0)
  • 2020-12-15 17:33

    see if this can help you out Remove border

    or maybe this

    Border emptyBorder = BorderFactory.createEmptyBorder();
    yourButton.setBorder(emptyBorder);
    
    0 讨论(0)
提交回复
热议问题