Solved

Add and Removing file with button press

Posted on 2004-04-15
44
339 Views
Last Modified: 2010-03-31
Can the code below be modified. The code below keeps track of the buttons pressed and stores them in an arrayList. I will need to insert another file after the button is pressed which will end up in a even location in the arraylist if +1 is added. So ever even number will have the added file in it. These buttons are grouped in a Jpanel with 50 buttons. I want the added file to be removed as well as the file that goes with that button if the button is pressed again.


public class MyListener implements ActionListener {

    private List theList = null;
    private Map theMap = new HashMap();

    public MyListener(List listToUse) {
       this.theList = listToUse;
    }

    public void addButton(JButton theButtonToAdd) {
       theMap.put(theButtonToAdd, theButtonToAdd.getActionCommand());   // remember the button in a map;
                                                                                                            // key=the button, value the action command
       theButtonToAdd.addActionListener(this);  // Listen for the button to be pressed
    }

    public void actionPerformed(ActionEvent e) {
       if (  !(e.getSource() instanceof JButton)  )
          return;

       JButton button = (JButton)e.getSource();
       if ( !theMap.contains(button) ) return;

       theList.add( (String)theMap.get(button) );  // if the button is pressed: add its action command to the list
    }

    public List getList() { return theList; }

}

0
Comment
Question by:Drop_of_Rain
  • 19
  • 16
  • 9
44 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10838420
that code would be a lot simpler like so:

public class MyListener implements ActionListener {

    private List theList = null;

    public MyListener(List listToUse) {
       this.theList = listToUse;
    }

    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();
       theList.add( button.getActionCommand()) );  // if the button is pressed: add its action command to the list
    }

    public List getList() { return theList; }

}
0
 
LVL 92

Expert Comment

by:objects
ID: 10838431
do you need to add the other file to the same list?  I think you'll find it a lot easier to implement that if you use two lists.
0
 
LVL 92

Expert Comment

by:objects
ID: 10838440
>  want the added file to be removed as well as the file that goes with that button if the button is pressed again.

To remove the file instead of adding it again use:

public class MyListener implements ActionListener {

    private List theList = null;

    public MyListener(List listToUse) {
       this.theList = listToUse;
    }

    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();
       if (thList.contains(command))
       {
          theList.remove(command);
       }
       else
       {
          theList.add(command);
       }
    }

    public List getList() { return theList; }

}
0
 

Author Comment

by:Drop_of_Rain
ID: 10838443
I would agree can you.  Can you do the add the extra file the removal part.
0
 

Author Comment

by:Drop_of_Rain
ID: 10838464
<do you need to add the other file to the same list?>

Yes I do, unless it would be easier to do it with 3 list. The reason I need to have this in the same list at some point is because this list will go to the AutoConcat program.
0
 
LVL 92

Expert Comment

by:objects
ID: 10838467
do u just want each button click to add/remove two files instead of one?
if so, where would the details of the second file come from?
does the extra file need to be in the same list?
0
 
LVL 92

Expert Comment

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

Author Comment

by:Drop_of_Rain
ID: 10838528
< do u just want each button click to add/remove two files instead of one? >

No I need to set this up so when one of the buttons are pressed another file is added right next to that file. And when the user wants to remove any file from the arraylist by pressing the button again, that the extra file that was added because that button was pressed is removed as well. Other wise there will be two of the same files (the added ones) next to each other.
0
 
LVL 92

Expert Comment

by:objects
ID: 10838545
the code above should achieve that if i understand u correctly.
using two list makes it a lot simpler
0
 
LVL 92

Expert Comment

by:objects
ID: 10839033
>  private static List list, String file) {

typo, that should be:

 private static void addToList(List list, String file) {
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10839439

This question is duplicate of

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20954740.html which is not yet closed.

Why don't you have look at that answers.
0
 
LVL 92

Expert Comment

by:objects
ID: 10839481
mmuruganandam,

i don't see any need for storeing everything in one list.
And if you did it would be better to store a single wrapper object in the list containing all the required data instead of adding two elements for every click.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10839484
Solution for requirement!
0
 

Author Comment

by:Drop_of_Rain
ID: 10839498
mmuruganandam:  That didn't work because as soon as I added it to a wirking file that didn;t have the remove paet I got error messages about actionPreformed. I an others trirc to fix it be with no preveil, so I let it go and started over again but from a better start this time.
0
 

Author Comment

by:Drop_of_Rain
ID: 10839509
That's not right it was when that and other things were added together that that happened sorry,
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10839516
there are lot of post on that question.  either you could have continued from there... or closed that question.  if you didn't post the problem, how we would know.

0
 

Author Comment

by:Drop_of_Rain
ID: 10839518
i don't see any need for storeing everything in one list.
And if you did it would be better to store a single wrapper object in the list containing all the required data instead of adding two elements for every click.

The only reason for doing this was to pass then to AutoConcat and the files could come from that one arraylist
0
 

Author Comment

by:Drop_of_Rain
ID: 10839535
I tried to work out the problems in this question: Fixing the problem or another way
0
 
LVL 92

Expert Comment

by:objects
ID: 10839569
> The only reason for doing this was to pass then to AutoConcat and the files could come from that one arraylist


How is it passed to AudioConcat
0
 

Author Comment

by:Drop_of_Rain
ID: 10839614
Command line args, this is a class that passed args to AudioConcat. What needs to be created I think is a class that can do that  and read the files from an arraylist. That is why I have been sticking to the one arraylist. This worked I got the single file and it was perfect.

public class AudioTest
{
     public static void main(String[] args)
     {
          AudioConcat.main(new String[] {
                    "-c",  // concatenation mode
                    "-o",  // output file specified
                    "c:/test/test.wav",   // output file
                    "c:/test/wav/kp1.wav", // input file 1
                    "c:/test/wav/kp2.wav", // input file 2
                    "c:/test/wav/kp3.wav", // input file 3
                    "c:/test/wav/kp4.wav" // input file 4
                    });
     }
0
 
LVL 92

Expert Comment

by:objects
ID: 10839645
I see, what you realloy should be doing is adding a new method to AudioConcat that accepts the required input directly.
main() is designed for handling command line arguments, and although it can be called directly it's a lot easier to call a specifically designed method directly.
0
 

Author Comment

by:Drop_of_Rain
ID: 10839696
go this this question and see what I just posted

blending 2 arraylist togrther into 1
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Drop_of_Rain
ID: 10839723
I forgot that mmuruganandam added this to SequenceAudioInputStream to add the stream to it, if we can get
SequenceAudioInputStream into AutoConcat ithe hard part will be completed


public void addAllAudioInputStream(ListOfFiles iterator) throws UnsupportedAudioFileException, IOException
      {
            while (iterator.hasMoreElements())
            {
                  addAudioInputStream(AudioSystem.getAudioInputStream((InputStream)iterator.nextElement()));
            }
      }
0
 
LVL 92

Expert Comment

by:objects
ID: 10839765
> if we can get SequenceAudioInputStream into AutoConcat ithe hard part will be completed

one of the reason you keep having problems and having to rework code is that you keep patching together disparate pieces of code instead of sitting back and determining what your design should look like and then implementing that design.


0
 

Author Comment

by:Drop_of_Rain
ID: 10839853
I have been taking care of getting the pieces of code that I know I can't do with the help I have available to me. And in the process of doing that I have learned things that I didn't know, and in that knowledge changed direction but not in any large way. The changes I have made seem small to me.

It would be interesting for you to be in my shoes with the knowledge I have and the perspective I see from and see if you would make that same statement.

I would like to see how many people have tried a project this complex for their first program. You guys have been very patient with me and let me make the mistakes I have. I am a very open person to suggestion if it is explained so I can see the relevance of it.

I would of never been as far as I am if you guys weren't kind enought to help me out. I appreciate it. If I get this handled with the audio part I believe I can make most of this program.
0
 
LVL 92

Expert Comment

by:objects
ID: 10839893
yes i understand all that :)
0
 

Author Comment

by:Drop_of_Rain
ID: 10839945
OK, so back to the drawing board how do we accomplish this part. Do I just close this question because it would be better to use 2 lists with what I need to have do. Then so be it!
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10839952
I have already told you to keep two lists as it is.  Otherwise, embed the audio file with the JColoredButton.

Choice is yours.
0
 
LVL 92

Expert Comment

by:objects
ID: 10839963
> Otherwise, embed the audio file with the JColoredButton.

how would that help? the buttons have nothing to do with the processing of the audio.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10839972
whenever he clicks on a button, he will add an audio to the list.  the audio file could be directly attached with the button itself.

based on conditions he would change the audio file for that given button.  

The audio file will go to one list always and the button list will not need to know about the audio file for the corresponding button.
0
 
LVL 92

Expert Comment

by:objects
ID: 10839981
>  because it would be better to use 2 lists

depends on how the data is needed to be used. If the data in the two list is strongly tied and should always remain together then one list may be the go but i wouldn't add two elements to it and instead use a wrapper class to store all related data.

Personally i'd probably use a slightly different approach entirely to what you have derived so far but thats another story :)
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10840004
As he is maintaining all the data structure as List.  We can't change in the middle.

Other better approaches would be like fireChangeProperty and a different data structure all together to maintain and process the audio file.

But it is a complete different story as you said.
0
 
LVL 92

Expert Comment

by:objects
ID: 10840033
> As he is maintaining all the data structure as List.  We can't change in the middle.

not what I was referring to.
 
> Other better approaches would be like fireChangeProperty and a different data structure all together to maintain and process the audio file.

That doesn't seem suitable for this application.



0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10840043
I have seen his other codings too...

That is the reason, why am confidently saying...
0
 

Author Comment

by:Drop_of_Rain
ID: 10840062
Well going through all this has made you see a better way, lucky you,  I wish I had that luxury. I have no idea what you just said about a wrapper class. It seems that by now you should have a good idea of what I am trying to do. To me I see one list because there is only one list in my mind. Because in real life that is how it happens. I have explained basicly what I am trying to do, but I can't expect you to remember what I am working on. The end product will be a person will put a CD in a player and hear the commands of what to do in their workout, everything will be included, a warm up, medical warnings, timing between exercises. The custom workout section will allow them to create a personal workout themselves by pressing some buttons. There will be an audio section were they will be able to add there own voice allowing another teacher in a different field to be able to make their own CD's.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 10840085
A wrapper class would be like:

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

You would then add instances of it to your list, and thus only require one list
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10840107
Where will you add the buttons then....

Actually needs a list for button
and a list for audio file....

He is expecting both the things to be added in a list say in odd all buttons and in even all audio ... something like that...

0
 

Author Comment

by:Drop_of_Rain
ID: 10840124
That is great. How would I use that with the above code.

Do you get a better understanding of what I am trying to do now. Not such a simple starter project.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 10840152
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 (thList.contains(details))
       {
          theList.remove(details);
       }
       else
       {
          theList.add(details);
       }
    }

    public List getList() { return theList; }

}
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10840154
The same code in a different class that's all.

I have suggested you to have the actionFile in the JColoredButton itself.

But this still needs to two lists and/or odd, even strategy in a single list.
0
 
LVL 92

Expert Comment

by:objects
ID: 10840168
the buttons are irrelevant to the processing so no point storing them, it would create too much coupling between gui and processing.
and only a single list is needed and no need for concerns with odd/even.
0
 

Author Comment

by:Drop_of_Rain
ID: 10840174
He is expecting both the things to be added in a list say in odd all buttons and in even all audio ... something like that...

What do you mean by odd all buttons. The odd would be the audiofile.wav and the even would be the timing.wav audiofile.

That is why I thought that if there was one list when the button was pressed again the corresponding audio file would be removed. Then a if question checked if a file was removed and if so it removed  the even number file would be removed as well, and all is well.
0
 

Author Comment

by:Drop_of_Rain
ID: 10851346
objects this is a wonderful answer your the greatest thanks for helping me so much!
0
 
LVL 92

Expert Comment

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

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

23 Experts available now in Live!

Get 1:1 Help Now