0

In a JavaFX application I attached a ChangeListener to a TableCell's tableRowProperty, which is of type ChangeListener<? super TableRow> (and TableRow<T> is generic too).

What I did was the following:

public final class PairingResultEditingCell extends TableCell<Pairing, Result> {

    private final ChoiceBox<Result> choiceField;

    // Unchecked casts and raw types are needed to wire the
    // tableRowProperty changed listener
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private PairingResultEditingCell() {

        super();
        this.choiceField = new ChoiceBox<Result>();
        // ReadOnlyObjectProperty<TableRow> javafx.scene.control.TableCell.tableRowProperty()
        this.tableRowProperty()
            // this cast is the actual source of the warnings
            // rawtype of TableRow<T>: ChangeListener<? super TableRow>
            .addListener((ChangeListener<? super TableRow>) new ChangeListener<TableRow<Result>>() {

                @Override
                public void changed(
                        final ObservableValue<? extends TableRow<Result>> observable,
                        final TableRow<Result> oldValue,
                        final TableRow<Result> newValue) {
                    choiceField.setVisible(newValue.getItem() != null);
                }
            });
    }
}

I need two suppress two sorts of warnings to do this: @SuppressWarnings({ "unchecked", "rawtypes" }). The rawtype warning appears to be Eclipse only. The Jenkins CI server, however, refuses to compile the code because of the former (and I cannot change its configuration).

Is there a way to do this without unchecked casts and raw types? I tried an inner class implementing the interface, but I got stuck. I'm also struggling with Java's ? super MyClass syntax in general.

Matthias Meid
  • 12,455
  • 7
  • 45
  • 79
  • Can you show the class definition? – assylias Jun 20 '12 at 10:09
  • Of course, thanks for reading so far. I added the declaration of my `PairingResultEditingCell` class and some links to the Oracle Docs. – Matthias Meid Jun 20 '12 at 10:49
  • Where is your `tableRowProperty` defined? More importantly, what is its generic type? `ReadOnlyObjectProperty` is generic so we must assume that the definition of `tableRowProperty` is generic. – John B Jun 20 '12 at 11:07
  • At least one place where you are getting the `raw` warning is `(ChangeListener super TableRow>` since `TableRow` is generic. – John B Jun 20 '12 at 11:08
  • Updated the question with two code inline comments. – Matthias Meid Jun 20 '12 at 12:46

1 Answers1

1

I don't get any warnings with the following code:

public final class PairingResultEditingCell extends TableCell<Pairing, Result> {

    private final ChoiceBox<Result> choiceField;

    private PairingResultEditingCell() {

        super();
        this.choiceField = new ChoiceBox<Result>();

        ReadOnlyObjectProperty<TableRow> roop= this.tableRowProperty();
        this.tableRowProperty().addListener(new ChangeListener<TableRow>() {
            @Override
            public void changed(ObservableValue<? extends TableRow> observable, TableRow oldValue, TableRow newValue) {
                choiceField.setVisible(newValue.getItem() != null);
            }
        });
    }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • Thanks. I still get the raw type warning, but I could get rid of the unchecked cast with this. – Matthias Meid Jun 20 '12 at 12:53
  • Will perhaps be the cleanest I can get and the CI server is happy with it. So the actual problem is solved. – Matthias Meid Jun 20 '12 at 13:09
  • 1
    @assylias. I guess you are using Netbeans. My colleague using Eclipse is getting the rawtype warning while I am not in Netbeans on the same project. Netbeans just pre-configured to not show this type of warnings I assume. – Uluk Biy Jun 20 '12 at 13:54
  • @UlukBiy Correct. No warning on netbeans (but I don't get a warning when I compile either). – assylias Jun 20 '12 at 13:55