Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Catching change of Boolean

 
Michal Bury
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • 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 ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring 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.
 
Ilja Preuss
author
Sheriff
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.
 
Tim LeMaster
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
 
Paul Santa Maria
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic