MyListener will not complie

I am using a IDE it will not complie MyListener because it doesn't recongnize timing. What should I do about this. That is one of the reasons I asked about joining the 2 classes together. One other thing what classes do I need to import with AudioDetails.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;

public class MyListener implements ActionListener {

    private List theList = new ArrayList();

    public MyListener() {
    }

    public void addButton(JButton theButtonToAdd) {
       theButtonToAdd.addActionListener(this);  // Listen for the button to be pressed
    }

    public void actionPerformed(ActionEvent e) {
       JButton button = (JButton)e.getSource();
       String command = button.getActionCommand();
       AudioDetails details = new AudioDetails(command, timing);
       if (theList.contains(details))
       {
          theList.remove(details);
       }
       else
       {
          theList.add(details);
       }
    }




    public List getList() { return theList; }

}


public class AudioDetails
{
   private String AudioFile;
   private String Timing;
   // could add other fields as needed

   public AudioDetails(String file, String timing)
   {
      AudioFile = file;
      Timing = timing;
   }

   public String getAudioFile()
   {
      return AudioFile;
   }

   public String getTiming()
   {
      return Timing;
   }
}
Drop_of_RainAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
objectsConnect With a Mentor Commented:
then you don't really need the AudioDetails class cause all u need to store is the audio file name string.
Then use a Set and the contains() method to determine what patterns of audio has been selected and fill in timing accordingly.
eg.
    if (selected.contains("dm6") && selected.contains("dm8") && selected.contains("dm21") && selected.contains("dm22") && selected.contains("dm26"))
   {
      timing = "timing3";
   }

etc.
0
 
objectsCommented:
where should the timing details come from?
0
 
Drop_of_RainAuthor Commented:
I just posted that in another question.

one list is being made from all the buttons ActionCommands
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
mmuruganandamConnect With a Mentor Commented:
one option would be passing the "timing* when the listener is created.... if the timing to that button is not a dynamic one.

public class MyListener implements ActionListener {

    private List theList = new ArrayList();
    private String timing ;

    public MyListener(String aTiming) {
         timing = aTiming;
    }

    ------
    ------
    ------


Regards,
Muruga
0
 
zzynxSoftware engineerCommented:
>> I just posted that in another question.
Link?
0
 
objectsCommented:
> one list is being made from all the buttons ActionCommands

If thats the case then you aren't going to get your odd/even effect in your list aren't you.
The purpose of the AudioDetails was to meet the requirement to store the details of both the audio and the timing together, is this not the case anymore.
0
 
JavatmCommented:
> That is one of the reasons I asked about joining the 2 classes together.

If you had just tried the code that I gave you when you told me to combine them
your not gonna have problems on errors. The thing about it is I tried combining
it for you to get the whole picture.

Can you try the codes and if it compiles or works then try to analyze the codes :

Comment from Javatm
Date: 04/26/2004 04:21PM PHT

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20967249.html

Hope that helps . . .
Javatm
0
 
Drop_of_RainAuthor Commented:
< store the details of both the audio and the timing together,> What do you mean by details.

That came about because I was tring to blend 2 arraylist into 1. It also came from having to remove an audiofile by the button being pressed again along with the timing.wav.


I just posted that in another question. Link?

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20969942.html

0
 
mmuruganandamCommented:
if the timing is a static one for each button...

why don't you use the one i have suggested above..

>> one option would be passing the "timing* when the listener is created.... if the timing to that button is not a dynamic one.

0
 
objectsCommented:
> What do you mean by details.

just meant the audio anf timing file names.
0
 
Drop_of_RainAuthor Commented:
I just raised the points to 500.

<If thats the case then you aren't going to get your odd/even effect in your list aren't you.>
No not with the code that exist at this time.


This was a question I asked before about making a single list from the 2.
 addToList(bList, ???); this code still had to be created to get the timing file into (blist) I was working on this to get the single list, and out of this came the new myListener and AudioDetails.



How would this code be modified to make it capable of creating a single arrayalist out of ( alist and blist ) where they switch back and forth from the 2 lists  Example   alist hold audiofiles name kp1.wav - kp10.wav  and the other holds a timimg file named the new arraylit will hold [0] kp1.wav [1] 10sec.wav [3] kp2.wav [4] 10sec.wav etc.......

public class MyListener implements ActionListener {

    private List aList = new ArrayList();
    private List bList = new ArrayList();

    public MyListener() {
    }

    public void addButton(JButton theButtonToAdd) {
       theButtonToAdd.addActionListener(this);  // Listen for the button to be pressed
    }

    public void actionPerformed(ActionEvent e) {
       JButton button = (JButton)e.getSource();
       String command = button.getActionCommand();
       addToList(aList, command);
       addToList(bList, ???);
    }

    private static List list, String file) {
       if (list.contains(file))
       {
          list.remove(file);
       }
       else
       {
          list.add(file);
       }
    }

    public List getAList() { return aList; }
    public List getBList() { return bList; }

}

0
 
objectsCommented:
so when a button is pressed how do u determine what timing file should be added?
0
 
mmuruganandamCommented:
one way would be

While creating the button....

button.setActionCommand("command|timing1.wav");


in the actionPerformed..

String command = button.getActionCommand();
int index = command.indexOf("|");

String timing = null;

if (index > -1)
{
     timing = command.substring(index+1);
     command = command.substring(0, index);
}

0
 
Drop_of_RainAuthor Commented:
There is a timing button panel. The user will choose a timing cycle from the options. The buttons actioncommand is set to an audiofile of time. the audiofile is blank except at the very end there is a statement (up). That file needs to be placed automaticly after each new button is pressed unless it fits a condition and is replaced with a longer timing cycle file.
0
 
mmuruganandamCommented:
not clear... can explain bit better
0
 
Drop_of_RainAuthor Commented:
<one way would be>  can't do it that way read my previous comment

While creating the button....
0
 
objectsCommented:
so there is no longer a requitement for them to be in pairs?
if so, then just remove timing from AudioDetails class altogether.
0
 
mmuruganandamCommented:
after the audio is selected, there would be an selection on timing... isn't it.
0
 
Drop_of_RainAuthor Commented:
No they have to be in pairs. Let me explain the purpose of what this is for.

Each button is a section of a workout. Each section may only last for 3 seconds and are directed to return to the starting postion. Then another section starts and may last for 3 seconds and are directed to return to the starting postion.

Example of the above in code terms: one button is pressed an actioncommand which is an audiofile is sent to the list, code then sends a timing cycle audiofile to the list; another button is pressed an actioncommand which is an audiofile is sent to the list, code then sends a timing cycle audiofile to the list etc.....  I though this would be a good time to run each actioncommand through some conditionals and see if another timing cycle file needs to replace the on that is being sent there automaticly, this way all of the workout sections and timing are coordinated in the right structure. So that if one of the buttons was pressed again because the user want to remove that section the timing cycle file would be removed at the same time.
0
 
Drop_of_RainAuthor Commented:
after the audio is selected, there would be an selection on timing... isn't it.

Read my last post
0
 
mmuruganandamCommented:
unless otherwise, there is a relation between the audio file button and the time file button, you can't achieve this
0
 
Drop_of_RainAuthor Commented:
You mean that through If statements set up in MyListener which are screening each actioncommand and if it detects an action command going into the list, code cannot send a file to the list in the next location in the list. I find that hard to believe.
0
 
objectsCommented:
> code then sends a timing cycle audiofile to the list;

but the q is how do you determine what timing file to use?
0
 
mmuruganandamCommented:
okie...

you audio file action and the time file action are two independent actions.

on clicking of the timing file action, how do you know which audio file needs to have this timing.


Other option could be, you can see the selected audio timing button, based on that you can insert the timing for all the audio files that you click.
0
 
Drop_of_RainAuthor Commented:
<how do you know which audio file needs to have this timing.>

the timing cycle is an audiofile

Other option could be, you can see the selected audio timing button, based on that you can insert the timing for all the audio files that you click.  That has been what I have been saying.

"Example of the above in code terms: one button is pressed an actioncommand which is an audiofile is sent to the list, code then sends a timing cycle audiofile to the list;"

I ahve asked these questions in another question long ago.

here is the link:  http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20967054.html
0
 
mmuruganandamCommented:
will that option solve your problem...

0
 
mmuruganandamCommented:
post your code... i will tell you the modifications..
0
 
Drop_of_RainAuthor Commented:
did you go the the question I put the link up for? If not go to it more is explained there of what is needed.

 http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20967054.html
0
 
mmuruganandamCommented:
There you have asked for comparison and some fixed timing interval on some condition basis.

For that, we have suggested you that you can do that at last (say when record/play).
0
 
Drop_of_RainAuthor Commented:
This is called from each class of the all button groups

some rules for the If statements needed
I can change the details in the statements later

 If  (dm6, dm8, dm21, dm22, dm26)  are being used, then 2 seconds more then the timing cycle being used needs to be used instead.

If (dm9, dm11, dm28, dm31, dm35)  are being used, then 3 seconds more then the timing cycle being used needs to be used instead.

Otherwise the regular timing cycle


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;


public class MyListener implements ActionListener {

    private List theList = new ArrayList();

    public MyListener() {
    }

    public void addButton(JButton theButtonToAdd) {
       theButtonToAdd.addActionListener(this);  // Listen for the button to be pressed
    }

    public void actionPerformed(ActionEvent e) {
       JButton button = (JButton)e.getSource();
       String command = button.getActionCommand();
       AudioDetails details = new AudioDetails(command, timing);
       if (theList.contains(details))
       {
          theList.remove(details);
       }
       else
       {
          theList.add(details);
       }
    }




    public List getList() { return theList; }

}


public class AudioDetails
{
   private String AudioFile;
   private String Timing;
   // could add other fields as needed

   public AudioDetails(String file, String timing)
   {
      AudioFile = file;
      Timing = timing;
   }

   public String getAudioFile()
   {
      return AudioFile;
   }

   public String getTiming()
   {
      return Timing;
   }
}
0
 
Drop_of_RainAuthor Commented:
For that, we have suggested you that you can do that at last (say when record/play).

OK but I will still need the code for it.
0
 
mmuruganandamConnect With a Mentor Commented:
List list = myListener.getList() ;

Iterator iter = list.iterator();

int combinationOneCount = 0;
int combinationTwoCount = 1;

String audioFile ;

while (iter.hasMoreElements())
{
    AudioDetails details = (AudioDetails)iter.next();

    audioFile = details.getAudioFile();
    if (audioFile.equals("dm6") || audioFile.equals("dm8") || audioFile.equals("dm21") || audioFile.equals("dm22") || audioFile.equals("dm26"))
   {
      combinationCountOne++;
   }
else if (audioFile.equals("dm9") || audioFile.equals("dm11") || audioFile.equals("dm28") || audioFile.equals("dm31") || audioFile.equals("dm35"))
  {
    combinationCountTwo++;
  }
}

String timing = null;

if (combinationCountOne >= 5)
{
  timing = "timing3.wav";
}
else if (combinationCountTwo >= 5)
{
  timing = "timing5.wav";
}

if (timing != null)
{
  // use the timing...
  // by adding the alternate index of list...

   iter = list.iterator();
   
   List newList = new ArrayList();
 
   while (iter.hasNext())
   {
         newList.add(iter.next());
         newList.add(timing);
   }
}

this is just like an abstract what you have to do on record and/or final assumble
0
 
mmuruganandamCommented:
typo

>>int combinationOneCount = 0;
>>int combinationTwoCount = 1;

int combinationCountOne = 0;
int combinationCountTwo = 0;
0
 
mmuruganandamCommented:
if (combinationCountOne >= 5)
{
  timing = "timing3.wav";
}
else if (combinationCountTwo >= 5)
{
  timing = "timing5.wav";
}
else
{
  timing = "timing2.wav"; // default timing interval
}


  iter = list.iterator();
   
   List newList = new ArrayList();
 
   while (iter.hasNext())
   {
         newList.add(iter.next());
         newList.add(timing);
   }


This way, you don't need to add the timing anywhere in the list before final assumble
0
 
objectsCommented:
I still don't understand how when a button is clicked you determine what timing file needs to be added to the list, the audio file is dtored with the button but the timing file is not.
0
 
objectsCommented:
> This way, you don't need to add the timing anywhere in the list before final assumble

Already mentioned that earlier. And was told that they needed to be there.
0
 
objectsCommented:
> If  (dm6, dm8, dm21, dm22, dm26)  are being used, then 2 seconds more then the timing cycle being
> used needs to be used instead.

> If (dm9, dm11, dm28, dm31, dm35)  are being used, then 3 seconds more then the timing cycle
> being used needs to be used instead.

What if all ten of them are present?

0
 
Drop_of_RainAuthor Commented:
<What if all ten of them are present?>  The code needs to be able to take care of this. That is why I thought it would be best to handle this at the time the buttons are pressed.

<Already mentioned that earlier. And was told that they needed to be there.>

But it seems to me that both of you think this doesn't need to happen when I think it does. So in a way, I just let it go. You guys know more then I do about this stuff.

The way I see it is just  a difference in timing of when this takes place as far as I can see it. If it is easier to do it, the other way, (after the list is created) then that is how it should be done I guess.
0
 
mmuruganandamCommented:
as i have posted above...
0
 
Drop_of_RainAuthor Commented:
<cause all u need to store is the audio file name string>   I didn't know anything else was being stored. I thought that was what was always the case.

But that doesn't account for the timing file being stored automatically, That would be done in the else at the end.

How would it start to get the actioncommand from the timing button panel that was pressed but not have it go into the list.

There are some problems that this brings up. Lets say that the user doesn't select the timing cycle button until the end. The whole list will need to be gone through and add the proper timing for each button that had been selected.
0
 
Drop_of_RainAuthor Commented:
<as i have posted above...>

I was looking at your code. Read what I have just wrote about a problem I just noticed, I think your code will take care of this but I am not sure. Help me out with this.
0
 
objectsCommented:
> as i have posted above...

With a difference that the approach I suggested doesn't require you to loop thru the list looking for matches, it can all be done with one if statement.
0
 
Drop_of_RainAuthor Commented:
<With a difference that the approach I suggested doesn't require you to loop thru the list looking for matches, it can all be done with one if statement.>

Did you read my last comment, it bring up something that will not be handled by that approach.
0
 
objectsConnect With a Mentor Commented:
yes you still need a loop for inserting the timings, but you don't need a seperate loop to determine what timing to use.

Though I must say I'm still a little vague on your exact requirements regards timing. How does that timing panel work?
0
 
Drop_of_RainAuthor Commented:
Here is the code.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.*;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TimeCycles extends JPanel implements ActionListener
{
     JButton onOffButton;
     JLabel label;
     JPanel gridPanel;
     ArrayList timecyclesArrayList;
     ArrayList arrayList;
     private Map timingbuttons = new HashMap();

     public TimeCycles()

     {
          timecyclesArrayList = new ArrayList();
          label = new JLabel();
          //Call it like this . . .
          gridPanel = new JPanel();
          gridPanel.setLayout(new GridLayout());
          // Setting MY layout...
          setLayout(new BorderLayout());
          add(label, BorderLayout.NORTH);
          add(gridPanel, BorderLayout.CENTER);
          label.setText(" Time cycles ");
          label.setForeground(Color.red);
          setBackground(Color.white);
               
           
           for (int i=2; i<13; i++)
            {
               JButton b = new JColoredButton((i+1)+"seconds", Color.green, Color.black);
               b.setActionCommand((i+1)+"seconds" + ".wav");
               b.setPreferredSize( new Dimension( 40, 20 ) );
               buttons.put("b"+(i+1), b);
               b.addActionListener(this);
               gridPanel.add(b);
            }

     }

     public void actionPerformed(ActionEvent e)
     {
                               
          JColoredButton b = (JColoredButton)e.getSource();
         
          int index = timecyclesArrayList.indexOf(b) ;

          if (index > -1)
          {
               timecyclesArrayList.remove(index); // removal of button
               timecyclesArrayList.remove(index); // removal of timing.wav

                b.setDefaultColor();
          }
          else
          {
               timecyclesArrayList.add(b);
               timecyclesArrayList.add("timing.wav");
 
                b.setPressedColor();
          }
         
          }  

    }

   class JColoredButton extends JButton
   {
     private Color foreground;
     private Color deafultForeground;
     private Color background;
     private Color defaultBackground;

     public JColoredButton(String label, Color background, Color foreground)
     {
          super(label);
          this.background = background;
          this.foreground = foreground;
          defaultBackground = getBackground();
          deafultForeground = getForeground();
     }
     
     public void setPressedColor()
     {
          setBackground(background);
          setForeground(foreground);
     }
     
     public void setDefaultColor()
     {
          setBackground(defaultBackground);
          setForeground(deafultForeground);
     }
}      
0
 
Drop_of_RainAuthor Commented:
READ THIS

There are some problems that this brings up. Lets say that the user doesn't select the timing cycle button until the end. The whole list will need to be gone through and add the proper timing for each button that had been selected
0
 
mmuruganandamCommented:
The think here is the final assumble code would take care of this.

But when you timing cycle button is selected, it won't go into any of the list since there is no way in which it is mapped with the actual wave file.
0
 
Drop_of_RainAuthor Commented:
<< But when you timing cycle button is selected, it won't go into any of the list since there is no way in which it is mapped with the actual wave file.>>

I know I need to get that done. That will come from a ActionCommand as well how would that be done?

Then we can close this question
0
 
objectsCommented:
Whatever class is handling the timing selection should have a method that returns the currently selected timing.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.