Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Problem with JavaFX ChangeListener? Date: Thu, 11 Dec 2014 15:08:08 -0800 Organization: A noiseless patient Spider Lines: 93 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 11 Dec 2014 23:07:46 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="aba33539224e5c782fe0c4053f7756fd"; logging-data="6045"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++K4mwdQUpwjFYLqNqFbZu" User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 Cancel-Lock: sha1:qLUi+hvWxxWrRpgVkq0ioX0eoq8= Xref: csiph.com comp.lang.java.programmer:30542 I'm trying to teach myself JavaFX and have hit a snag. In the program below I add a ChangeListener to a TextField. The Java8 method works fine, adding an anonymous ChangeListener class works fine but I can't create a class that implements ChangeListener and get it to compile. 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 ? Thanks, knute... import javafx.application.*; import javafx.beans.*; import javafx.beans.property.*; import javafx.beans.value.*; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; import java.time.*; public class test99 extends Application { public static void main(String... args) { launch(args); } public void start(Stage primaryStage) { primaryStage.setTitle("test99"); VBox root = new VBox(10); Scene scene = new Scene(root,320,240); TextField textField = new TextField(); textField.setOnAction(ae -> System.out.println(textField.getText())); textField.textProperty().addListener((observable,oldValue,newValue) -> { ((StringProperty)observable).setValue(newValue.toLowerCase()); }); /* 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)); */ textField.textProperty().addListener((observable,oldValue,newValue) -> { ((StringProperty)observable).setValue(newValue.length() > 4 ? newValue.substring(0,4) : newValue); }); /* textField.textProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, String oldValue,String newValue) { ((StringProperty)observable).setValue(newValue.length() > 4 ? newValue.substring(0,4) : newValue); } }); */ textField.setText("Hello World!"); root.getChildren().add(textField); primaryStage.setScene(scene); primaryStage.show(); } } -- Knute Johnson