• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to reference the parent class from a ChangeListener

 
Ranch Hand
Posts: 167
1
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a slew of TextFields, where I need to place a ChangeListener on their respective focusedProperty. Instead of creating a ChangeListener for each individual TextField, I want to write a generic ChangeListener<Boolean> one time, then re-use that by assigning it to each TextFields focusedProperty. Then, within that ChangeListener, I would like to use the observable object passed into the ChangeListener to somehow gain reference back to the TextField class so that I can get to the actual text that the user typed into the TextField itself.

So the way it would work, is when someone types text into the a TextField, then tabs out of the field, the focusedProperty ChangeListener that I assigned will trigger where I can then take action on the text that the user typed.

Is it possible to do this?

Thank you,

Mike
 
Saloon Keeper
Posts: 12142
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure! Usually, the first parameter of listeners holds the source of the event. Cast it to the textfield type and you're done.

There is a more elegant way however. Since you have a custom class, provide it with a constructor that accepts the textfield that the listener will be handling. Assign it to a field and then when the event triggers you can access the field to get the source of the event without having to cast it.
 
Marshal
Posts: 25677
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Stephan proposes in his second paragraph is a good solution (and it's probably the way I would do it too). However it creates one instance of the custom listener class for each TextField. But it seems that you want to create only one instance, and have it handle all of the TextFields. You didn't exactly say that you had that goal in mind, but if you did, I would just suggest it isn't really a worthy goal. Unless you have many thousands of TextFields, the memory used by creating one listener per TextField isn't worth worrying about.
 
Michael D Sims
Ranch Hand
Posts: 167
1
IntelliJ IDE MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:I would just suggest it isn't really a worthy goal. Unless you have many thousands of TextFields, the memory used by creating one listener per TextField isn't worth worrying about.


I sort of worked myself into that conclusion while I was waiting for a reply to my post. I always look for more efficient ways to code wherever possible, even if the time/value benefit doesn't exist. It's code for personal use so no real loss in the learning curve.



Cheers!
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic