4

I am currently developing a Swing application which shall execute on 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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
MikeG
  • 41
  • 2
  • 1
    Have you tried creating a custom ListCellRenderer to be used with your JComboBox? – Hovercraft Full Of Eels Jul 02 '11 at 13:25
  • JComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); – mKorbel Jul 02 '11 at 13:32
  • Thanks, but I tried both and nothing worked yet. I already use a ListCellRenderer where I use setIcon(...) and setText(...). Strange thing is, I layout the components using BoxLayout and behind the ComboBoxes there is grey background (that shouldn't be there) and spacing, therefore it seems the height is computed correctly - still, it is displayed way too small. I found this [link](http://tinyurl.com/3k2a4es) regarding sizing of JComboBoxes - it seems, MacOs is rather strict with component sizing, which lets me fear, that there is really no way to display the icons properly :( – MikeG Jul 02 '11 at 18:18

1 Answers1

3

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));

enter image description here

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();

....

enter image description here

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • hight tax for more that expensive platform +1 – mKorbel Jul 02 '11 at 18:17
  • I think that will do the trick (have to wait until tomorrow to test it) but that means, my JComboBoxes won't look like usual MacOS-ComboBoxes any more - not quite what i wanted. Changing the arrow might help a bit, but the rounded border and blue "glowing" might be something not that easy to copy. So is there no alternative between "Looks like MacOS but is too small" and "Correct size but does not look like MacOS any more"? – MikeG Jul 02 '11 at 19:11
  • NB: The scroll bar and button remain Mac L&F. You _can_ change the arrow, but it's not _required_. You can alter the metal default colors, as shown above. The two delegates use the defaults differently, so you'll need to experiment. Also, condition this on the "os.name" property. – trashgod Jul 02 '11 at 20:01
  • @Lukas Knuth: Yes, but the question specified Swing. – trashgod Jul 02 '11 at 20:29