2

I am attempting to create a simple applet that plays an audio file when a play or loop button is clicked and then stops when stop is clicked. However for some reason my event handling is not working properly and I cannot figure out why.

Here is my code:

import java.applet.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;


public class JWater extends JApplet {

public void init()
{
    sound = getAudioClip(getCodeBase(),"water001.au"); //Set audio file

    //Prepare to try and create GUI
    try
    {
        SwingUtilities.invokeAndWait(new Runnable()
        {
            public void run()
            {                   
                makeGUI(); //Create the GUI                         
            }
        });
    }

    catch (Exception e)
    {
        System.err.println("GUI was not created successfully"); //In case something goes wrong
    }               
}

private void makeGUI() {

    //Create content pane which everything will reside in
    setBounds(100, 100, 317, 189);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    setVisible(true);

    //Create and add panel, buttons and the label will be added to this 
    JPanel panel = new JPanel();
    panel.setBounds(0, 0, 300, 150);
    contentPane.add(panel);
    panel.setLayout(null);

    //Create and add the label
    JLabel lblNewLabel = new JLabel("This is the sound of flowing water.");
    lblNewLabel.setBounds(54, 46, 250, 14);
    panel.add(lblNewLabel);

    jOP = new JOptionPane();
    jOP.setBounds(10,40,250,10);
    panel.add(jOP);


    //Create and add media control buttons      
    JButton btnPlay = new JButton("Play");
    btnPlay.setBounds(10, 116, 89, 23);
    panel.add(btnPlay);

    JButton btnLoop = new JButton("Loop");
    btnLoop.setBounds(109, 116, 89, 23);
    panel.add(btnLoop);

    JButton btnStop = new JButton("Stop");
    btnStop.setBounds(208, 116, 89, 23);
    panel.add(btnStop);

    //Create an event handler named handler
    EventHandler handler = new EventHandler();

    //Add these action listeners to detect when a button is clicked
    btnPlay.addActionListener(handler);
    btnLoop.addActionListener(handler);
    btnStop.addActionListener(handler);
}

//Implement the event handler class for button clicks
    class EventHandler implements ActionListener
    {       
        public void actionPerformed(ActionEvent event)
        {
            if (event.getSource() == btnPlay)   
            {

            }

            else if (event.getSource() == btnLoop)  
            {

            }

            else if (event.getSource() == btnStop)  
            {                           
            }

            else
            {           
                JOptionPane.showMessageDialog(null,"Message not detected or not sent!!!  Recevied " + event.getSource(),"ERROR CODE 1",JOptionPane.ERROR_MESSAGE);
            }
        }       
    }

    AudioClip sound;
    JPanel contentPane,panel;
    JButton btnPlay,btnLoop,btnStop;
    JOptionPane jOP;
}

When ever a button is clicked it sails through my if statements and lands on else in the debugger. I think there may be something wrong within the EventHandler class but I am not sure where it is. I have used this method in other programs and it works just fine but not with this one.

Geowil
  • 624
  • 1
  • 12
  • 36
  • BTW - nicely formed question. I especially appreciated that you put compilable code which used the correct way to create a Swing applet, and did not ignore exceptions. – Andrew Thompson Jul 29 '13 at 01:49

1 Answers1

4
//Create and add media control buttons      
JButton btnPlay = new JButton("Play");
btnPlay.setBounds(10, 116, 89, 23);
panel.add(btnPlay);

JButton btnLoop = new JButton("Loop");
btnLoop.setBounds(109, 116, 89, 23);
panel.add(btnLoop);

JButton btnStop = new JButton("Stop");
btnStop.setBounds(208, 116, 89, 23);
panel.add(btnStop);

Should be:

//Create and add media control buttons      
btnPlay = new JButton("Play");
btnPlay.setBounds(10, 116, 89, 23);
panel.add(btnPlay);

btnLoop = new JButton("Loop");
btnLoop.setBounds(109, 116, 89, 23);
panel.add(btnLoop);

btnStop = new JButton("Stop");
btnStop.setBounds(208, 116, 89, 23);
panel.add(btnStop);

To fix the immediate problem you see. By adding JButton before the construction of the button, the code is effectively re-declaring them as local variables. Thereby 'shadowing' the class attributes that the action listener is comparing against.

Further tips

  1. setBounds(100, 100, 317, 189); Remove that. The applet size should be set in HTML.
  2. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space.

    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.
    1. Nested layout
    2. White Space

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *face palm* So the code was creating new button elements, that were not defined in terms of the variable name, and so the handler was not detecting those names since they were not included in the if statements? Man, epic phail on my part lol. Thanks. – Geowil Jul 29 '13 at 01:45
  • ..and in case anyone is thinking I am exaggerating for the OP's benefit, evidence of my face-palms can be seen in [1](http://stackoverflow.com/q/17377641/418556) & [2](http://stackoverflow.com/q/17816277/418556). – Andrew Thompson Jul 29 '13 at 01:54