adding an audiofile to the arraylist

Could this code be modified to add another audiofile after each addition to dmArrayList.  Like all even numbers of the arraylist +1 Example [0] =1, [1] =2, [3]=3 etc... But when a element is removed from the dmArrayList  dmArrayList.remove(button); the added audiofile will be removed as well.


public void actionPerformed(ActionEvent e)
            {
                               
          JColoredButton button = (JColoredButton)e.getSource();
         
          if (dmArrayList.contains(button))
          {
               dmArrayList.remove(button);
               button.setDefaultColor();
          }
          else
          {
               dmArrayList.add(button);
               button.setPressedColor();
          }
         
          System.out.println(dmArrayList);
     }  
}
Drop_of_RainAsked:
Who is Participating?
 
mmuruganandamConnect With a Mentor Commented:
public void actionPerformed(ActionEvent e)
{
                               
          JColoredButton button = (JColoredButton)e.getSource();
         
          int index = arrayList.indexOf(button) ;

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

               button.setDefaultColor();
          }
          else
          {
               dmArrayList.add(button);
               dmArrayList.add("timing.wav");
 
               button.setPressedColor();
          }
         
          System.out.println(dmArrayList);
}  
0
 
mmuruganandamCommented:
You have JColoredButton Right.

Add a method to hold the audio file.

That way, you can get the information about the audio file for the given button.

+ It will be removed along with the button when it is removed from the arraylist.

So there is no need to have logic on even and odd index in arraylist



Regards,
Muruga
0
 
mmuruganandamCommented:
I mean,


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

     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);
     }

     public void setAudioFileName(String name)
     {
           audioFileName = name;
     }

     public String getAudioFileName()
     {
           return audioFileName;
     }
}
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mmuruganandamCommented:
This way, you don't need to change your actionPerformed(ActionEvent e) method.  You can keep the method as it is.
0
 
Drop_of_RainAuthor Commented:
How is this adding an audiofile between the files that go into the ayyaylist from the actionCommand.

Could this code be modified to add another audiofile after each addition to dmArrayList.  Like all even numbers of the arraylist +1 Example [0] =1, [1] =2, [3]=3 etc... But when a element is removed from the dmArrayList  dmArrayList.remove(button); the added audiofile will be removed as well.

How is this adding an audiofile between the files that go into the ayyaylist from the actionCommand.


public void setAudioFileName(String name)
     {
           audioFileName = name;
     }

     public String getAudioFileName()
     {
           return audioFileName;
0
 
mmuruganandamCommented:
When you create the button itself, you will set the actioncommand right.  There itself, you can set your audio files too...
0
 
Drop_of_RainAuthor Commented:
They are already set that way. I am saying that I need this to happen in the background by code. There will be no button pressed to set the audiofile. I need to have it this way, that every time a button is pressed and a audio file is sent to the arraylist that another file that is the timer between the files will then go into the arraylist. This file at times won't be the same file, but I will let an if statement take care of that.
0
 
mmuruganandamCommented:
in the actionPerformed,

set the different audio file to the button based on your condition
0
 
zzynxSoftware engineerCommented:
I still don't understand your Q.
Can you give an example of what you want to happen?
0
 
Drop_of_RainAuthor Commented:
Could this code be modified to add another audiofile after each addition to dmArrayList.  Like all even numbers of the arraylist +1 Example [0] =1, [1] =2, [3]=3 etc... But when a element is removed from the dmArrayList  dmArrayList.remove(button); the added audiofile will be removed as well.


Example #1 buttom pressed = #1.wav, #2 buttom pressed = #2.wav, #3 buttom pressed = #3.wav,
#4 buttom pressed = #4.wav, #5 buttom pressed = #5.wav That is what is happening right now.

What I need is:

Example #1 buttom pressed = #1.wav, Timing audiofile added time.wav, #2 buttom pressed = #2.wav, Timing audiofile added time.wav, #3 buttom pressed = #3.wav, Timing audiofile added time.wav,  #4 buttom pressed = #4.wav, Timing audiofile added time.wav, #5 buttom pressed = #1.wav, Timing audiofile added time.wav


Example: #1 buttom pressed = #1.wav, Timing audiofile added time.wav, #2 buttom pressed = #2.wav, Timing audiofile added time.wav, #3 buttom pressed = #3.wav, Timing audiofile added time.wav,  #4 buttom pressed = #4.wav, Timing audiofile added time.wav, #5 buttom pressed = #1.wav, Timing audiofile added time.wav

NOW the user presses #3 again removing it from the arraylist and here is my problem:

Example: #1 buttom pressed = #1.wav, Timing audiofile added time.wav, #2 buttom pressed = #2.wav, Timing audiofile added time.wav, Timing audiofile added time.wav,  #4 buttom pressed = #4.wav, Timing audiofile added time.wav, #5 buttom pressed = #1.wav, Timing audiofile added time.wav

there are 2 timing files next to each other in the arraylist:

2 buttom pressed = #2.wav, Timing audiofile added time.wav, Timing audiofile added time.wav,  #4 buttom pressed = #4.wav, Timing audiofile added time.wav
0
 
mmuruganandamCommented:
do this way


int index = arrayList.indexOf(button) ;

arrayList.remove(index); // your button
arrayList.remove(index);  // timing.wav
0
 
mmuruganandamCommented:
but careful that

For adding the time.wav,

int index = arrayList.indexOf(button) ;

if (index > -1)
{
    arrayList.add(index+1, "timing.wav");
}



For removing...

int index = arrayList.indexOf(button) ;

if (index > -1)
{
    arrayList.remove(index); // your button
    arrayList.remove(index);  // timing.wav
}


Regards,
Muruga
0
 
Drop_of_RainAuthor Commented:
That was a great answer, thanks for the support.
Christopher

P.S. Sorry about taking so long to accept this answer, I didn't bookmark it.
0
 
mmuruganandamCommented:
Not a problem... Its my pleasure..
0
 
Drop_of_RainAuthor Commented:
Thanks for the help and support,
Christopher
0
All Courses

From novice to tech pro — start learning today.