Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass different Generics in a Callback Class?

For a JFXTreeTableColumn I wrote a custom Cell Factory as Callback. Code works fine but what if I want to pass different Generics?

I already tried to pass the Generics as ? or T, S but I definitely did something wrong

public class CallbackImpl implements Callback<TreeTableColumn<Order, String>, TreeTableCell<Order, String>> {

    private final ObservableList<String> paymentData;

    public CallbackImpl(ObservableList<String> paymentData) {
        this.paymentData = paymentData;
    }

    @Override
    public TreeTableCell<Order, String> call(TreeTableColumn<Order, String> tc) {
        ComboBox<String> combo = new ComboBox<>();
        combo.getItems().addAll(paymentData);
        JFXTreeTableCell<Order, String> cell = new JFXTreeTableCell<Order, String>() {
            @Override
            protected void updateItem(String payment, boolean empty) {
                super.updateItem(payment, empty);
                if (empty) {
                    setGraphic(null);
                } else {
                    combo.setValue(payment);
                    setGraphic(combo);
                }
            }
        };
        return cell ;
    }
}

I want to to pass a Table with <DifferentClass, String> or even <DifferentClass, Integer> (I know that i have to change the code for Integer to work).

Usage in FXML Controller: col.setCellFactory(new CallbackImpl(paymentData));

like image 373
Thomas Bernhard Avatar asked Dec 29 '25 04:12

Thomas Bernhard


1 Answers

I just did a quick change to generics. Since I don't have full code, I dunno if it will all work (or if everything I changed SHOULD be changed) but it will give you rough idea how to attempt to do it :D

class CallbackImpl<V, U> implements Callback<TreeTableColumn<V, U>, TreeTableCell<V, U>>
{

    private final ObservableList<U> paymentData;

    public CallbackImpl(ObservableList<U> paymentData) {
        this.paymentData = paymentData;
    }

    @Override
    public TreeTableCell<V, U> call(TreeTableColumn<V, U> tc) {
        ComboBox<U> combo = new ComboBox<>();
        combo.getItems().addAll(paymentData);
        JFXTreeTableCell<V, U> cell = new JFXTreeTableCell<V, U>() {
            @Override
            protected void updateItem(U payment, boolean empty) {
                super.updateItem(payment, empty);
                if (empty) {
                    setGraphic(null);
                } else {
                    combo.setValue(payment);
                    setGraphic(combo);
                }
            }
        };
        return cell ;
    }
}

Just make whole CallbackImpl generic, and then you can specify what you wish to give it when using it.

like image 145
Worthless Avatar answered Dec 31 '25 18:12

Worthless