Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: ActionListener in Java

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default ActionListener in Java

    I wonder whether everything else stops while ActionListener running?
    If I loop a button ActionListener when this button is pressed, I can not invoke another button ActionListener
    I want to toggle 2 actions of 2 buttons:
    - Press btn1 --> do btn1 ActionListener
    { Flag1 = true, Flag2 = false
    while Flag1 = true, do action1
    }

    - Press btn2 --> do btn2 ActionListener
    { Flag1 = false, Flag2 = true
    while Flag2 = true, do action2
    }

    But when invoke btn1, everything else is out of control

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ActionListener in Java

    everything else stops while ActionListener running
    ActionListeners run on the Event Dispatch Thread which is the same thread used by Swing. If any code running on the EDT uses too much time, the responsiveness of all GUI components will be delayed until the code using the EDT ends.

    If there is a need for a long running task, better to have the code for that task run on its own, separate thread.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: ActionListener in Java

    Quote Originally Posted by Norm View Post
    ActionListeners run on the Event Dispatch Thread which is the same thread used by Swing. If any code running on the EDT uses too much time, the responsiveness of all GUI components will be delayed until the code using the EDT ends.

    If there is a need for a long running task, better to have the code for that task run on its own, separate thread.
    In this case, if a button invokes to sound a midi note, how can you prolong the sound until you click the button STOP or another button?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ActionListener in Java

    Does the class that makes the sound have methods to call to stop the sound?
    If the code that makes the sound is run on another thread, not the EDT, then buttons will be responsive and their action listeners can call the method to stop the sound.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. ActionListener inside another ActionListener
    By kpat in forum AWT / Java Swing
    Replies: 6
    Last Post: March 28th, 2012, 03:43 PM
  2. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 15
    Last Post: February 10th, 2011, 02:21 AM
  3. ActionListener help
    By QBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 12:25 PM
  4. [SOLVED] ActionListener help
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 14th, 2010, 06:57 PM
  5. New Java user ActionListener question
    By VBGuy in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 20th, 2010, 09:46 PM