Solved

adding an audiofile to the arraylist

Posted on 2004-04-14
15
223 Views
Last Modified: 2010-03-31
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);
     }  
}
0
Comment
Question by:Drop_of_Rain
  • 9
  • 5
15 Comments
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10830333
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10830360
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10830367
This way, you don't need to change your actionPerformed(ActionEvent e) method.  You can keep the method as it is.
0
 

Author Comment

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

Expert Comment

by:mmuruganandam
ID: 10830576
When you create the button itself, you will set the actioncommand right.  There itself, you can set your audio files too...
0
 

Author Comment

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

Expert Comment

by:mmuruganandam
ID: 10830717
in the actionPerformed,

set the different audio file to the button based on your condition
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 37

Expert Comment

by:zzynx
ID: 10830767
I still don't understand your Q.
Can you give an example of what you want to happen?
0
 

Author Comment

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

Expert Comment

by:mmuruganandam
ID: 10830904
do this way


int index = arrayList.indexOf(button) ;

arrayList.remove(index); // your button
arrayList.remove(index);  // timing.wav
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10830916
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
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 350 total points
ID: 10830926
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
 

Author Comment

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

Expert Comment

by:mmuruganandam
ID: 10993122
Not a problem... Its my pleasure..
0
 

Author Comment

by:Drop_of_Rain
ID: 10993198
Thanks for the help and support,
Christopher
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

19 Experts available now in Live!

Get 1:1 Help Now