Dropdown: How do I add separators in my dropdowns?

Separators will only appear in between list options that implement OptionGroup (a subclass of Option). So, if a dropdown's "items" property is bound to a list that contains one or more OptionGroup elements, and if the "separators" property is set to true, then separators should appear. Below is an example on how to configure this:

  • Drag a basic > dropdown onto the designer
  • Press the Java tab at the top of the designer to enter Java mode
  • Locate the init() method and add the following source to the method
//Initialize OptionGroup1
OptionGroup optionGroup_1 = new OptionGroup();
Option[[ | ]] options_1 = new Option[] {
   new Option("red"),
   new Option("pink")
optionGroup_1.setLabel("Group 1");

//Initialize OptionGroup2
OptionGroup optionGroup_2 = new OptionGroup();
Option[[ | ]] options_2 = new Option[] {
   new Option("brown"),
   new Option("sepea")
optionGroup_2.setLabel("Group 2");

//Initialize dropdown with optionGroups
getDropDown1().setItems(new Option[]{optionGroup_1, optionGroup_2});
  • Reformat the code (Ctrl-Shift-F) and fix imports.
*  If your application is based on Java EE 5, choose com.sun.webui.jsf.model.Option.
* If your application is based on J2EE 1.4, choose com.sun.rave.web.ui.model.Option.

  • Deploy the application
  • The dropdown options will display with a separator between the groups.

Note: You can disable the separator property to list the options as if they were all obtained from the same group.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo