Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Catching change of Boolean

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new in JAVA programming.
Can anybody explain to me , how can I catch a change of Boolen expression and then make a action.
I need to alter a Boolean expression (it's public) from different point's of program, and after any change start some other code.

thanks.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question is a bit vague. What do you mean by "catch a change of a boolean expression"? Can you show us some code to explain in more detail what exactly you want to do and what you don't understand?

You can use if ... else statements to execute statements depending on if a boolean expression evaluates to true or false.
 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case you're talking about a boolean field, make sure it's not public, but private, and always accessed through a setter, i.e. a method like

Then you can add code to this method to do whatever needs to be done when the value changes.
[ September 13, 2006: Message edited by: Ulf Dittmer ]
 
Ranch Hand
Posts: 1249
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Might be I didn't get the question. But if you need to track a Boolean variable in Whole Application, then it would be better to run a parallel thread to check whether the value of boolean variable has been changed or not.


I am sorry if I mislead the thread. But I suggested what I thought about.
 
Michal Bury
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank's for helping.
I done it with parallel thread.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ankur Sharma:
Well Might be I didn't get the question. But if you need to track a Boolean variable in Whole Application, then it would be better to run a parallel thread to check whether the value of boolean variable has been changed or not.



Better than what?

I think typically the best solution would be to encapsulate the boolean in an observable object - an object that fires a change event every time the value changes.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You definately don't need a thread to monitor this boolean.

Take a look at PropertyChangeListener - its part of the JavaBean spec but most people seem to treat JavaBean as get/set.

There is a class PropertyChangeSupport that makes it easy to add Propery Change Events to a JavaBean class. Then in your setBoolean method you can code to fire the property change event.

Any interested party can add itself as a property change listener.
 
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:

Better than what?

I think typically the best solution would be to encapsulate the boolean in an observable object - an object that fires a change event every time the value changes.



Agreed. Using a thread is not really a good idea here.

There is no way for this "parallel thread" to be informed of the changes, so it must check periodically -- which depending on how often it changes, you are wasting lots of time just checking.

Henry
 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael -

Like Tim suggested, it sounds like you really want to implement your class as a Java bean, and use "Events" to notify any other interested classes about any "PropertyChanges" in your bean.

Here's a good tutorial:
http://java.sun.com/docs/books/tutorial/javabeans/
 
Michal Bury
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. I try it with javabeans.It's better way.
thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!