Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with JavaFX ChangeListener?

417 views
Skip to first unread message

Knute Johnson

unread,
Dec 11, 2014, 6:08:18 PM12/11/14
to
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 <? extends String>?

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<String> implements ChangeListener<String> {
private final int length;

public LengthListener(int length) {
this.length = length;
}

@Override public void changed(
ObservableValue<? extends String> 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<String>() {
@Override
public void changed(ObservableValue<? extends String>
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

Arne Vajhøj

unread,
Dec 11, 2014, 6:36:53 PM12/11/14
to
On 12/11/2014 6:08 PM, Knute Johnson wrote:
> 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.

> class LengthListener<String> implements ChangeListener<String> {
^^^^^^^^ remove this (it has a very bad
impact when String is used later!)
> private final int length;
>
> public LengthListener(int length) {
> this.length = length;
> }
>
> @Override public void changed(
> ObservableValue<? extends String> observable,
> String oldValue,String newValue) {
> if (newValue.length() > length)
> ((StringProperty)observable).setValue(
> newValue.substring(0,length));
> }
> }
> textField.textProperty().addListener(new LengthListener(4));

Arne


Knute Johnson

unread,
Dec 11, 2014, 6:53:25 PM12/11/14
to
Thanks Arne, that was why it wouldn't compile!

--

Knute Johnson

Arne Vajhøj

unread,
Dec 11, 2014, 9:25:04 PM12/11/14
to
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 <? extends String>?
> class LengthListener<String> implements ChangeListener<String> {
> private final int length;
>
> public LengthListener(int length) {
> this.length = length;
> }
>
> @Override public void changed(
> ObservableValue<? extends String> 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




Knute Johnson

unread,
Dec 11, 2014, 9:31:37 PM12/11/14
to
OK, I see now. Thanks for that too Arne.

--

Knute Johnson

Roedy Green

unread,
Dec 31, 2014, 9:23:49 PM12/31/14
to
On Thu, 11 Dec 2014 15:08:08 -0800, Knute Johnson
<ete...@knutejohnson.com> wrote, quoted or indirectly quoted someone
who said :

> class LengthListener<String> implements ChangeListener<String> {

Look at the code for ChangeListener

public interface ChangeListener extends EventListener

There is no <xxxx>. You can't just make one up.

--
Roedy Green Canadian Mind Products http://mindprod.com
Software is a gas; it expands to fill its container.
~ Nathan Myhrvold (born: 1959-08-03 age: 55) Nathan’s First Law

Joerg Meier

unread,
Jan 1, 2015, 6:39:40 AM1/1/15
to
On Wed, 31 Dec 2014 18:23:33 -0800, Roedy Green wrote:

> On Thu, 11 Dec 2014 15:08:08 -0800, Knute Johnson
> <ete...@knutejohnson.com> wrote, quoted or indirectly quoted someone
> who said :
>> class LengthListener<String> implements ChangeListener<String> {
> Look at the code for ChangeListener

> public interface ChangeListener extends EventListener

> There is no <xxxx>. You can't just make one up.

javax.swing.event.ChangeListener

is not the same as

javafx.beans.value.ChangeListener

the latter is defined as

public interface ChangeListener<T>

This is a good example of why using * imports is a bad idea. I recommend
getting a modern IDE to prevent such issues.

Liebe Gruesse,
Joerg

--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
0 new messages