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 2 of 2

Thread: JButton, adding a Listener to stop and start?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JButton, adding a Listener to stop and start?

    Hello! I'm currently working on a project for my java class. I have it almost completely done, but the last thing I need to do is program 2 jbuttons to turn a radio on and off. I have it coded now where it will start playing the URL I've linked it to, and the code below compiles with the rest of my program, but I'm not sure how to alter it to make it so that when I click it again, it turns off. I tried writing a for loop, but the way I was going about it was clearly not working...I was trying to create the sound file "ac" in the try catch, and then use an if then with ac.loop() and ac.stop(), based on a number being 1 or -1, but my program didn't seem to like that.

    Any suggestions?

    jbtRadio1.addActionListener(new ActionListener() {  //Programs Get Angry Car 1 to play a sound
      	public void actionPerformed(ActionEvent e) {
     
         try {
          URL url = new URL("http://www.angelfire.com/sc/wavsetc8/images/2sexy.wav");
          AudioClip ac = Applet.newAudioClip(url);
         ac.loop();
          } 
    catch (Exception f) {
          System.out.println(f);
        }
    	 }});
     
    jbtRadio2.addActionListener(new ActionListener() {  //Programs Get Angry Car 2 to play a sound
      	public void actionPerformed(ActionEvent e) {
    	 try {
                URL url = new URL("http://www.angelfire.com/sc/wavsetc2/images/500miles.wav");
                 AudioClip ac = Applet.newAudioClip(url);
                ac.loop();
                } 
    catch (Exception f) {
          System.out.println(f);
        }
    	 }});


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JButton, adding a Listener to stop and start?

    You're going to have to declare the AudioClip outside the actionPerformed method. When you click the button, you check whether it's currently playing- if it is, you stop it, if it's not, you start it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Adding an actionlistener to a JButton with no text
    By captain in forum AWT / Java Swing
    Replies: 1
    Last Post: April 5th, 2012, 02:20 PM
  2. Adding a mouse listener to my Draughts game
    By MXA92 in forum Object Oriented Programming
    Replies: 10
    Last Post: March 22nd, 2012, 06:43 PM
  3. Error when adding ActionListener to JButton
    By grimrader22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2011, 06:53 AM
  4. How to Stop a Thread Safley
    By viper_07 in forum Threads
    Replies: 4
    Last Post: July 17th, 2011, 05:26 PM
  5. JButton listener trouble...
    By sparky5783 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 13th, 2011, 01:19 PM