0

I am creating image buttons as below

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Demo {

private JFrame mainFrame;

static public Color BGCOLOUR1 = new Color(240, 240, 240);

public Demo() {

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception ex) {
        ex.printStackTrace();
    } 

    mainFrame = new JFrame("DEMO");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setBackground(BGCOLOUR1);
    mainFrame.setSize(300, 300);
    mainFrame.setLayout(new BorderLayout());


    JPanel smButtonContainer = new JPanel();
    smButtonContainer.setLayout(new FlowLayout());
    smButtonContainer.setOpaque(true);
    smButtonContainer.setBackground(Color.WHITE);
    smButtonContainer.setBorder(BorderFactory.createEmptyBorder(25, 10, 5, 0));

    ImageIcon emailLogo = new ImageIcon(getClass().getResource("/resources/email.png"));
    JButton emailButton = new JButton(emailLogo);
    emailButton.setBorder(null);
    emailButton.setBorderPainted(false);
    emailButton.setBackground(Color.WHITE);
    emailButton.setMargin(new Insets(0, 0, 0, 0));

    smButtonContainer.add(emailButton);

    mainFrame.add(smButtonContainer);


}

public static void main(String[] args) {
    Demo demo = new Demo();
    demo.showUI();
}

private void showUI() {
    mainFrame.setVisible(true);
}

}

On my development machine (Fedora 23 / KDE) this looks fine:

enter image description here

But when I run the application on a Windows 7 machine the button looks raised:

enter image description here

Why does that happen & how can I prevent this?

Edit: Updated the example.

Lieuwe
  • 1,734
  • 2
  • 27
  • 41
  • are you using the same LAF? – JoSSte Jun 07 '16 at 11:05
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 07 '16 at 11:12
  • Updated the example. Not sure how to include resources though. – Lieuwe Jun 07 '16 at 13:33
  • 1) Tip: Add @JoSSte (or whoever, the `@` is important) to *notify* the person of a new comment. 2) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. 3) *"Not sure how to include resources"* One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jun 07 '16 at 14:18
  • @AndrewThompson - sorry about the bracket - I really struggle with the SO editor as it f$%^s up indentation when I c&p from Eclipse. It also is the size of a postcard leaving 3/4rd of my screen real-estate empty. Anyway, issue was just addressed by another contributor. – Lieuwe Jun 07 '16 at 14:59

1 Answers1

3

You also need to add:

emailButton.setFocusPainted(false);

On Windows a dotted rectangle is painted to indicate the button has focus.

camickr
  • 321,443
  • 19
  • 166
  • 288