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
.
You can create a radio button in JavaFX by instantiating the javafx. scene. control. RadioButton class, which is the subclass of the ToggleButton class.
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.
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.
FXML is an XML-based language that provides the structure for building a user interface separate from the application logic of your code.
Not only Node
s but also ToggleGroup
s 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);
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With