LabelFor field for label does not work as I expected

What exactly is the purpose of the labelFor field of a Label object?

Yesterday I first heard about this JavaFX thing, so I apologize if this sounds downright stupid. This word Label

seems familiar to me from what is in the HTML nodes. For this reason, I thought the field labelFor

would be like a property for

for Label

node. Unfortunately it did not function as I expected, at least I could not do it.

Here, let me clarify with an example:

Label name = new Label("Your name:");
TextField nameField = new TextField();

name.setLabelFor(nameField);

      

Sorry it's not actually an MWE, but here's what I expected from it: whenever the user clicks over the label name

, the textbox nameField

gets focus.

This, as far as I remember, is exactly what happens to the labels / input pairs in HTML. On the other hand, nothing happens with the code above in JavaFX.

I can fix it to make this happen by adding the following line after the other three:

name.setOnMouseClicked(event -> name.getLabelFor().requestFocus());

      

But why, why did I indicate labelFor

in the first place?

+3


source to share


1 answer


As the javadoc states:

A label can act as a label for another control or Node. This is used for Mnemonics and Accelerator analysis. This allows you to set the target Node.

So, if you set the mnemonic for the label:



    Label label = new Label("_Text");
    label.setMnemonicParsing(true);
    label.setLabelFor(tf);

      

then using this mnemonic (Alt-T) puts focus on the label Node

.

Also labelFor will help the disabled user to be given a description of the control taken from the specified label.

+2


source







All Articles