Results 1 to 20 of 20
  1. #1
    lostmind is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default Developing a Listener

    Hi guys!

    Im from Brazil, and Im developing a card game Called "TRUCO". What happends is that I need to create (ived dont even know if it is possible) a LISTENER that monitors three especific labels! "campo_rod1" , "campo_rod2" and "campo_rod3" are the Jlabels, that i need to "listen" to... after they change state i need to know what did they turned in to, so i can "process" that..

    This is an e.g of what the listener should do:


    if (campo_rod1.getText().equals("DRAW")) {
    //code here...
    }


    Besides that listener, I need to create another "listener" that "listens" to another label that simbolizes the score... and that label name is: "campo_placarJOG"... If the number gets over 12, then the game is over, and needs to start over...

    if (Integer.parseInt(campo_placarJOG.getText()) >= 12) {
    //code here
    }

    Ived already made some method that bypassed my need, but still I need to know if is possible to do this using listeners, because if it is, my life will get much easier from now on!

    Thanks guys,
    and sorry for my english!

    Bye!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why listen to the JLabel which is listening to the view? Why not listen to model instead -- that which changes the view?

  3. #3
    lostmind is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Thanks for the Replay Fubarable,

    I need to listen to the label, because there are many methods that changes the label.. I dont know if what i said was clear, sorry about if it wasnt!

    Is it possible to listen to a Jlabel?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by lostmind View Post
    Thanks for the Replay Fubarable,

    I need to listen to the label, because there are many methods that changes the label..
    Then then perhaps this is your problem. How about making one single method for changing the label, and placing your listening code inside of this method.


    Is it possible to listen to a Jlabel?
    I've not seen this done.

  5. #5
    lostmind is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Ok.. thank you for your help. I will do that! :)

    Thank you very much!
    Ill post here when im finished

  6. #6
    lostmind is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    ived done it.. very simple!


    public void atualizaRodada() {
    switch (rodadaAtual) {
    case 1:
    campo_rod1.setText(ganhou);
    imitacaoListenerRodada();
    break;


    case 2:
    campo_rod2.setText(ganhou);
    imitacaoListenerRodada();
    break;

    case 3:
    campo_rod3.setText(ganhou);
    imitacaoListenerRodada();
    break;

    }
    }

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by lostmind View Post
    Is it possible to listen to a Jlabel?
    Yep, a JLabel fires a PropertyChangeEvent by the name of "text" when its text is changed. You have to register a PropertyChangeListener using that name and it will be notified when the JLabels' name changes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Glad you've got it working, though I'm not sure what your code does. Just a quick look though suggests that you might benefit from using an array of JLabels rather than a switch statement.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    Yep, a JLabel fires a PropertyChangeEvent by the name of "text" when its text is changed. You have to register a PropertyChangeListener using that name and it will be notified when the JLabels' name changes.

    kind regards,

    Jos
    Ah, I stand corrected, and thanks for the heads up!

    still,... it smells a little funny to me, that we're listening to the wrong thing, but again, I'm no pro.

  10. #10
    lostmind is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Cool guys! Thank you so much...

    I wount need to use the listener afterall, but still is good to know that it can me "listened" to! Thank you Fubarable for the solution and thank you josAH for giving me knowlegde !

    Bye guys!

  11. #11
    lostmind is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Ohh ! In case you guys want to check out the game I am making here is the link do download:

    http://dl.dropbox.com/u/15625458/Truco.jar

    Suggestions are welcome!

    Thanks!

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, if you had an array of JLabel, say called campoRod your method could be simplified and written something like so:


    Java Code:
    public void atualizaRodada() {
      campoRod[rodadaAtual].setText(ganhou);
      imitacaoListenerRodada();
    }

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by lostmind View Post
    Ohh ! In case you guys want to check out the game I am making here is the link do download:
    http://dl.dropbox.com/u/15625458/Truco.jar

    Suggestions are welcome!
    I suggest you upload a jar file that contains your source code. :)

  14. #14
    lostmind is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I suggest you upload a jar file that contains your source code. :)
    Sorry, i didnt understand that... You want to see the source code? :)

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by lostmind View Post
    Sorry, i didnt understand that... You want to see the source code? :)
    I downloaded the Jar file, but it contained no java files, only class files and resources. So it's hard to make suggestions without seeing code. You might consider making a jar file that contains the source (java files) code.

  16. #16
    lostmind is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Ok... Here is the netbeans project ived created:

    (in /dist/ is the jar)

    http://dl.dropbox.com/u/15625458/Truco.rar

  17. #17
    lostmind is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Fubarable i am having BIG BIG trouble in this game because heres how it really is:

    Some one starts (pc our CPU) , then, the 'person' that won the first round, starts the secound, and the person who won the secound, starts the third...

    And the way I made the game, only the person that is playing is going to start... Shes going to start the first round, the secound round and the third.. and that isnt right...

    And i need some help, can you help me by msn or something? because i need to explain how it works, is that ok?
    Last edited by lostmind; 12-24-2010 at 09:41 PM. Reason: forgot to finish up

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your best bet is to try to explain your problem here and if necessary to create a small compilable program that demonstrates the problem (if possible).

  19. #19
    lostmind is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    ok ... should i creat a new thread?


    ---Any how, the problem is the one ived already typed:

    Some one starts (pc our CPU) , then, the 'person' that won the first round, starts the secound, and the person who won the secound, starts the third...

    And the way I made the game, only the person that is playing is going to start... Shes going to start the first round, the secound round and the third.. and that isnt right...


    --- I need to figure a way to 'control' that in my aplication, and i have absoluty no clue in how could i do that!
    Last edited by lostmind; 12-24-2010 at 09:59 PM. Reason: completing my sentense

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by lostmind View Post
    ok ... should i creat a new thread?
    Yes, since you are now discussing a new problem, I would recommend that you start new forum thread for this. Is your app a stand-alone desktop application or is it a network application? -- actually better to respond in your new thread. :)

Similar Threads

  1. possible for developing ops by swing
    By karthi66 in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2010, 06:42 AM
  2. developing a password vault
    By ramesh.8189 in forum Threads and Synchronization
    Replies: 6
    Last Post: 02-03-2009, 07:32 AM
  3. developing a GUI for solaris 10
    By DuceDuceExplorer in forum NetBeans
    Replies: 5
    Last Post: 08-05-2008, 07:00 AM
  4. help in email developing
    By reached in forum New To Java
    Replies: 0
    Last Post: 12-09-2007, 08:55 PM
  5. Developing for Mac
    By jmds in forum NetBeans
    Replies: 0
    Last Post: 11-25-2007, 08:55 PM

Posting Permissions

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