Chrispresso
Chrispresso

Reputation: 4070

Getting a custom class to be recognized as an fxml tag

I have a custom textfield, PersistentPromptTextField that extends TextField and would like to be able to add it my .fxml file. When I do and I run it, I get a list of errors such as this:

Caused by: java.lang.InstantiationException: gui.PersistentPromptTextField
at java.lang.Class.newInstance(Unknown Source)
at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
... 75 more
Caused by: java.lang.NoSuchMethodException: gui.PersistentPromptTextField.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
... 77 more

In my .fxml file I have included <?import gui.PersistentPromptTextField?> as well, but obviously I am missing something.

Upvotes: 0

Views: 52

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49195

The error raised due to the FXMLLoader is (by default) tried to instantiate the given class using its a no-arg constructor. If you didn't define, try it.

Instantiating using a constructor having some arguments, you need to use @NamedArgs annotation. See this comprehensive answer for more details.

Upvotes: 2

Related Questions