JComboBox on Windows 7 has rendering artifacts

When I use JComboBox on Windows 7, each of the four corners has a pixel that does not match the background color of the parent component.

In Windows 8, this problem does not occur (although it may be because in Windows 8 the JComboBox appears as a perfect rectangle). This also does not happen on OS X.

What can I do to make the angular pixels display the background color of the parent component through?

Here is an image showing the problem:

enter image description here

Here is an example of the standalone code 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); } }); } } 
+6
source share
2 answers

Try to remove the border ...

 comboBox.setBorder(null); 

enter image description here

The next choice will be to create a custom delegate to do what you want on Windows ...

For instance...

 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 means that you will not need to remove borders from each combo field you create.

Or you can just override the default border property in UIManager ...

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

In any case, this will affect all combined fields created after its application ...

+3
source

The first thing I'll try is setOpaque (false) on a JComboBox.

Also, you should not set WindowLookAndFeel directly. Instead, install L & F:

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

This will always determine the default appearance of the OS, regardless of which OS you are running on.

-1
source

Source: https://habr.com/ru/post/970942/


All Articles