0

ChangeListener has a method changed() with the signature

void changed(ObservableValue<? extends T> observable, T oldValue, T newValue);

But when i tried to do the following it says "incompatible type parameter in lambda expression".

ChangeListener listener1;
listener1 = (ObservableValue<? extends Number> observableValue , Number number, Number number2) -> 

{ textareaDescription.setText((String) options1.get(number2.intValue())); textareaMustCheck.setText((String) options2.get(number2.intValue())); };

however follwoing code works perfectly

 choiceboxStrategy.getSelectionModel().selectedIndexProperty().addListener((ObservableValue<? extends Number> observableValue, Number number, Number number2) -> {
            textareaDescription.setText((String) options1.get(number2.intValue()));
            textareaMustCheck.setText((String) options2.get(number2.intValue()));
MikaelF
  • 3,518
  • 4
  • 20
  • 33
Roker
  • 575
  • 1
  • 5
  • 10
  • Isnt it suppose to be `=>` then `->` ? – Smit Mar 06 '17 at 05:32
  • Corrected, but still doesn't work @Henry – Roker Mar 06 '17 at 05:36
  • 1
    I think we need an [MCVE](http://stackoverflow.com/help/mcve). I don't know how `ObservableValue` or `ChangeListener` are defined, but if I try using other classes instead I don't get an error. So I think we need more info. – ajb Mar 06 '17 at 05:49
  • 1
    `ChangeListener` takes a parameter, but you didn't specify one in the declaration (ex. `ChangeListener listener1`). – MikaelF Mar 06 '17 at 05:50
  • @MikaelF What `ChangeListener` takes a type parameter? [This one](http://docs.oracle.com/javase/8/docs/api/javax/swing/event/ChangeListener.html) doesn't. We weren't given enough info about what classes he's using, so maybe there's another `ChangeListener` that does--but do we have enough info to guess? – ajb Mar 06 '17 at 05:53
  • Sorry, voted to reopen because there's not enough info in the question to know whether this is a raw type issue. – ajb Mar 06 '17 at 05:55
  • @ajb Not sure why you say that. It's easily reproduced and easily fixed by adding the type parameter. – shmosel Mar 06 '17 at 06:01
  • @ajb [This one](https://docs.oracle.com/javase/8/javafx/api/javafx/beans/value/ChangeListener.html) (from JavaFX) does, and I'm pretty sure that's the one OP's using (it has a `changed()` method). – MikaelF Mar 06 '17 at 06:01
  • @shmosel It can only be reproduced if you happen to know where these classes are. There is nothing in the question that indicates that this is JavaFX. The classes aren't in the normal Java 8 javadoc. This makes it not only impossible to answer if you aren't real familiar with JavaFX, it also makes it impossible to know that it involves knowledge I don't know about. – ajb Mar 06 '17 at 06:03
  • @shmosel the `ChangeListener` in JavaFX takes a type parameter; the one in Swing doesn't, and it wasn't specified in the tags which of the two OP was using. – MikaelF Mar 06 '17 at 06:03
  • @shmosel Given that I eventually figured out what was going on, it's OK with me to close as a duplicate. But there really needs to be more information in the question. Even a "javafx" tag would have helped. – ajb Mar 06 '17 at 06:03
  • It's clear from the method signature that it's from JavaFX. I'm not familiar with the class myself, but it wasn't hard to figure out. – shmosel Mar 06 '17 at 06:04
  • @shmosel how is it clear from the method signature??? If you've used JavaFX a lot, then it's clear. If one is not familiar with it, then there's no way to tell. There's no way to tell that it isn't a class they wrote themselves. Anyway, I've added the javafx tag to avoid anyone else being confused. – ajb Mar 06 '17 at 06:06
  • to avoid any confusion yes its javafx. help me out guys. For testing purpose you can put any simple code inside lambda block @shmosel – Roker Mar 06 '17 at 06:23
  • Your question was answered by [@MikaelF](http://stackoverflow.com/questions/42618178/incompatible-type-parameter-in-lambda-expression?noredirect=1#comment72365467_42618178). – shmosel Mar 06 '17 at 06:24
  • I don't think it has been answered. – Roker Mar 06 '17 at 06:31

1 Answers1

1

Well, the answer is quite simple. Your Lambda expression is wrong. Replace it with this version:

    ChangeListener<Number> listener1;

    listener1 = (observableValue , number, number2) -> 

    { textareaDescription.setText((String) options1.get(number2.intValue())); textareaMustCheck.setText((String) options2.get(number2.intValue())); };

You used ChangeListener as a raw type.

Added:

This is a shortened version of your listener which compiles without errors:

    ChangeListener<Number> listener1 = (v, o, n) -> {int i2 = n.intValue();};

If you still see errors then they are caused by your other code which you haven't shown us and so we can't say anything about it.

mipa
  • 10,369
  • 2
  • 16
  • 35