java - JavaFX: Constructor created with annotation -


i creating javafx application , when create controller fxml file constructors same.

is there way write custom annotation create constructors? this:

public class myclass() {      @initfxml(file = "test")     public myclass() {     } 

and @initfxml inject following code constructor:

fxmlloader loader = new fxmlloader(getclass().getresource("test.fxml"); ... 

or possible create annotation class creates default constructor?

any appreciated.

to process annotation, have define kind of container processed it, , load class through container, or define annotation processor attached compiler (i think: know nothing second option).

why not pass string parameter, though. define interface:

import java.net.url;  import javafx.fxml.fxmlloader;   public interface customcomponent {     public default void loadfxml(string fxml) {         try {             url resource = getclass().getresource(fxml);             fxmlloader loader = new fxmlloader(resource);             loader.setroot(this);             loader.setcontroller(this);             loader.load();         } catch (exception exc) {             if (! (exc instanceof runtimeexception)) {                 throw new runtimeexception(exc);             } else {                 throw (runtimeexception)exc ;             }         }     } } 

and have custom components implement it, calling method constructor:

import javafx.application.application; import javafx.fxml.fxml; import javafx.scene.scene; import javafx.scene.control.label; import javafx.scene.layout.vbox; import javafx.stage.stage;  public class customcomponenttest extends application {      @override     public void start(stage primarystage) {         scene scene = new scene(new customvbox(), 400, 400);         primarystage.setscene(scene);         primarystage.show();     }      public static class customvbox extends vbox implements customcomponent {          @fxml         private label label ;          public customvbox() {             loadfxml("customvbox.fxml");         }          @fxml         private void click() {             system.out.println("click!");         }     }      public static void main(string[] args) {         launch(args);     } } 

this seems no heavier defining annotation on empty constructor.


Comments