Solved

MyListener will not complie

Posted on 2004-04-27
49
359 Views
Last Modified: 2010-03-31
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;
   }
}
0
Comment
Question by:Drop_of_Rain
  • 19
  • 15
  • 13
  • +2
49 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10935584
where should the timing details come from?
0
 

Author Comment

by:Drop_of_Rain
ID: 10935639
I just posted that in another question.

one list is being made from all the buttons ActionCommands
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 250 total points
ID: 10936009
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10936186
>> I just posted that in another question.
Link?
0
 
LVL 92

Expert Comment

by:objects
ID: 10936250
> 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
 
LVL 15

Expert Comment

by:Javatm
ID: 10936367
> 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
 

Author Comment

by:Drop_of_Rain
ID: 10936417
< 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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10936437
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
 
LVL 92

Expert Comment

by:objects
ID: 10936482
> What do you mean by details.

just meant the audio anf timing file names.
0
 

Author Comment

by:Drop_of_Rain
ID: 10936578
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
 
LVL 92

Expert Comment

by:objects
ID: 10936601
so when a button is pressed how do u determine what timing file should be added?
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10936626
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
 

Author Comment

by:Drop_of_Rain
ID: 10936669
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10936687
not clear... can explain bit better
0
 

Author Comment

by:Drop_of_Rain
ID: 10936693
<one way would be>  can't do it that way read my previous comment

While creating the button....
0
 
LVL 92

Expert Comment

by:objects
ID: 10936697
so there is no longer a requitement for them to be in pairs?
if so, then just remove timing from AudioDetails class altogether.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10936711
after the audio is selected, there would be an selection on timing... isn't it.
0
 

Author Comment

by:Drop_of_Rain
ID: 10936762
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
 

Author Comment

by:Drop_of_Rain
ID: 10936770
after the audio is selected, there would be an selection on timing... isn't it.

Read my last post
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10936789
unless otherwise, there is a relation between the audio file button and the time file button, you can't achieve this
0
 

Author Comment

by:Drop_of_Rain
ID: 10936831
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
 
LVL 92

Expert Comment

by:objects
ID: 10936848
> code then sends a timing cycle audiofile to the list;

but the q is how do you determine what timing file to use?
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10936851
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
 

Author Comment

by:Drop_of_Rain
ID: 10936877
<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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10936890
will that option solve your problem...

0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10936894
post your code... i will tell you the modifications..
0
 

Author Comment

by:Drop_of_Rain
ID: 10936902
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10936921
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
 

Author Comment

by:Drop_of_Rain
ID: 10936925
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
 

Author Comment

by:Drop_of_Rain
ID: 10936937
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
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 250 total points
ID: 10937000
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10937006
typo

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

int combinationCountOne = 0;
int combinationCountTwo = 0;
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10937015
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
 
LVL 92

Expert Comment

by:objects
ID: 10944587
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
 
LVL 92

Expert Comment

by:objects
ID: 10944622
> 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
 
LVL 92

Expert Comment

by:objects
ID: 10944634
> 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
 

Author Comment

by:Drop_of_Rain
ID: 10945951
<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
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 10945985
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10946045
as i have posted above...
0
 

Author Comment

by:Drop_of_Rain
ID: 10946144
<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
 

Author Comment

by:Drop_of_Rain
ID: 10946164
<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
 
LVL 92

Expert Comment

by:objects
ID: 10946182
> 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
 

Author Comment

by:Drop_of_Rain
ID: 10946246
<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
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 10946275
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
 

Author Comment

by:Drop_of_Rain
ID: 10946336
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
 

Author Comment

by:Drop_of_Rain
ID: 10946524
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10946990
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
 

Author Comment

by:Drop_of_Rain
ID: 10970355
<< 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
 
LVL 92

Expert Comment

by:objects
ID: 10974161
Whatever class is handling the timing selection should have a method that returns the currently selected timing.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now