Will
Will

Reputation: 3585

JavaFX CustomControl<T>: Is this possible?

I want to create a simple reusable custom control in JavaFX that is nothing more than a ComboBox with a label over its head that can have the text set.

I would like for it to be usable in JavaFX Scene Builder.

I would also like for it to be able to take a single Generic Parameter <T> to be able to as closely as possible emulate the behavior of the standard ComboBox that is available.

The problem which I am encountering is that when I attempt to set the Controls Controller to Controller<T> in SceneBuilder, I get an error telling me: Controller<T> is invalid for Controller class.

This makes sense as when you call FXMLLoader.load() (after setting the root, classLoader, and Location), there is no way (that I can find) to tell the loader "Oh, and this is a CustomControl."

This is the code I have for the Control:

public class LabeledComboBox<T> extends VBox {
    private final LCBController<T> Controller;
    public LabeledComboBox(){
        this.Controller = this.Load();
    }

    private LCBController Load(){
        final FXMLLoader loader = new FXMLLoader();
        loader.setRoot(this);
        loader.setClassLoader(this.getClass().getClassLoader());
        loader.setLocation(this.getClass().getResource("LabeledComboBox.fxml"));
        try{
            final Object root = loader.load();
            assert root == this;
        } catch (IOException ex){
            throw new IllegalStateException(ex);
        }

        final LCBController ctrlr = loader.getController();
        assert ctrlr != null;
        return ctrlr;
    }
    /*Methods*/
}

This is the Controller class:

public class LCBController<T> implements Initializable {
    //<editor-fold defaultstate="collapsed" desc="Variables">
    @FXML private ResourceBundle resources;

    @FXML private URL location;

    @FXML private Label lbl; // Value injected by FXMLLoader

    @FXML private ComboBox<T> cbx; // Value injected by FXMLLoader
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Initialization">
    @Override public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        this.location = fxmlFileLocation;
        this.resources = resources;
    //<editor-fold defaultstate="collapsed" desc="Assertions" defaultstate="collapsed">
        assert lbl != null : "fx:id=\"lbl\" was not injected: check your FXML file 'LabeledComboBox.fxml'.";
        assert cbx != null : "fx:id=\"cbx\" was not injected: check your FXML file 'LabeledComboBox.fxml'.";
    //</editor-fold>
    }
    //</editor-fold>
    /*Methods*/
}

Clearly there is something that I am missing here. I am really hoping this is possible without having to come up with my own implementation of the FXMLLoader Class (REALLY, REALLY, REALLY REALLY hoping).

Can someone please tell me what I am missing, or if this is even possible?

EDIT 1:

After someone pointed me to a link I may have an idea of how to do this but I'm still not one hundred percent. To me it feels like the Controller class itself can not be created with a generic parameter (I.E.: public class Controller<T>{...} = No Good) That's kind of annoying but I guess makes sense.

Then what about applying Generic parameters to the Methods inside the custom control controller, and making the control itself (not the controller) a generic: like so?

Control:

public class LabeledComboBox<T> extends VBox {...}

Controller:

public class LCBController implements Initializable {
    /*Stuff...*/

    /**
     * Set the ComboBox selected value.
     * @param <T> 
     * @param Value
     */
    public <T> void setValue(T Value){
        this.cbx.setValue(Value);
    }

    /**
     * Adds a single item of type T to the ComboBox.
     * @param <T> ComboBox Type
     * @param Item
     */
    public <T> void Add(T Item){
        this.cbx.getItems().add(Item);
    }

    /**
     * Adds a list of items of type T to the ComboBox.
     * @param <T> ComboBox Type
     * @param Items
     */
    public <T> void Add(ObservableList<T> Items){
        this.cbx.getItems().addAll(Items);
    }

    /**
     * Removes an item of type T from the ComboBox.
     * @param <T> ComboBox Type
     * @param Item
     * @return True if successful(?)
     */
    public <T> boolean Remove(T Item){
        return this.cbx.getItems().remove(Item);
    }
}

Would that work? Is that more along the right track? Again, my desire is nothing more than a ComboBox with a Label on it to tell users what its all about.

Upvotes: 1

Views: 2272

Answers (2)

James_D
James_D

Reputation: 209225

This worked for me, and when I imported the library into SceneBuilder it worked fine:

(Very basic) FXML:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ComboBox?>

<fx:root xmlns:fx="http://javafx.com/fxml/1" type="VBox"
    fx:controller="application.LabeledComboBoxController">
    <Label fx:id="label" />
    <ComboBox fx:id="comboBox" />
</fx:root>

Controller:

package application;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.SingleSelectionModel;

public class LabeledComboBoxController<T> {
    @FXML
    private Label label ;
    @FXML
    private ComboBox<T> comboBox ;

    public void setText(String text) {
        label.setText(text);
    }
    public String getText() {
        return label.getText();
    }
    public StringProperty textProperty() {
        return label.textProperty();
    }

    public ObservableList<T> getItems() {
        return comboBox.getItems();
    }
    public void setItems(ObservableList<T> items) {
        comboBox.setItems(items);
    }

    public boolean isWrapText() {
        return label.isWrapText();
    }

    public void setWrapText(boolean wrapText) {
        label.setWrapText(wrapText);
    }

    public BooleanProperty wrapTextProperty() {
        return label.wrapTextProperty();
    }

    public SingleSelectionModel<T> getSelectionModel() {
        return comboBox.getSelectionModel();
    }
}

Control:

package application;

import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.layout.VBox;

public class LabeledComboBox<T> extends VBox {

    private final LabeledComboBoxController<T> controller ;

    public LabeledComboBox(ObservableList<T> items, String text) {
        controller = load();
        if (controller != null) {
            setText(text);
            setItems(items);
        }
    }

    public LabeledComboBox(ObservableList<T> items) {
        this(items, "");
    }

    public LabeledComboBox(String text) {
        this(FXCollections.observableArrayList(), text);
    }

    public LabeledComboBox() {
        this("");
    }

    private LabeledComboBoxController<T> load() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource(
                    "LabeledComboBox.fxml"));
            loader.setRoot(this);
            loader.load();
            return loader.getController() ;
        } catch (Exception exc) {
            Logger.getLogger("LabeledComboBox").log(Level.SEVERE,
                    "Exception occurred instantiating LabeledComboBox", exc);
            return null ;
        }
    }

    // Expose properties, but just delegate to controller to manage them 
    // (by delegating in turn to the underlying controls):

    public void setText(String text) {
        controller.setText(text);
    }
    public String getText() {
        return controller.getText();
    }
    public StringProperty textProperty() {
        return controller.textProperty();
    }

    public boolean isWrapText() {
        return controller.isWrapText();
    }

    public void setWrapText(boolean wrapText) {
        controller.setWrapText(wrapText);
    }

    public BooleanProperty wrapTextProperty() {
        return controller.wrapTextProperty();
    }

    public ObservableList<T> getItems() {
        return controller.getItems();
    }
    public void setItems(ObservableList<T> items) {
        controller.setItems(items);
    }
    public SingleSelectionModel<T> getSelectionModel() {
        return controller.getSelectionModel();
    }
}

Test code:

package application;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);
            LabeledComboBox<String> comboBox = new LabeledComboBox<String>(
                    FXCollections.observableArrayList("One", "Two", "Three"), "Test");
            root.setTop(comboBox);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Upvotes: 1

sxleixer
sxleixer

Reputation: 159

I'm sure that this construction is not possible as FXML is evaluated at runtime. And generics are already deleted at runtime.

But what's possible to do is to assign a generic to the controller.

FXML implements the Model-View-Controller (MVC) design which is subject to the following topic:

What is MVC (Model View Controller)?


Your question ist also an issue in the following topic:

Setting TableView Generic Type from FXML

Upvotes: 0

Related Questions