1

I got a String in my model that I want to keep track of (the String represents a continent). If the continent changes, a ListView should be updated in the gui with the correct countries for that continent.

String selectedContinent;
ObservableValue continent = (ObservableValue) selectedContinent;
continent.addListener( ......

Obviously I get error: incompatible types: String cannot be converted to ObservableValue.

I've previously used ObservableList, etc.. But this ObservableValue is new to me. After searching I found alot related to Property etc.. but nothing I could learn from sorta.

ManyQuestions
  • 1,069
  • 1
  • 16
  • 34
  • look at the following post http://stackoverflow.com/questions/14413040/converting-integer-to-observablevalueinteger-in-javafx – sasankad Feb 20 '15 at 02:51
  • Can you use [StringProperty](http://docs.oracle.com/javafx/2/api/javafx/beans/property/StringProperty.html) instead? Not familiar with javafx myself, but a quick Google search makes it seems like you can use StringProperty (which implements ObservableValue via Property interface) to wrap your selectedContinent String and then call addListener on it. – Ryan Thames Feb 20 '15 at 03:35
  • What is the added value that a Property has then? I'll see what I can do. – ManyQuestions Feb 20 '15 at 03:37
  • You should be able to create a new instance of StringProperty and call setValue(selectedContinent) on it to wrap your selectedContinent in it, then you can take advantage of the methods of StringProperty (addLIstener, etc.) – Ryan Thames Feb 20 '15 at 03:48
  • The added value that a `StringProperty` has is that it is an `ObservableValue`, so you can add listeners to it. A `String` is not an `ObservableValue`. – James_D Feb 20 '15 at 03:54

1 Answers1

3

You could use a StringProperty instead. You can't instantiate StringProperty since it is an abstract class so use SimpleStringProperty.

e.g.

SimpleStringProperty selectedContinent = new SimpleStringProperty;
selectedContinent.setValue(selection);
selectedContinent.addListener(....
ManyQuestions
  • 1,069
  • 1
  • 16
  • 34