thread with MouseListener

Discussion in 'Java' started by devphylosoff, Dec 22, 2006.

  1. devphylosoff

    devphylosoff Guest

    hej

    1.
    I have big class extends Frame (electronic clock) and implemented 3
    interfaces:WindowListener, ActionListener, MouseListener.

    just now I want to add Alarm, which can be closed by any from 2 buttons
    clicked by mouse
    i think that the best idea is run new thread with countdown to start
    alarm (because of avoid alarm check in some states of my clock)

    ok, so i will have new thread with countdown
    when it reaches 0 alarm function starting and beeper beep through 1
    minute or until user press any key (it means : click any button). I
    expect MouseListener with my parent class will be sleep in this case.

    how write MouseListener in alarm thread?

    2.
    btw: how use static variable in functions
    I receive error while compiling code with that declaration in function
    - only in class scope I can do that.
    devphylosoff, Dec 22, 2006
    #1
    1. Advertising

  2. devphylosoff wrote:
    > I have big class extends Frame (electronic clock) and implemented 3
    > interfaces:WindowListener, ActionListener, MouseListener.


    I suggest start by refactoring your code into something much simpler.
    Bad text books like to extend Frame and implement loads of classes
    because it reduces line count slightly. However, it does produce bad
    code. There is rarely any need to extend Frame. Use inner classes for
    listeners.

    BTW: AWT components are very old fashioned. There is little reason not
    to use Swing.

    > just now I want to add Alarm, which can be closed by any from 2 buttons
    > clicked by mouse
    > i think that the best idea is run new thread with countdown to start
    > alarm (because of avoid alarm check in some states of my clock)


    You really want to avoid threads if you can. If you use
    javax.swing.Timer, the actions are fired in the same thread as input
    events are fired in (the Event Dispatch Thread, EDT).

    > ok, so i will have new thread with countdown
    > when it reaches 0 alarm function starting and beeper beep through 1
    > minute or until user press any key (it means : click any button). I
    > expect MouseListener with my parent class will be sleep in this case.


    Don't do anything *too* complicated within listener methods. If it
    starts to get complicated, or you feel the need to call the listener
    methods yourself, have them call another method having extracted any
    relevant information from the event object. The interface of the new
    method should make sense in the situation it is defined.

    > btw: how use static variable in functions
    > I receive error while compiling code with that declaration in function
    > - only in class scope I can do that.


    You can't (well there is a hack to do with local classes). In general,
    you should avoid any mutable static. Just place your constants in the
    outer class.

    Tom Hawtin
    Thomas Hawtin, Dec 22, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian J. Sayatovic
    Replies:
    1
    Views:
    5,061
    Kleopatra
    Jul 31, 2003
  2. Peter the Swede

    MouseListener and hiding components.

    Peter the Swede, Nov 19, 2003, in forum: Java
    Replies:
    0
    Views:
    345
    Peter the Swede
    Nov 19, 2003
  3. Minti
    Replies:
    2
    Views:
    458
    Ian Shef
    Feb 19, 2004
  4. Cammy

    Newbie MouseListener Problem

    Cammy, Apr 9, 2004, in forum: Java
    Replies:
    2
    Views:
    509
    Knute Johnson
    Apr 10, 2004
  5. Jenny

    Interface MouseListener

    Jenny, Aug 19, 2004, in forum: Java
    Replies:
    1
    Views:
    342
    Lee Weiner
    Aug 20, 2004
Loading...

Share This Page