Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30542 > unrolled thread

Problem with JavaFX ChangeListener?

Started byKnute Johnson <eternal@knutejohnson.com>
First post2014-12-11 15:08 -0800
Last post2015-01-01 12:39 +0100
Articles 7 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Problem with JavaFX ChangeListener? Knute Johnson <eternal@knutejohnson.com> - 2014-12-11 15:08 -0800
    Re: Problem with JavaFX ChangeListener? Arne Vajhøj <arne@vajhoej.dk> - 2014-12-11 18:36 -0500
      Re: Problem with JavaFX ChangeListener? Knute Johnson <eternal@knutejohnson.com> - 2014-12-11 15:53 -0800
    Re: Problem with JavaFX ChangeListener? Arne Vajhøj <arne@vajhoej.dk> - 2014-12-11 21:24 -0500
      Re: Problem with JavaFX ChangeListener? Knute Johnson <eternal@knutejohnson.com> - 2014-12-11 18:31 -0800
    Re: Problem with JavaFX ChangeListener? Roedy Green <see_website@mindprod.com.invalid> - 2014-12-31 18:23 -0800
      Re: Problem with JavaFX ChangeListener? Joerg Meier <joergmmeier@arcor.de> - 2015-01-01 12:39 +0100

#30542 — Problem with JavaFX ChangeListener?

FromKnute Johnson <eternal@knutejohnson.com>
Date2014-12-11 15:08 -0800
SubjectProblem with JavaFX ChangeListener?
Message-ID<m6d842$5st$1@dont-email.me>
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

[toc] | [next] | [standalone]


#30543

FromArne Vajhøj <arne@vajhoej.dk>
Date2014-12-11 18:36 -0500
Message-ID<548a2a8b$0$296$14726298@news.sunsite.dk>
In reply to#30542
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

[toc] | [prev] | [next] | [standalone]


#30544

FromKnute Johnson <eternal@knutejohnson.com>
Date2014-12-11 15:53 -0800
Message-ID<m6daoq$ee8$1@dont-email.me>
In reply to#30543
On 12/11/2014 15:36, Arne Vajhøj wrote:
> 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
>
>

Thanks Arne, that was why it wouldn't compile!

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#30545

FromArne Vajhøj <arne@vajhoej.dk>
Date2014-12-11 21:24 -0500
Message-ID<548a51f0$0$300$14726298@news.sunsite.dk>
In reply to#30542
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



[toc] | [prev] | [next] | [standalone]


#30546

FromKnute Johnson <eternal@knutejohnson.com>
Date2014-12-11 18:31 -0800
Message-ID<m6dk16$dp$1@dont-email.me>
In reply to#30545
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 <? 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

OK, I see now.  Thanks for that too Arne.

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#30622

FromRoedy Green <see_website@mindprod.com.invalid>
Date2014-12-31 18:23 -0800
Message-ID<3rb9aa1dkuafl6i6k8nu9cradooe6bcr3f@4ax.com>
In reply to#30542
On Thu, 11 Dec 2014 15:08:08 -0800, Knute Johnson
<eternal@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

[toc] | [prev] | [next] | [standalone]


#30623

FromJoerg Meier <joergmmeier@arcor.de>
Date2015-01-01 12:39 +0100
Message-ID<u8vnibpn960p$.29du55ciig4k.dlg@40tude.net>
In reply to#30622
On Wed, 31 Dec 2014 18:23:33 -0800, Roedy Green wrote:

> On Thu, 11 Dec 2014 15:08:08 -0800, Knute Johnson
> <eternal@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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web