Learn how to create interfaces with FXML and JAVAFX, static valueof() objects

The second way of creating objects in FXML is using the valueOf method. Sometimes, immutable classes, such as the string class provide a valueOf method to construct an object instead of a no arguments instructor.

If the valueOf method is declared static it can accept a single string argument and returns an object. You can use the fx:value attribute to create an object using this method.

For example, the string class found in the Java.lang.String library defines a valueOf method. As a matter of fact, all the primitive wrapper data types define a valueOf method and can be constructed in FXML as follows: String fx:value = “Hello, World!” Double fx:value = “1.0” Boolean fx:value = “false” FXML specifies values for attributes as strings but the appropriate conversion is automatically applied to convert the string values to the required types.

So for the double one, it would actually convert the string 1.0 to the double value of 1.0. Custom classes that define a static valueOf, with a string parameter method can also.

Leave a Reply

avatar
1000
  Subscribe  
Notify of