My problem is this. I need to validate my form, and I've done the CSS styles and everything I need. But I wonder if I have a way to get my form components through anchorpane.
Is there any way I can get a component of a anchorpane by fx: id?
My problem is this. I need to validate my form, and I've done the CSS styles and everything I need. But I wonder if I have a way to get my form components through anchorpane.
Is there any way I can get a component of a anchorpane by fx: id?
Using FXML controller member references
Probably the best solution for you given that you are using FXML, is to make use of the reference to the node which is injected in the controller. When you have an fx:id
element, you will have a corresponding member in your FXML with an @FXML
annotation. You can use the mechanisms defined in Passing Parameters JavaFX FXML to get a reference to your controller. The controller can either directly expose the node via a getter or (usually better), expose some properties representing the state of the node (e.g. the text of a label) and you can listen to and manipulate those properties if necessary.
Using getChildren on the pane
You can call pane.getChildren() to get the children of the pane, additionally recursively calling getChildren on each of the children if needed. A sophisticated solution can use a visitor pattern.
Using CSS lookups
As you have already set up CSS styles, rather than iterating through children, you can use a CSS lookup to look up a child by a CSS id. Note that you have to define an id
attribute for your nodes in addition to a fx:id
attribute in order for such lookups to work. Also note that the CSS must have been applied to the nodes for this to work, e.g. the Nodes are shown (already displayed) on an active scene, or you have manually triggered the applyCSS()
function to the parent node (when it is attached to a scene). These additional constraints and the non-typed, non-compiler verified nature of CSS selectors, makes using CSS lookups as a mechanism for finding children a pretty fragile solution and only really recommended in fairly limited circumstances.
Given that your task is form validation, you might want to google JavaFX form validation and choose an existing library rather than trying to roll your own thing. Example libraries you might consider are JideFX, FXForm2 or ControlsFX.