1

This document describes how to add a separator to a javafx 2 choicebox by code: http://docs.oracle.com/javafx/2/ui_controls/choice-box.htm

I would like to achieve the same using a FXML layout. Any ideas?

shadowhorst
  • 1,480
  • 13
  • 21

2 Answers2

2

.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.HBox?>
<?import java.lang.*?>

<HBox xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
  <ChoiceBox>
    <String fx:value="Item 1" />
    <String fx:value="Item 2" />
    <Separator />
    <String fx:value="Item 3" />
  </ChoiceBox>
</HBox>

Don't forget to import the correct Class. With importing the correct classpath you can include any class and try to display it, even your self-made. Just open the FXML in your SceneBuilder and use Preview to see it in action without building a custom fxml loader for it.

seperated choices

jewelsea
  • 150,031
  • 14
  • 366
  • 406
thatsIch
  • 828
  • 11
  • 23
  • Is this possible without FXML? – Hendra Anggrian Mar 15 '18 at 17:20
  • sure, but this should be in another question since this is strictly related to using FXML to solve it – thatsIch Mar 17 '18 at 08:54
  • It turns out to be easy: `choiceBox.add(Separator())`. But of course the `ChoiceBox` generic must be `Object`. – Hendra Anggrian Mar 18 '18 at 18:44
  • using `javafx.scene.Node` should work as a generic.You could also use `javafx.scene.control.Control` depending on the situation. Check out the inheritence in the javadoch at https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Separator.html – thatsIch Mar 19 '18 at 12:16
1

This should do it. Replace the "Items" with your own content. Also, take a look at the FXML reference found here for more information on using FXML.

<ChoiceBox>
  <items>
    <FXCollections fx:factory="observableArrayList">
      <String fx:value="Item 1" />
      <String fx:value="Item 2" />
      <Separator fx:id="separator"/>
      <String fx:value="Item 3" />
    </FXCollections>
  </items>
</ChoiceBox>
OttPrime
  • 1,878
  • 1
  • 15
  • 24