954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Listeners

Is there anyway to listen to a bool value? I have a login window that has a loggedIn boolean value, I need to watch that value in the main thread so when it changes to true it returns to the main thread and loads up the rest of the program, i used to have it trailing through the program, ie, if you logged in the login listener would then start the main program but due to threading problems i am trying to keep it returning the the main thread to control it all. If there is another way i havent thought of that doesnt use listeners let me know, I dont really want to have to use a loop which checks it all the time.

Black Knight
Light Poster
25 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

I think you need to look at the Observer class. I'm not entirely sure how to use it, but it sounds like thats what you need.

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

why don't you make a method which assigns true instead of just declaring loggedIn = true;

in other words

loggedIn = setTrue();

public boolean setTrue()
{
     //do some stuff when the variable is set true here!
     return true;
}
paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 

I don't think you would have to use loops to continously check to see if it's true. I think you could just use some simple if statements or something simple to check if it's true.

Paradox has a neat solution to it..Have a method that makes it true, and also can perform any other task you want if the boolean is true. If it were me, I would go that route. Nice solution Paradox!

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

i already have the set and get methods, the problem is the true bool is an attrib of the gui and you cant return values from a gui. so an if statement wouldnt work either, i need the main thread to watch the awt thread for the bool change and then shut down that gui and start another one. I think Phaelax was hitting nearer the mark with the observer class, I had looked at it but was playing with invokeandwait() thread method.

Black Knight
Light Poster
25 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

ahhh... it's a gui component, well then just implement the appropiate listener. You should have said this from the beggining it's kind of hard to guess what you are doing. What component are you using? I will show you how to implement the appropiate listener if you do not know how to.

paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 

Your component can fire for example a PropertyChangedEvent and your other class register itself with the component as a PropertyChangeListener (or whatever it's called).

Needs a bit of code, but I'm sure most decent books and the Java tutorial have ample examples.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

well in i want the main thread to basically wath the gui it creates and as soon as the bool in the gui flips to true i want it to dispose of the log in window and open the main app window, i have tried to implement propertychangelisteners and changelisteners etc... to no avail, i did have it creating the main window through the login listener but i am trying to code it a different way now

Black Knight
Light Poster
25 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

I think i will look further into propertychangeevents

Black Knight
Light Poster
25 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You