Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sizing the Width of a ComboBox based on the items

user2932712Apr 16 2015 — edited Apr 18 2015

Hi

I have a ComboBox -- just a simple ComboBox with string items.

My string items aren't very long (5 letters max), but the ComboBox shows quite wide relative to my strings. So I would like to size my ComboBox to the size of my strings.

I messed around with setting the CellFactory of the ComboBox, and have the Cells produced by the CellFactory set the preferred width of the cells to some constant like 100. That worked in terms of changing the size of the ComboBox to the preferred width of 100.

But what I really want to do is to compute the preferred width from the string items. For that I need to be able to compute how wide a string item will be when rendered. And I guess that depends on the font size used by the ComboBox to render strings, which probably depends on the platform (OS X different to Windows?).

Any suggestions would be greatly appreciated as this is an issue which occurs often for me.

graham

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 16 2015
Added on Apr 16 2015
4 comments
1,459 views