Zoltán
Zoltán

Reputation: 22156

How can I populate a combo box with values of an enum in NetBeans GUI editor?

I have a combo box whose values need to correspond to the values of an enumeration that I've defined.

How can I set up the combo box in the NetBeans GUI so that it takes the values directly from the enumeration?

Upvotes: 0

Views: 1690

Answers (1)

Zoltán
Zoltán

Reputation: 22156

Based on an answer from this thread, I have found the solution to be

  1. Right click on the combo box, select Properties
  2. Select '...' next to model
  3. In the combo box menu on top of the window, select Custom code for 'Set yourComboBox's model property using:'
  4. Enter new DefaultComboBoxModel(YourEnum.values()) into the given text field.
  5. Press 'OK' and 'Close'
  6. Switch to the 'Source' tab and make sure that DefaultComboBoxModel and YourEnum are imported.

Unfortunately, this doesn't seem to show the values of the combo box in the 'Preview Design' view, but it does show the values if you embed your component into another component (e.g. if you defined your combo box as a part of a panel and you later add that panel to another panel).

Upvotes: 2

Related Questions