How to catch serial fields with Javafx reflecition API?

Asked

Viewed 51 times

2

I have a form with several Checkboxs, which I named C1, C2, C3, C4, C5...until C66. So:

@FXML
private CheckBox c1;
@FXML
private CheckBox c2;
.
.
.
@FXML
private CheckBox c66;

Now I want to do an org.bson.Document to put all fields with their proper values. But to not have to put one by one, I would like to create a 'for'. But I don’t know how to send the Checkbox by name. Something like this:

 Document doc = new Document();
 for (int x = 1; x <= 66; x++) {
     CheckBox checkBox = this.getDecleredField("c" + x); //isso aqui é que eu não sei fazer
     doc.append("c" + x , checkBox.isSelected());
 } 
  • 1

    All these checkboxes must be inserted in a parent node, you can take this parent node, call the method getChildren() and from this you will have a list of all these checkboxes (and maybe other things on the list as well).

1 answer

1


Option 1:

Add your Checkbox to a Checkbox list, then navigate with a loop like this:

ch1 = new CheckBox("1");
ch2 = new CheckBox("2");
ch3 = new CheckBox("3");

//[...]

List<CheckBox> listaCheckBox = new ArrayList<>();
listaCheckBox.addAll(Arrays.asList(ch1,ch2,ch3, ..., ch66));

for(int i = 0; i < 66; i++){
    System.out.println("ch" + i + ":" + ch.isSelected());
}

Option 2:

Retrieve nodes from your dashboard in search of Checkbox:

// É uma lista para um tipo genérico, mas se todos os filhos forem
// Checkbox pode por ObservableList<CheckBox> e retirar o if
ObservableList<Node> listaNos = seuPane.getChildren();

// Você terá que passar a quantidade exata de nós para usar um contador
// nesse laço
for(Node n: listaNos){
    if(n instanceof CheckBox){
        System.out.println(((CheckBox) n).isSelected());
    }
}

Option 3:

You can use Checkcombobox / Checklistview / Checktreeview from Controlsfx, are very good components for this task. Would look like this:

// Criação do CheckListView
ObservableList<String> lista = FXCollections.observableArrayList();
lista.addAll("ch1","ch2","ch3",...,"ch66");
checklistview = new CheckListView(lista);

// Para pegar o texto de todos os selecionados é só fazer
ObservableList<String> s = adminUsuarioCbbSetor.getCheckModel().getCheckedItems();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.