EDIT: I fixed my code and managed to get the song to work on the first radio button. But now If i unclick the button, the song will just start over instead of stopping. How do I make the song stop once the button has been clicked again, i tried a null but its not working.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.AudioClip;
import java.net.URL;
public class RadioControls extends JPanel
{
private JRadioButton b1, b2, b3, b4, b5, b6;
private AudioClip[] music;
private AudioClip current;
private JSlider vSlider;
private JCheckBox airC;
//-----------------------------------------------------------------
// Sets up the GUI
//-----------------------------------------------------------------
public RadioControls()
{
URL url1, url2, url3, url4, url5, url6;
url1 = url2 = url3 = url4 = url5 = url6 = null;
// Obtain and store the audio clips to play
try
{
url1 = new URL ("file", "localhost", "westernBeat.wav");
url2 = new URL ("file", "localhost", "classical.wav");
url3 = new URL ("file", "localhost", "jeopardy.au");
url4 = new URL ("file", "localhost", "newAgeRythm.wav");
url5 = new URL ("file", "localhost", "eightiesJam.wav");
url6 = new URL ("file", "localhost", "hitchcock.wav");
}
catch (Exception exception) {}
music = new AudioClip[7];
music[0] = null; // Corresponds to "Make a Selection..."
music[1] = JApplet.newAudioClip (url1);
music[2] = JApplet.newAudioClip (url2);
music[3] = JApplet.newAudioClip (url3);
music[4] = JApplet.newAudioClip (url4);
music[5] = JApplet.newAudioClip (url5);
music[6] = JApplet.newAudioClip (url6);
b1 = new JRadioButton("1");
b1.setBackground(Color.yellow);
b2 = new JRadioButton("2");
b2.setBackground(Color.yellow);
b3 = new JRadioButton("3");
b3.setBackground(Color.yellow);
b4 = new JRadioButton("4");
b4.setBackground(Color.yellow);
b5 = new JRadioButton("5");
b5.setBackground(Color.yellow);
b6 = new JRadioButton("6");
b6.setBackground(Color.yellow);
//slider
vSlider = new JSlider(JSlider.HORIZONTAL, 1, 5, 1);
vSlider.setMinorTickSpacing(1);
vSlider.setMajorTickSpacing(1);
vSlider.setPaintTicks(true);
//air
airC = new JCheckBox("On/Off");
airC.setBackground(Color.cyan);
//add buttons
add (b1);
add (b2);
add (b3);
add (b4);
add (b5);
add (b6);
add (vSlider);
add (airC);
b1.addActionListener (new ButtonListener());
}
//*****************************************************************
// Represents the action listener for both control buttons.
//*****************************************************************
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();
if(source==b1)
music[1].play();
//I think the problem is here****//
if(source==null)
music[0].play();
}
}
}
The problem is your code runs everytime an event occurs on the button (regardless check or uncheck)
You need to check the button status like this:
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();
if(source==b1){
if(b1.isSelected()){
music[1].play();
}
else{
music[1].stop()
}
}
}
You should also consider stopping the songs with .stop() rather then pointing the a null song.