Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: Problem with JavaFX ChangeListener? Date: Thu, 11 Dec 2014 18:31:21 -0800 Organization: A noiseless patient Spider Lines: 41 Message-ID: References: <548a51f0$0$300$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 12 Dec 2014 02:31:02 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="aba33539224e5c782fe0c4053f7756fd"; logging-data="441"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+E9JvmfeVXM9K6W2NvT+vO" User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 In-Reply-To: <548a51f0$0$300$14726298@news.sunsite.dk> Cancel-Lock: sha1:1XbqVQXzkgLBhjeGdiJTs9E4+J8= Xref: csiph.com comp.lang.java.programmer:30546 On 12/11/2014 18:24, Arne Vajhøj wrote: > On 12/11/2014 6:08 PM, Knute Johnson wrote: >> The other question I have is why does the ObservableValue require a cast >> to StringProperty in both of the working examples and why do I declare >> it as ? >> class LengthListener implements ChangeListener { >> private final int length; >> >> public LengthListener(int length) { >> this.length = length; >> } >> >> @Override public void changed( >> ObservableValue observable, >> String oldValue,String newValue) { >> if (newValue.length() > length) >> ((StringProperty)observable).setValue( >> newValue.substring(0,length)); >> } >> } >> textField.textProperty().addListener(new LengthListener(4)); > > Formal type of observable: > > interface javafx.beans.value.ObservableValue > > Actual ype of observable: > > class javafx.scene.control.TextInputControl$TextProperty > **** class javafx.beans.property.StringProperty > ******** interface javafx.beans.value.ObservableValue > > The cast is necessary! > > Arne OK, I see now. Thanks for that too Arne. -- Knute Johnson