kottesvaran m February 2016

how to get enabled checkbox values or names or fx:id's in javafx

I am new to javaFX and FXML, I have created a fxml with few checkbox's and submit button (both are inside a tab called "test tab").

How do I get only enabled checkbox values and/or text and/or fx:id's and while click on submit button.

below the fxml.

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.web.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.45" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainViewController">
<children>
<TabPane fx:id="tabPane" layoutX="7.0" layoutY="6.0" prefHeight="389.0" prefWidth="589.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Test tab">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button fx:id="submit" layoutX="450.0" layoutY="283.0" mnemonicParsing="false" onAction="#dsubmit" text="SUBMIT" />
<CheckBox fx:id="dbox1" layoutX="46.0" layoutY="30.0" mnemonicParsing="false" onAction="#someaction" text="First Checkbox" />
<CheckBox fx:id="dbox2" layoutX="46.0" layoutY="65.0" mnemonicParsing="false" onAction="#someaction" text="Second Checkbox" />
<CheckBox fx:id="dbox3" layoutX="46.0" layoutY="105.0" mnemonicParsing="false" onAction="#someaction" text="Third Checkbox" />
<CheckBox fx:id="dbox4" layoutX="46.0" layoutY="140.0" mnemonicParsing="false" onAction="#someaction" text="Fourth Checkbox" />
<CheckBox fx:id="dbox5" layoutX="46.0"        

Answers


James_D February 2016

Not quite sure exactly what you are asking, but in your dsubmit() method you can just do

if (dbox1.isSelected()) {
    // ...
}

if (dbox2.isSelected()) {
    // ...
} 

or something similar.

If you want a list of the selected check boxes you could do something like

List<CheckBox> selectedBoxes = 
    Stream.of(dbox1, dbox2, dbox3, dbox4, dbox5, dbox6)
    .filter(CheckBox::isSelected)
    .collect(Collectors.toList());

Post Status

Asked in February 2016
Viewed 3,234 times
Voted 12
Answered 1 times

Search




Leave an answer