Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement ToggleGroup in fxml file, using Spring vs JavaFx

Tags:

spring

javafx

Have MainController:

public class MainController {
    @FXML private RadioButton radioButton;
    @FXML private RadioButton radioButton2;

    @FXML public void addContact() {
        boolean b = radioButton.isSelected();
        boolean b2 = radioButton.isSelected();
    }
}

And main.fxml:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="350.0" prefWidth="755.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.habrahabr.ui.MainController">
      <TableView fx:id="table" editable="true" prefHeight="200.0" prefWidth="405.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
          <columnResizePolicy>
              <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
          </columnResizePolicy>
      </TableView>

      <HBox alignment="CENTER" layoutX="21.0" layoutY="207.0" prefHeight="50.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0">
            <RadioButton fx:id="radioButton" text="Male">
               <HBox.margin>
                  <Insets right="3.0"/>
               </HBox.margin>
            </RadioButton>

            <RadioButton fx:id="radioButton2" text="Female">
               <HBox.margin>
                  <Insets right="30.0"/>
                  <Insets bottom="10.0"/>
               </HBox.margin>
            </RadioButton>

            <Button minWidth="-Infinity" mnemonicParsing="false" onAction="#addContact" text="Add" />
      </HBox>
</AnchorPane>

All is fine, but I need to combine the both Radio Buttons in the one group and I can not find a solution to how to implement something like ToggleGroup in main.fxml.

like image 267
Valentyn Hruzytskyi Avatar asked Nov 25 '18 12:11

Valentyn Hruzytskyi


People also ask

How to add Radio buttons JavaFX?

You can create a radio button in JavaFX by instantiating the javafx. scene. control. RadioButton class, which is the subclass of the ToggleButton class.

Is FXML in Java?

FXML is an XML-based user interface markup language created by Oracle Corporation for defining the user interface of a JavaFX application. FXML presents an alternative to designing user interfaces using procedural code, and allows for abstracting program design from program logic.

What is toggle group in JavaFX?

public class ToggleGroup extends Object. A class which contains a reference to all Toggles whose selected variables should be managed such that only a single Toggle within the ToggleGroup may be selected at any one time.

What is FXML?

FXML is an XML-based language that provides the structure for building a user interface separate from the application logic of your code.


2 Answers

Not only Nodes but also ToggleGroups can be created in the fxml. Using <fx:reference> you can use an existing object:

<?import javafx.scene.control.ToggleGroup?>

...
<RadioButton fx:id="radioButton" text="Male">
   <HBox.margin>
      <Insets right="3.0"/>
   </HBox.margin>
   <toggleGroup>
       <ToggleGroup fx:id="group"/>
   </toggleGroup>
</RadioButton>
<RadioButton fx:id="radioButton2" text="Female">
   <HBox.margin>
      <Insets right="30.0"/>
      <Insets bottom="10.0"/>
   </HBox.margin>
   <toggleGroup>
       <fx:reference source="group"/>
   </toggleGroup>
</RadioButton>
...

Alternatively use the initialize method of the controller for this purpose:

@FXML
private void initialize() {
    ToggleGroup group = new ToggleGroup();
    radioButton.setToggleGroup(group);
    radioButton2.setToggleGroup(group);
}
like image 94
fabian Avatar answered Sep 30 '22 04:09

fabian


Following fabian's answer, you can define your toggle group in FXML then call it via the toggleGroup attribute. It works the same way but it is a little bit shorter.

<HBox ...>
    <fx:define>
        <ToggleGroup fx:id="group" />
    </fx:define>

    <RadioButton fx:id="radioButton" text="Male" toggleGroup="$group">
        <HBox.margin>
            <Insets right="3.0"/>
        </HBox.margin>
    </RadioButton>

    <RadioButton fx:id="radioButton2" text="Female" toggleGroup="$group">
        <HBox.margin>
            <Insets right="30.0" bottom="10.0"/>
        </HBox.margin>
    </RadioButton>

    <Button ... />
</HBox>
like image 35
0009laH Avatar answered Sep 30 '22 05:09

0009laH