File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Catching change of Boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Catching change of Boolean" Watch "Catching change of Boolean" New topic
Author

Catching change of Boolean

Michal Bury
Greenhorn

Joined: Sep 13, 2006
Posts: 6
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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
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 ]

Ping & DNS - updated with new look and Ping home screen widget
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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.


The Best way to predict your future is to create it - Every great individual common man
Michal Bury
Greenhorn

Joined: Sep 13, 2006
Posts: 6
thank's for helping.
I done it with parallel thread.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
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
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
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/


Paul M. Santa Maria, SCJP
Michal Bury
Greenhorn

Joined: Sep 13, 2006
Posts: 6
You are right. I try it with javabeans.It's better way.
thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Catching change of Boolean
 
Similar Threads
Assertions
Expression instead of condition in if block
'Who wants to be a millionaire?' style practice test
Problem understanding the ! operator.
Rock Paper Scissors