mercredi 22 janvier 2020

JavaFx Get text from selected CheckBox

I was trying to get the text from selected CheckBox by clicking the button. My CheckBoxes are being created as children of a vbox and i can not figure out how to make an if statement in onRandom function which collects text of a selected CheckBoxes.

package sample;


import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;


public class Controller {

    @FXML
    private Button add;
    @FXML
    private Button delete;
    @FXML
    private VBox vbox;
    @FXML
    private TextField text;
    @FXML
    private Button random;
    @FXML
    private ListView listview1;
    @FXML
    private ListView listview2;



    @FXML
    void initialize() {

    }

    @FXML
    public void onAdd(ActionEvent e) {
        CheckBox cb = new CheckBox(text.getText());
        vbox.getChildren().add(cb);
        if (text.getText().matches("")) {
            vbox.getChildren().remove(cb);
        }
    }
    @FXML
    public void onDelete(ActionEvent e) {
        vbox.getChildren().removeIf(child -> ((CheckBox) child).isSelected());
    }

    @FXML
    public void onRandom(ActionEvent e) {


    }
}



Aucun commentaire:

Enregistrer un commentaire