JComboBox containing Icon is too small on Mac
I am currently developing a Swing application which shall execute o开发者_StackOverflown every platform. The general application is not the problem, it works fine on Windows, Linux and Mac.
But I have this JComboBox
that displays in addition to the text also an Icon
(size 50x50).
I do understand, that Mac has strict layout rules and that oversized JComboBoxes
are not really wanted.
The problem is, that using the Mac Look-and-Feel
, my JComboBoxes
have correct width but the height is way too small (same height as if there would not be an icon). Therefore the top and bottom part of my Icon
are cut off which does not really look nice.
Displaying the JComboBox
without the Icon
does not make sense, but I have not found a solution so far to display the JComboBox
with the correct height (even setting MinimumSize
, PreferredSize
and MaximumSize
to icon.getIconHeight()+2
does not help). Using Windows or Linux the JComboBoxes
are displayed correctly.
Is there any property I could use or do I really have to live with the cut off icons?
Using CustomComboBoxDemo
shown in How to Use Combo Boxes, the custom renderer works as expected; but the UI delegate, com.apple.laf.AquaComboBoxUI
, ignores a request such as this:
petList.setPreferredSize(new Dimension(200, 130));
As an alternative, javax.swing.plaf.metal.MetalComboBoxUI
produces the result shown below. Note that the arrow and scrollbar (not shown) remain unchanged. As an aside, the arrow may be changed, as shown here.
Addendum: You can alter the UI defaults ad lib, as depicted below.
//Create the combo box.
JComboBox petList = new JComboBox(intArray);
Color bg = (Color) UIManager.get("ComboBox.background");
Color fg = (Color) UIManager.get("ComboBox.foreground");
UIManager.put("ComboBox.selectionBackground", bg);
UIManager.put("ComboBox.selectionForeground", fg);
petList.setUI(new MetalComboBoxUI());
ComboBoxRenderer renderer = new ComboBoxRenderer();
....
精彩评论