help with action listener please

i am having problems when i execute my application, the program runs and displays exactly what i want it to but when i press the buttons they are not doing what i want. the genre is not being changed and the tracklist updated. And when i select a track from the list and press the play button i am not getting any music to play. I am desperate for someone to sort this out for me as i have now been at it for 26 hours and i have to get it finished

here is my code.

import java.applet.Applet;

import java.applet.AudioClip;

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Canvas;

import java.awt.Checkbox;

import java.awt.CheckboxGroup;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.Frame;

import java.awt.Graphics;

import java.awt.List;

import java.awt.Panel;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.net.URL;

publicclass Helpextends Frame{

private Panel controlPanel;

private Panel drawPanel;

private Panel topPanel;

private Panel rPanel;

private CheckboxGroup genre;

private Checkboxrock;

private Checkboxpop;

private Buttonplay;

private Buttonstop;

private Buttonchangegenre;

private List rockChoice, popChoice;

private DrawingCanvas DrawingCanvas;

private Stringpoplist[];

private Stringrocklist[];

private URLurl1;

private URLurl2;

private URLurl3;

private URLurl4;

private URLurl5;

private URLurl6;

private AudioClip[]rockMusic;

private URLurl7;

private URLurl8;

private URLurl9;

private URLurl10;

private URLurl11;

private AudioClip[]popMusic;

private AudioClipcurrent;

public Help()//costructor

{

rockMusic =new AudioClip[6];

try

{

rockMusic[0] = Applet.newAudioClip(new java.net.URL("file:song7.wav"));

rockMusic[1] = Applet.newAudioClip(new java.net.URL("file:song7.wav"));

rockMusic[2] = Applet.newAudioClip(new java.net.URL("file:song7.wav"));

rockMusic[3] = Applet.newAudioClip(new java.net.URL("file:song7.wav"));

rockMusic[4] = Applet.newAudioClip(new java.net.URL("file:song7.wav"));

rockMusic[5] = Applet.newAudioClip(new java.net.URL("file:song7.wav"));

}

catch (Exception exception){}

popMusic =new AudioClip[5];

try

{

popMusic[0] = Applet.newAudioClip(new java.net.URL("file:song7.wav"));

popMusic[1] = Applet.newAudioClip(new java.net.URL("file:song7.wav"));

popMusic[2] = Applet.newAudioClip(new java.net.URL("file:song7.wav"));

popMusic[3] = Applet.newAudioClip(new java.net.URL("file:song7.wav"));

popMusic[4] = Applet.newAudioClip(new java.net.URL("file:song7.wav"));

}

catch (Exception ex){}

setLayout(new BorderLayout());

drawPanel =new Panel();

drawPanel.setLayout(new BorderLayout());

drawPanel.setBackground(Color.black);

DrawingCanvas drawingCanvas =new DrawingCanvas();

drawPanel.add(drawingCanvas,"Center");

add(drawPanel,"North");

stop =new Button("STOP");

stop.setBackground(Color.blue);

play =new Button("PLAY");

play.setBackground(Color.blue);

changegenre =new Button("CHANGE GENRE");

changegenre.setBackground(Color.blue);

controlPanel =new Panel();

controlPanel.setBackground(Color.red);

controlPanel.setLayout(new FlowLayout());

controlPanel.add(play);

play.addActionListener(new ButtonListener());

controlPanel.add(stop);

stop.addActionListener(new ButtonListener());

controlPanel.add(changegenre);

changegenre.addActionListener(new ButtonListener());

add(controlPanel,"South");

genre =new CheckboxGroup();

rock =new Checkbox("ROCK", genre,true);

pop =new Checkbox("POP", genre,false);

topPanel =new Panel();

topPanel.setLayout(new FlowLayout());

topPanel.setBackground(Color.yellow);

topPanel.add(rock);

topPanel.add(pop);

rocklist =new String[]{

"Select Rock Song","Turn Up The Sun","Mucky Fingers","LYLA","Love Like A Bomb","A Bell Will Ring","Let There BE Love"

};

rockChoice =new List();

for (int i = 0; i < rocklist.length; i++)

rockChoice.add(rocklist[i]);

poplist =new String[]{"Select Pop Song","The Importance Of Being Idle","The Meaning Of Soul","Guess God Thinks I Am Able","Part Of TheQueue","Keep The Dream Alive"};

popChoice =new List();

for (int i = 0; i < poplist.length; i++)

popChoice.add(poplist[i]);

topPanel.add(rockChoice);

rockChoice.setVisible(true);

rockChoice.addActionListener(new ListListener());

topPanel.add(popChoice);

popChoice.setVisible(false);

popChoice.addActionListener(new ListListener());

add(topPanel,"Center");

addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent e){

dispose();

System.exit(0);

}

});

}

publicstaticvoid main(String[] args){

Help mainFrame =new Help();

mainFrame.setSize(800, 600);

mainFrame.setTitle("Glenn's Java Jukebox");

mainFrame.setVisible(true);

}

privateclass DrawingCanvasextends Canvas{

private DrawingCanvas(){

setSize(100, 400);

}

publicvoid paint(Graphics g){

// do all the drawing of the jukebox in the

g.drawString("please work", 50, 50);

g.setColor(Color.yellow);

g.fillRect(700, 105, 30, 450);

g.fillRect(80, 105, 30, 450);

g.setColor(Color.yellow);

g.fillArc(80, 10, 650, 200, -180, -180);

g.setColor(Color.white);

g.fillOval(150, 60, 20, 20);

g.fillOval(210, 60, 20, 20);

g.fillOval(270, 60, 20, 20);

g.fillOval(330, 60, 20, 20);

g.fillOval(390, 60, 20, 20);

g.fillOval(630, 60, 20, 20);

g.fillOval(450, 60, 20, 20);

g.fillOval(510, 60, 20, 20);

g.fillOval(570, 60, 20, 20);

g.setColor(Color.black);

g.fillOval(180, 60, 20, 20);

g.fillOval(240, 60, 20, 20);

g.fillOval(300, 60, 20, 20);

g.fillOval(360, 60, 20, 20);

g.fillOval(420, 60, 20, 20);

g.fillOval(480, 60, 20, 20);

g.fillOval(540, 60, 20, 20);

g.fillOval(200, 80, 15, 15);

g.fillOval(380, 80, 15, 15);

g.fillOval(260, 80, 15, 15);

g.fillOval(320, 80, 15, 15);

g.fillOval(380, 80, 15, 15);

g.fillOval(440, 80, 15, 15);

g.fillOval(500, 80, 15, 15);

g.fillOval(560, 80, 15, 15);

g.fillOval(620, 80, 15, 15);

g.fillOval(680, 80, 15, 15);

g.fillOval(140, 80, 15, 15);

g.fillOval(600, 60, 20, 20);

g.fillOval(660, 60, 20, 20);

g.setColor(Color.red);

g.fillOval(110, 80, 15, 15);

g.fillOval(170, 80, 15, 15);

g.fillOval(230, 80, 15, 15);

g.fillOval(290, 80, 15, 15);

g.fillOval(350, 80, 15, 15);

g.fillOval(410, 80, 15, 15);

g.fillOval(470, 80, 15, 15);

g.fillOval(530, 80, 15, 15);

g.fillOval(590, 80, 15, 15);

g.fillOval(650, 80, 15, 15);

g.setColor(Color.white);

g.fillRect(110, 100, 600, 500);

g.setColor(Color.magenta);

g.setFont(new Font("TimesRoman", Font.ITALIC, 48));

g.drawString(" Swurlitzer Jukebox", 230, 160);

g.setColor(Color.black);

g.setFont(new Font("Helvetica", Font.PLAIN, 18));

g.drawString(" 1.50 For 4 Plays", 140, 380);

g.drawString("Please Select Your Genre", 400, 380);

g.setFont(new Font("Helvetica", Font.ITALIC, 18));

g.drawString("Glenn Proudly Presents", 290, 40);

g.setColor(Color.yellow);

g.fillRect(690,100,20,450);

}

}//end DrawingCanvas

private class ListListener implements ActionListener {

publicvoid actionPerformed(ActionEvent e){

if (current !=null)

current.stop();

Checkbox chk = genre.getSelectedCheckbox();

if ("rock".equals(chk.getLabel()))

current = rockMusic[rockChoice.getSelectedIndex()];

elseif ("pop".equals(chk.getLabel()))

current = popMusic[popChoice.getSelectedIndex()];//213x1

}

}

privateclass ButtonListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent event){

if (current !=null)

current.stop();

if (event.getSource() == play)

if (current !=null)

current.play();

if (event.getSource() == changegenre)

changeGenre();

}

}//end ButtonListener

privatevoid changeGenre()//line 229

{

Checkbox c = genre.getSelectedCheckbox();

if (c.getLabel() =="rock")//line 232

{

popChoice.setVisible(false);

rockChoice.setVisible(true);//line235

}//236

elseif (c.getLabel() =="pop"){

//etc for other genres..

repaint();

}//end changeGenre() //line 241

}

}

[16427 byte] By [fowlergod09a] at [2007-11-26 13:49:42]
# 1

First of all, stop silently catching exceptions.At the very least, put an "exception.printStackTrace();" in the catch blocks. You're missing valuable diagnostic information.

Secondly, intent your code consistently and sanely.

Third, get rid of unused fields and variables, and name the ones that you do use meaningfully. You shouldn't have eleven fields named "url1", "url2"...

Fourth, rather than having one "ListListener" and "ButtonListener" and making lots of copies of them, and having them go back (I guess -- you didn't quote the whole thing) and somehow figure out (assuming it's working) what the state of the widgets are, etc., create individual handlers for each widget and event, probably using anonymous inner classes. It makes your code much cleaner.

paulcwa at 2007-7-8 1:26:15 > top of Java-index,Java Essentials,New To Java...