Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JComboBox on Windows 7 has rendering artifacts

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);
            }
        });
    }
}
like image 287
Steve McLeod Avatar asked Jun 17 '14 10:06

Steve McLeod


People also ask

What is JComboBox?

JComboBox is a part of Java Swing package. JComboBox inherits JComponent class . JComboBox shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only depending on the choice of the programmer .

What is getSelectedIndex?

int. getSelectedIndex() Returns the first item in the list that matches the given item. Object. getSelectedItem()

Which method does display the messages whenever there is an item selected or deselection of the JComboBox item?

getRenderer. Returns the renderer used to display the selected item in the JComboBox field.


1 Answers

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...

like image 153
MadProgrammer Avatar answered Sep 25 '22 12:09

MadProgrammer