6

When I use a JComboBox on Windows 7, the four corners each have a pixel that doesn't match the background colour of the parent component.

In Windows 8 this problem doesn't happen (although that could be because in Windows 8, the JComboBox is rendered as a perfect rectangle). Nor does it happen on OS X.

What can I do to make the corner pixels let the background colour of the parent component through?

Here's an image showing the problem:

enter image description here

Here's a self-contained code example I'm using:

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

import javax.swing.*;
import java.awt.*;

public class Main {

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

                JPanel contentPane = new JPanel();
                contentPane.setBackground(Color.WHITE);

                JComboBox<String> comboBox = new JComboBox<String>(new String[]{"One", "Two"});
                contentPane.add(comboBox);

                JFrame frame = new JFrame("JComboBox Test");
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.setContentPane(contentPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
  • i know this issue... but i thought it would be normal. maybe you have to set all backgroundcolors of the components (the combobox included) to the same color. – Rubinum Jun 17 '14 at 10:40
  • What `UIManager` [defaults](http://stackoverflow.com/a/1197350/230513) in `WindowsLookAndFeel` have you tried? – trashgod Jun 17 '14 at 10:45
  • 1
    @trashgod sadly this is default in Win7, only to override Borders, common issue from Win Vista + Aero – mKorbel Jun 17 '14 at 10:48
  • @trashgod I haven't changed any defaults. – Steve McLeod Jun 17 '14 at 11:26

2 Answers2

3

Try removing the border...

comboBox.setBorder(null);

enter image description here

The next choice would be to design a specialised look and feel delegate that achieved what you wanted on Windows...

For example...

public static class MyComboBoxUI extends WindowsComboBoxUI {

    @Override
    protected void installDefaults() {
        super.installDefaults();
        LookAndFeel.uninstallBorder(comboBox);
    }

    public static ComponentUI createUI(JComponent c) {
        return new MyComboBoxUI();
    }

}

And then install it using...

UIManager.put("ComboBoxUI", MyComboBoxUI.class.getName());

This will mean you won't need to remove the borders from every combo box you create

Or, you could simply override the default border property in the UIManager...

UIManager.put("ComboBox.border", new EmptyBorder(0, 0, 0, 0));

Either way, it will effect all combo boxes created after you apply it...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-1

First thing I would try is setOpaque(false) on the JComboBox.

Also you should not set the WindowLookAndFeel directly. Instead set the System default L&F:

// force platform native look & feel
try {
    final String className = UIManager.getSystemLookAndFeelClassName();
    UIManager.setLookAndFeel(className);
} catch (final Exception e) {
    // ignore
}

That will always set the OS's default look and feel, regardless of what OS you're running on.

Durandal
  • 19,919
  • 4
  • 36
  • 70