Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For JavaFx 2.1, You can create a custom FXML control component by this way:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;?import java.lang.*?&gt; &lt;?import java.util.*?&gt; &lt;?import javafx.scene.*?&gt; &lt;?import javafx.scene.control.*?&gt; &lt;?import javafx.scene.layout.*?&gt; &lt;?import customcontrolexample.myCommponent.*?&gt; &lt;VBox xmlns:fx="http://javafx.com/fxml" fx:controller="customcontrolexample.FXML1Controller"&gt; &lt;children&gt; &lt;MyComponent welcome="1234"/&gt; &lt;/children&gt; &lt;/VBox&gt; </code></pre> <p>Component code:</p> <p>MyComponent.java</p> <pre><code>package customcontrolexample.myCommponent; import java.io.IOException; import javafx.beans.property.StringProperty; import javafx.fxml.FXMLLoader; import javafx.scene.Node; import javafx.scene.layout.Pane; import javafx.util.Callback; public class MyComponent extends Pane { private Node view; private MyComponentController controller; public MyComponent() { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("myComponent.fxml")); fxmlLoader.setControllerFactory(new Callback&lt;Class&lt;?&gt;, Object&gt;() { @Override public Object call(Class&lt;?&gt; param) { return controller = new MyComponentController(); } }); try { view = (Node) fxmlLoader.load(); } catch (IOException ex) { } getChildren().add(view); } public void setWelcome(String str) { controller.textField.setText(str); } public String getWelcome() { return controller.textField.getText(); } public StringProperty welcomeProperty() { return controller.textField.textProperty(); } } </code></pre> <p>MyComponentController.java</p> <pre><code>package customcontrolexample.myCommponent; import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.TextField; public class MyComponentController implements Initializable { int i = 0; @FXML TextField textField; @FXML protected void doSomething() { textField.setText("The button was clicked #" + ++i); } @Override public void initialize(URL location, ResourceBundle resources) { textField.setText("Just click the button!"); } } </code></pre> <p>myComponent.fxml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;?import java.lang.*?&gt; &lt;?import java.util.*?&gt; &lt;?import javafx.scene.*?&gt; &lt;?import javafx.scene.control.*?&gt; &lt;?import javafx.scene.layout.*?&gt; &lt;VBox xmlns:fx="http://javafx.com/fxml" fx:controller="customcontrolexample.myCommponent.MyComponentController"&gt; &lt;children&gt; &lt;TextField fx:id="textField" prefWidth="200.0" /&gt; &lt;Button mnemonicParsing="false" onAction="#doSomething" text="B" /&gt; &lt;/children&gt; &lt;/VBox&gt; </code></pre> <p>This code needs to check if there is no memory leak.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload