Add setOnAction method to custom Scene Builder Control

admin

Administrator
Staff member
So I followed <a href="https://rterp.wordpress.com/2014/05/21/adding-custom-javafx-components-to-scene-builder-2-0/" rel="nofollow noreferrer">this blog</a> (<a href="https://rterp.wordpress.com/2014/07/28/adding-custom-javafx-component-to-scene-builder-2-0-part-2/" rel="nofollow noreferrer">part 2</a> in particular) to create a custom control and import it into Scene Builder.

This is what it looks like:

<a href=" " rel="nofollow noreferrer"><img src=" " alt="Custom control that holds a ArrayList"></a>

Here is the fxml File:

Code:
&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;?import javafx.scene.control.*?&gt;
&lt;?import java.lang.*?&gt;
&lt;?import javafx.scene.layout.*?&gt;

&lt;fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="150.0" type="AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"&gt;
   &lt;children&gt;
      &lt;HBox alignment="CENTER" layoutX="-65.0" layoutY="-56.0" spacing="5.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
         &lt;children&gt;
            &lt;Button fx:id="previousButton" minWidth="25.0" mnemonicParsing="false" prefWidth="25.0" text="&amp;lt;" /&gt;
            &lt;Label fx:id="label" alignment="CENTER" contentDisplay="CENTER" prefWidth="500.0" text="Label" /&gt;
            &lt;Button fx:id="nextButton" minWidth="25.0" mnemonicParsing="false" prefWidth="25.0" text="&amp;gt;" /&gt;
         &lt;/children&gt;
      &lt;/HBox&gt;
   &lt;/children&gt;
&lt;/fx:root&gt;

Here is the class File:

Code:
package swipeselector;

import java.io.IOException;
import java.util.ArrayList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;

/**
 *
 * @author patrickb
 */
public class SwipeSelector extends AnchorPane {

    @FXML
    private Button previousButton;
    @FXML
    private Label label;
    @FXML
    private Button nextButton;

    ArrayList&lt;String&gt; items;
    int selectedIndex;
    private boolean repetitive;

    public SwipeSelector() {
        FXMLLoader fxmlLoader = new FXMLLoader(
                getClass().getResource("swipeSelectorFXML.fxml"));

        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

        items = new ArrayList&lt;&gt;();
        selectedIndex = 0;

        previousButton.setOnAction((ActionEvent event) -&gt; {
            setPrevious();
        });

        nextButton.setOnAction((ActionEvent event) -&gt; {
            setNext();
        });

    }

    public void setItems(ArrayList&lt;String&gt; items) {
        this.items = items;
        selectFirst();
    }

    public void setPrevious() {
        updateItem(selectedIndex - 1);
    }

    public void setNext() {
        updateItem(selectedIndex + 1);
    }

    public void selectFirst() {
        updateItem(0);
    }

    private void selectLast() {
        updateItem(items.size() - 1);
    }

    private void updateItem() {
        if (items.isEmpty()) {
            label.setText("");
        } else {
            if (selectedIndex &lt; 0) {
                if (repetitive) {
                    selectedIndex = items.size() - 1;
                } else {
                    selectedIndex = 0;
                }
            }
            if (selectedIndex &gt;= items.size()) {
                if (repetitive) {
                    selectedIndex = 0;
                } else {
                    selectedIndex = items.size() - 1;
                }
            }
            label.setText(items.get(selectedIndex));
        }
    }

    private void updateItem(int selectedIndex) {
        this.selectedIndex = selectedIndex;
        updateItem();
    }

    public void setRepetitive(boolean cyclesThrough) {
        this.repetitive = cyclesThrough;
    }

}

Everything works fine, BUT: When I click the next or previous button, I want something to happen in my original project. I would usually do this by adding a
Code:
setOnAction(new EventHandler ...
method to the object. This method does not exist though, so I somehow need to add this to my custom control. <strong>How do I make my custom control invoke a ActionEvent every time one of the two buttons is clicked, and how do I create a setOnAction Method for my object that will work?</strong>