Results 1 to 3 of 3
  1. #1
    gloomy1991 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    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.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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

  1. different instant variable of same class
    By javastuden in forum New To Java
    Replies: 1
    Last Post: 11-20-2009, 05:06 AM
  2. How can I change the main class of a project?
    By batya in forum AWT / Swing
    Replies: 2
    Last Post: 11-17-2009, 07:55 PM
  3. change value in the in the creator class
    By livnihai in forum New To Java
    Replies: 8
    Last Post: 10-07-2009, 06:14 AM
  4. can we change label from action class?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-28-2009, 02:12 PM
  5. Interface variable to class
    By zill in forum Advanced Java
    Replies: 6
    Last Post: 10-11-2008, 03:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •