Link to home
Start Free TrialLog in
Avatar of Drop_of_Rain
Drop_of_Rain

asked on

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;
   }
}
Avatar of Mick Barry
Mick Barry
Flag of Australia image

where should the timing details come from?
Avatar of Drop_of_Rain
Drop_of_Rain

ASKER

I just posted that in another question.

one list is being made from all the buttons ActionCommands
SOLUTION
Avatar of mmuruganandam
mmuruganandam
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> I just posted that in another question.
Link?
> 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.
> 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

https://www.experts-exchange.com/questions/20967249/Question-about-a-class.html

Hope that helps . . .
Javatm
< 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?

https://www.experts-exchange.com/questions/20969942/one-list-is-being-made-from-all-the-buttons-ActionCommands.html

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.

> What do you mean by details.

just meant the audio anf timing file names.
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; }

}

so when a button is pressed how do u determine what timing file should be added?
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);
}

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.
not clear... can explain bit better
<one way would be>  can't do it that way read my previous comment

While creating the button....
so there is no longer a requitement for them to be in pairs?
if so, then just remove timing from AudioDetails class altogether.
after the audio is selected, there would be an selection on timing... isn't it.
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.
after the audio is selected, there would be an selection on timing... isn't it.

Read my last post
unless otherwise, there is a relation between the audio file button and the time file button, you can't achieve this
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.
> code then sends a timing cycle audiofile to the list;

but the q is how do you determine what timing file to use?
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.
<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:  https://www.experts-exchange.com/questions/20967054/Help-with-a-If-statement.html
will that option solve your problem...

post your code... i will tell you the modifications..
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.

 https://www.experts-exchange.com/questions/20967054/Help-with-a-If-statement.html
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).
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;
   }
}
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
typo

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

int combinationCountOne = 0;
int combinationCountTwo = 0;
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
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.
> 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.
> 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?

<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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
as i have posted above...
<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.
<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.
> 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.
<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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
     }
}      
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
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.
<< 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
Whatever class is handling the timing selection should have a method that returns the currently selected timing.