1

Does somebody know how to remove white rectangle border on JButton ? This issue is there only for windows look & feel when the button is a little bit rounded.

Please find attached the example on the imageenter image description here.

Setting the border to empty or null doesn't help. The same for margin.

The white margin/border dissapear only when I set the opacity of the button to false, but unfortunately in this case also the whole button is opaque on some versions of windows.

When I set opacity to false, it looks like: enter image description here

Code example:

public class TestFrame extends javax.swing.JFrame {

/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {

            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }

            TestFrame inst = new TestFrame();
            inst.setLocationRelativeTo(null);
            inst.setVisible(true);
        }
    });
}

public TestFrame() {

    this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    this.setLayout(null);
    this.getContentPane().setBackground(Color.BLACK);

    JButton button = new JButton();
    button.setBounds(10, 10, 100, 50);
    button.setBorder(BorderFactory.createEmptyBorder());    // not working
    button.setBorder(null);                                 // not working
    button.setMargin(new Insets(0,0,0,0));                  // not working

    add(button);
    pack();
    setSize(400, 300);
}

}

Thanks, Lubos

Lubos
  • 1,169
  • 10
  • 19
  • those Insets could be different by different theme is used in Win7 (nobody knows users setting in your PC), (maybe, I'm hope) Java version has not to do with, try to change Insets in UImanager, or came from container (parent for this JButton), for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, just about a.m. issue – mKorbel Apr 22 '13 at 15:19
  • This happens on all windows PCs. Which UI manager property do you mean ? I couldn't find there Button.margin or something like that. – Lubos Apr 22 '13 at 15:29
  • @splungebob Please see the second image. There are really some white borders or margins, the problem is that I cannot just set opacity to false, because it is not multiplatform. – Lubos Apr 22 '13 at 15:59
  • @Lubos: I still only see 1 button. – splungebob Apr 22 '13 at 16:06
  • Sorry, I edited my question. When you refresh, you will see the second image. – Lubos Apr 22 '13 at 16:08
  • 1
    Don't post an image. Post your SSCCE so we can see exatly what your code is doing. – camickr Apr 22 '13 at 16:08
  • As suggested twice, please post your code. Also, I did not suggest showing a second image. I suggested adding more JButtons to your existing container to compare what a focused button look like (there will only ever be at most 1) to another button that has no focus. – splungebob Apr 22 '13 at 16:14
  • I added code example. It is nothing special, just adding one button to Frame using windows look & feel. It has nothing to do with focusing of the button. If you set opacity of the button to false like button.setOpaque(false), you'll get rid of white margins. – Lubos Apr 22 '13 at 16:23

2 Answers2

0

Seems like a painting problem. You can use:

button.setBackground( Color.BLACK );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Yes, you'right. It looks like painting problem. With your solution it works, but it is a little bit hack :) I need now to find a way how to fix it e.g. when I use gradient as a background ... – Lubos Apr 22 '13 at 16:28
  • +1 for the straightforward approach. Lubos: See also this related [example](http://stackoverflow.com/a/5755124/230513). – trashgod Apr 22 '13 at 17:05
  • -1 This doesn't solve the problem on my Win XP machine. It hides MOST of it, but now it looks kinda sloppy, having some of the border still peeking through on the bottom. – splungebob Apr 22 '13 at 17:15
0

EDIT: See comments below. This sample still shows the effect, even using a proper layout. Setting the background color seems to show the unwanted border. This effect doesn't show with Metal. It seems as if Windows L&F shows a rounded edge, but the button is still rectangular. The space between is only noticable if the BG color of the container is changed to something obvious, like black.

import java.awt.*;

import javax.swing.*;

public class TestFrame extends JFrame
{
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        try
        {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }

        TestFrame inst = new TestFrame();
        inst.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        inst.setLocationRelativeTo(null);
        inst.setVisible(true);
      }
    });
  }

  public TestFrame()
  {
    JButton button = new JButton("One");
    JButton button2 = new JButton("Two");

    JPanel p = new JPanel();
    p.setBackground(Color.BLACK);
    p.setLayout(new FlowLayout());
    p.add(button);
    p.add(button2);

    add(p);
    setSize(400, 300);
  }
}
splungebob
  • 5,357
  • 2
  • 22
  • 45
  • 1
    `Do you realise that you're adding the button directly to the frame?` - the frame.add() method adds components to the content pane. The content pane is a JPanel. This a a SSCCE so this is not a big issue. A bigger issue is using a null layout. – camickr Apr 22 '13 at 16:41
  • @camickr: true, but it does make a difference visually. The OP's code uses a null layout for the frame, added a button, and it showed a border. My change constructed the button the same way, added it to a panel with null layout (instead of frame with null layout), then added that panel to the frame (using default layout) – splungebob Apr 22 '13 at 16:47
  • 1
    It doesn't matter at all in this case where the button is placed. That is just test example. Regardless where the button is placed, the issue is the same – Lubos Apr 22 '13 at 16:56
  • this is caused only in ContentPane, there are White Borders at 1pixel – mKorbel Apr 22 '13 at 17:03
  • 1
    My example removed the border, however I neglected to set the BG color to black. This seems to be the culprit. I edited my post to use a proper LayoutManager but still produce the border effect. Simply comment/uncomment the setBackgound(...) call to see the difference. – splungebob Apr 22 '13 at 17:04
  • @splungebob: the white border is there always, you just don't see it when using bright background. In this sense, changing layout manager will not help. The problem is painting of the button itself. – Lubos Apr 22 '13 at 20:15
  • -1, there is no need for this answer. The OP has already posted a SSCCE to demonstrate the problem. Also, you have entirely changed the context of this answer which originally noted that the OP was adding the button to the frame. Again that was irrevlevant to the described problem which is why I made my original comment. My comment about layout managers had nothing to do with this problem, it was just a general comment that Swing code should use layout managers. – camickr Apr 22 '13 at 20:21
  • @camickr: The OP's attempt at an SSCCE had syntax errors (no imports, missing curly brace), and thus was not an SSCCE. Also, in my original post I thought I had corrected the problem by avoiding null layout (`A bigger issue is using a null layout` as someone said). When I realized the mistake (by not setting the BG color), I fixed the post and explained. As of this writing, it's STILL the only SSCCE here. Your comment reeks of a revenge downvote, which is disappointing. – splungebob Apr 22 '13 at 22:13
  • Furthermore, another "need" for my post (besides it being compilable) is that it contains 2 buttons instead of one. I could not see the effect the OP described in his picture due to the focus being painted on the sole button. A second button w/o focus made the effect more obvious to old geezers like myself who don't have 20/20 vision. – splungebob Apr 22 '13 at 22:27
  • Funny, I thought your downvote of my suggestion was a revenge downvote because I made my original comment on your answer. I stand by my downvote for the reasons given. We post solutions that attempt to help the OP. I couldn't figure out how your code helped the OP and it confused the issue. By the way, my suggestion did help the OP as the answer was accepted. I'm sorry I wasn't able to test the suggestion and all platforms and JDKs. I would have made a comment (without the downvote) that the solution didn't work in all environments to warn other people, but that's just me. – camickr Apr 23 '13 at 04:07
  • @splungebob: sorry, that the "curly brace" appeared somehow out of the "code box" or there were missing imports, but I didn't notice that. When you use some IDE, you could fix this in a second - at least I'd do that. In my opinion this was totally irrelevant to the problem and I already mentioned in my answer that the problem has nothing to do with the button focus, so no need to construct 2 buttons. – Lubos Apr 23 '13 at 08:19