java - JavaFX create and use custom control with it's own functions -


i have created simple custom control shown in picture:
enter image description here

when click on "click me" button console shows me message "the button clicked!" function called dosomething, , custom control's fxml file code:

<?xml version="1.0" encoding="utf-8"?>  <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.anchorpane?>  <fx:root prefheight="83.0" prefwidth="196.0" type="javafx.scene.layout.anchorpane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">    <children>       <textfield fx:id="textfield" layouty="2.0" prefheight="25.0" prefwidth="196.0" />       <button layoutx="72.0" layouty="29.0" mnemonicparsing="false" onaction="#dosomething" text="click me" />    </children> </fx:root> 

and controller of custom control:

package control;  import java.io.ioexception;  import javafx.beans.property.stringproperty; import javafx.fxml.fxml; import javafx.fxml.fxmlloader; import javafx.scene.control.textfield; import javafx.scene.layout.anchorpane;  public class controller extends anchorpane {     @fxml private textfield textfield;      public controller() {         fxmlloader fxmlloader = new fxmlloader(getclass().getresource("customcontrolview.fxml"));         fxmlloader.setroot(this);         fxmlloader.setcontroller(this);          try {             fxmlloader.load();         } catch (ioexception exception) {             throw new runtimeexception(exception);         }     }      public string gettext() {         return textproperty().get();     }      public void settext(string value) {         textproperty().set(value);     }      public stringproperty textproperty() {         return textfield.textproperty();     }      @fxml     protected void dosomething() {         system.out.println("the button clicked!");     } } 

the control working , can call in fxml file using "javafx scence builder" (as shown in picture) :
enter image description here

and fxml file code:

<?xml version="1.0" encoding="utf-8"?>  <?import javafx.scene.text.*?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.anchorpane?>  <anchorpane prefheight="237.0" prefwidth="324.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">    <children>       <javafx.scene.layout.anchorpane layoutx="64.0" layouty="92.0" prefheight="40.0" prefwidth="196.0">          <children>             <textfield fx:id="textfield" layouty="2.0" prefheight="25.0" prefwidth="196.0" />             <button layoutx="72.0" layouty="29.0" mnemonicparsing="false" onaction="#dosomething" text="click me" />          </children>       </javafx.scene.layout.anchorpane>       <label layoutx="75.0" layouty="14.0" text="trying custom control:">          <font>             <font size="15.0" />          </font>       </label>    </children> </anchorpane> 

but problem have redefine dosomething function of custom control button !!!i mean when added custom conrol fxml file functions of custom control should work without redefining swing.
wrong ?

the problem scene builder copies code of custom control new control rather linking it. use custom control have use fx:include fxml tag.

so fxml file should like:

<?xml version="1.0" encoding="utf-8"?>  <?import javafx.scene.text.*?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.anchorpane?>  <anchorpane prefheight="237.0" prefwidth="324.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">    <children>       <fx:include source="yourcustomcontrol.fxml" />       <label layoutx="75.0" layouty="14.0" text="trying custom control:">          <font>             <font size="15.0" />          </font>       </label>    </children> </anchorpane> 

however, have manually. far know, scene builder not capable of doing it.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -