Results 1 to 3 of 3
- 01-07-2011, 11:47 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 1
- Rep Power
- 0
can i listen to variable change in diffrent class
well as the title states: is it possible for me to listen to a variable change in a diffrent class?
im making a game in wich the GUI.java ( within the view package) has to change the labels of a number of buttons if the gameStatus variable in spel.java( in the model package) changes to false.
i want the model package to be able to run even if the view package aint there. This for Junit testing later on ( its for school btw).
however. i cant find a way to do this. at the moment i made a really ugly solution. whenever i change the variable's value i set a timer to timer.start this timer has set its repeat to false. and im listening to it from the GUI.java.
is there a way if a variable is changed just like the way you listen if the timer is firing?
like spel.timer.addActionListener();
but then spel.spelStatus.addCheckIfChangedThingyListener();
i spent 1 hour on google finding something like this but no results ( maybe google and i were better friends if he actually knew what i was looking for in this case).
hope if you guys can help me :D thanks in advance
- 01-08-2011, 12:59 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
If I understand correctly, just write your own Listener interface. This is in my opinion one of the more important design patterns (see Observer pattern - Wikipedia, the free encyclopedia). Java provides a bare-bones way you can implement using pattern using a Property Change Listener
Last edited by doWhile; 01-08-2011 at 02:11 AM.
- 01-08-2011, 01:50 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Note that property change listeners are probably more complex than is really necessary. Just write an entirely new interface, similar to the ActionListener interface.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
different instant variable of same class
By javastuden in forum New To JavaReplies: 1Last Post: 11-20-2009, 05:06 AM -
How can I change the main class of a project?
By batya in forum AWT / SwingReplies: 2Last Post: 11-17-2009, 07:55 PM -
change value in the in the creator class
By livnihai in forum New To JavaReplies: 8Last Post: 10-07-2009, 06:14 AM -
can we change label from action class?
By kishan in forum Advanced JavaReplies: 2Last Post: 04-28-2009, 02:12 PM -
Interface variable to class
By zill in forum Advanced JavaReplies: 6Last Post: 10-11-2008, 03:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks