How to get Rid of this strange Border on JButton in Windows System look and Feel?

Posted by Timo J. on Stack Overflow See other posts from Stack Overflow or by Timo J.
Published on 2013-11-10T13:59:24Z Indexed on 2013/11/10 15:54 UTC
Read the original article Hit count: 253

I have a small Problem with the Layout of my Frame. As You could see in a picture (if i would have my 10 rep ;) ) there is a small Border around my JButtons. I'm already searching long time on this topic and i finally decided to ask for help, i'm just out of ideas. Is this Part of the Windows Theme which shouldn't be changed? It just doesnt fit into my current Layout, as I'm Listing TextBoxes and Comboboxes on Page Axis without any Border.

I would be very happy if there is solution for this issue.

Thanks in advance!

EDIT 1:

I do not mean the Focus Border. I like the Highlighting of a focussed Element. What i mean is the Border in the Background Color which causes a small distance of background-colored space beetween a JButton and another Element. (Picture on Personnal Webspace: http://tijamocobs.no-ip.biz/border_jbutton.png)

EDIT 2:

I'm using the Windows 8 Look and Feel but saw this Problem on Windows 7 Look and Feel, too.

short Example

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestFrame extends JFrame {

    public TestFrame(){
        setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
        for (int i = 0; i < 3; i++) {
        JButton btn = new JButton(".");
//          not working:
//          btn.setBorder(null);
//          btn.setBorder(BorderFactory.createEmptyBorder());
        add(btn);
        }
        pack();
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        TestFrame tiss = new TestFrame();
        tiss.setVisible(true);
    }

}

Example Code (long version):

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestFrame extends JFrame {

public TestFrame(){
    JPanel muh = new JPanel();
    muh.setLayout(new BoxLayout(muh, BoxLayout.PAGE_AXIS));
    for (int i = 0; i < 3; i++) {
        Container c = new JPanel();
        c.setLayout(new BoxLayout(c, BoxLayout.LINE_AXIS));
        Box bx = Box.createHorizontalBox();
        final String[] tmp = {"anything1","anything2"};
        JComboBox<String> cmbbx = new JComboBox<String>(tmp);
        cmbbx.setMinimumSize(new Dimension(80,20));
        bx.add(cmbbx);
        JButton btn = new JButton(".");
        btn.setMinimumSize(new Dimension(cmbbx.getMinimumSize().height,cmbbx.getMinimumSize().height));
        btn.setPreferredSize(new Dimension(30,30));
        btn.setMaximumSize(new Dimension(30,30));
        bx.add(btn);
        c.setMaximumSize(new Dimension(Integer.MAX_VALUE,30));
        c.add(new JLabel("Just anything"));
        c.add(bx);
        muh.add(c);
    }
    add(muh,BorderLayout.CENTER);
    pack();
}

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException,     IllegalAccessException, UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        TestFrame tiss = new TestFrame();
        tiss.setVisible(true);
    }

}

© Stack Overflow or respective owner

Related posts about java

Related posts about border