3 Replies Latest reply: Mar 14, 2013 2:24 PM by daniel RSS

    SceneBuilder 1.1-b18 stubs an <items> list for ChoiceBox elements

    pfurbacher
      In Scene Builder, I placed a ChoiceBox and gave it an fx:id.

      In the "controller" class:
      @FXML ChoiceBox<UserGroup> userGroupCB;
      In the "initialize()" method of the "controller", I set a StringConverter for the choice box so that it would display the "name" property of the domain object:
          @FXML
          protected void initialize() {
              ...
              userGroupCB.setConverter(new StringConverter<UserGroup>() {
                  
                  @Override
                  public String toString(UserGroup userGroup) {
                      return userGroup.getName();
                  }
                  
                  @Override
                  public UserGroup fromString(String arg0) {
                      return null;
                  }
              });
              ....
      Prior to setting this converter, I had added a toString() method to the UserGroup and everything worked just fine without the converter. However, I really didn't want to default to using the toString() method, so I set the converter.

      Boom! Crash! A ClassCastException occurred when setting the converter in the "initialize()" method. Scratch head, pull hair.

      By chance, I looked at the FXML in an XML editor and discovered that Scene Builder had stubbed out an <item> list!
          <ChoiceBox ... >
            <items>
              <FXCollections fx:factory="observableArrayList">
                <String fx:value="Item 1" />
                <String fx:value="Item 2" />
                <String fx:value="Item 3" />
              </FXCollections>
            </items>
          </ChoiceBox>
      So, yeah, of course, unbeknownst to me, it was expecting a String and I was specifying a UserGroup in the converter! (See also the @FXML ChoiceBox<UserGroup> ivar above.) Silly me.

      Why is Scene Builder stubbing out this <item> list?! There's no reason for it.

      Note that it does the same thing for ComboBox.

      I suppose that when I invoke "setItems()" on this choice box [this occurs outside the initialize() method because the values in the choice box are context sensitive, but always UserGroup items], but I shouldn't have to work around what shouldn't be a problem in the first place.


      Does anyone else think this is unexpected behind the scenes behavior for Scene Builder?
        • 1. Re: SceneBuilder 1.1-b18 stubs an <items> list for ChoiceBox elements
          Eric Le Ponner
          Scene Builder puts some sample item strings in ChoiceBox, ComboBox... to
          improve previewing experience.
          However it currently does not offer a way to clear/edit items. That's a known limitation.

          You can workaround this easily by clearing the "items" property manually in the FXML file.
          • 2. Re: SceneBuilder 1.1-b18 stubs an <items> list for ChoiceBox elements
            pfurbacher
            Eric Le Ponner wrote:
            Scene Builder puts some sample item strings in ChoiceBox, ComboBox... to
            improve previewing experience.
            However it currently does not offer a way to clear/edit items. That's a known limitation.

            You can workaround this easily by clearing the "items" property manually in the FXML file.
            I understand that putting the sample data there was meant for previewing. And once discovered by looking at the XML in a code editor, I removed those items.

            However, I would not describe this as a "limitation". It's bad design, especially since, if you do not work with the FXML directly, but only through Scene Builder (and that's what we should be doing), you wouldn't really know that those items are there. Unless the user selects an option called something like "add sample data to FXML file", not sample data should be there.

            Improving previewing experience can be done other ways than inserting "sample" XML into the FXML document. The preview runner can do this as it walks the scene graph, inserting sample data where it would prove useful. There is a "Show Sample Data" if you want to see data. Even that should not be putting the sample data into the FXML file.

            The sample data just doesn't belong in the FXML, period.

            Time to file a bug report.

            Edited by: pfurbacher on Mar 14, 2013 2:59 PM
            • 3. Re: SceneBuilder 1.1-b18 stubs an <items> list for ChoiceBox elements
              daniel
              Hi,

              There's already an RFE for that:
              http://javafx-jira.kenai.com/browse/DTL-5156

              -- daniel