Link to home
Start Free TrialLog in
Avatar of Drop_of_Rain
Drop_of_Rain

asked on

How to access a audio file from these checkboxs

This code was used to create the layout, but how would I be able to access a checkbox from this code when audio files and checboxs are in a hashmap. (code included)


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MyClass extends JPanel {
 
  ArrayList myArrayList = new ArrayList();

  // Constructor
  public MyClass() {
    JPanel myPanel = new JPanel();
    myPanel.setLayout(new GridLayout(0,3));
    for (int row=0; row<20; row++) {
      for(int col=0; col<3; col++) {
        /* choose one of these numbering */
        int labelint = 20 * col + row;
        //int labelint = col + 3 * row;
        JCheckBox check = new JCheckBox("" + labelint);
        myArrayList.add(check);
        myPanel.add(check);
      }
    }
    this.setLayout(new FlowLayout());
    this.add(myPanel);
  }

  private static final String FRAME_TITLE = "My Class";
  private static final int FRAME_WIDTH  = 150;
  private static final int FRAME_HEIGHT = 300;

  public void showFrame() {
    JFrame myFrame = new JFrame(FRAME_TITLE) {
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); }
      }
    };
    myFrame.getContentPane().add(this, BorderLayout.CENTER);
    myFrame.pack();
    myFrame.setSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
    myFrame.setVisible(true);
  }

  public static void main(String[] args) {
    MyClass myMyClass = new MyClass();
    myMyClass.showFrame();
  }

}

//checkbox to hashmap
public class CheckBoxesToAudioFiles {
Map checkToAudio;

public CheckBoxesToAudioFiles {
checkToAudio = new HashMap();
}

public void mapCheckboxToAudioFile(JCheckbox cb, AudioFile af) {
checkToAudio.put(cb, af);
}

public AudioFile getAudioFileForCheckbox(JCheckbox cb) {
(AudioFile)return checkToAudio.get(cb);
}

}

Avatar of Javatm
Javatm
Flag of Singapore image

You need 1st to know how to play music in Java consider this example :
https://www.experts-exchange.com/questions/20641397/How-to-play-a-music-in-Java.html

Hope that helps . . .
Javatm
Avatar of Drop_of_Rain
Drop_of_Rain

ASKER

I have that code that was easy. I even have a player in my FileChooser. I wasn't clear I need to know how to access the checkboxs in MyClass. Code below

public MyClass() {
    JPanel myPanel = new JPanel();
    myPanel.setLayout(new GridLayout(0,3));
    for (int row=0; row<20; row++) {
      for(int col=0; col<3; col++) {
        /* choose one of these numbering */
        int labelint = 20 * col + row;
        //int labelint = col + 3 * row;
        JCheckBox check = new JCheckBox("" + labelint);
        myArrayList.add(check);
        myPanel.add(check);
      }
    }
ASKER CERTIFIED SOLUTION
Avatar of Javatm
Javatm
Flag of Singapore 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
> Or else you need to import it to use the codes above.

While I'm not arround read this to help you use package and importing it :
http://java.sun.com/docs/books/tutorial/java/interpack/packages.html

Hope that helps . . .
Javatm
Are the numbers generated from the code below, the names for the checkboxs in the arraylist. I know this is a dump question.

public class MyClass extends JPanel {
 
  ArrayList myArrayList = new ArrayList();

  // Constructor
  public MyClass() {
    JPanel myPanel = new JPanel();
    myPanel.setLayout(new GridLayout(0,3));
    for (int row=0; row<20; row++) {
      for(int col=0; col<3; col++) {
        /* choose one of these numbering */
        int labelint = 20 * col + row;
        //int labelint = col + 3 * row;
        JCheckBox check = new JCheckBox("" + labelint);
        myArrayList.add(check);
        myPanel.add(check);
      }
    }
> Are the numbers generated from the code below, the names for the checkboxs in the arraylist.

Yes, comming from :

for (int row=0; row<20; row++) {
      for(int col=0; col<3; col++) {
        /* choose one of these numbering */
        int labelint = 20 * col + row;
        // int labelint = col + 3 * row;
        JCheckBox check = new JCheckBox("" + labelint);
        myArrayList.add(check);
        myPanel.add(check);
      }
    }
Could someone please explain how this code, or better yet write a small program that uses it in both directions to help me understand this. I know this is a great piece of code for what i am trying to accomplish, I just don't get it. HELP PLEASE


public class MyClass extends JPanel {
 
  ArrayList myArrayList = new ArrayList();

  // Constructor
  public MyClass() {
    JPanel myPanel = new JPanel();
    myPanel.setLayout(new GridLayout(0,3));
    for (int row=0; row<20; row++) {
      for(int col=0; col<3; col++) {
        /* choose one of these numbering */
        int labelint = 20 * col + row;
        //int labelint = col + 3 * row;
        JCheckBox check = new JCheckBox("" + labelint);
        myArrayList.add(check);
        myPanel.add(check);
      }
    }



//checkbox to hashmap
public class CheckBoxesToAudioFiles {
Map checkToAudio;

public CheckBoxesToAudioFiles {
checkToAudio = new HashMap();
}

public void mapCheckboxToAudioFile(JCheckbox cb, AudioFile af) {
checkToAudio.put(cb, af);
}

public AudioFile getAudioFileForCheckbox(JCheckbox cb) {
(AudioFile)return checkToAudio.get(cb);
}

OK, so how would I link the checboxs in MyArrayList to the hashmap where the audio files and the checkboxs are linked together. Or would it be better to let go of the hashmap and use seperate arraylist for each checkbox group.
I ask this now because this came from another question. Please read to understand what I am trying to ask and where my thoughts are going now.

https://www.experts-exchange.com/questions/20920844/Five-arraylist-need-to-use-the-same-audio-files-in-another-arraylist.html

In that question I stated that 5 arraylist would be using the same audio files. What I am thinking know if I made seperate unique audio files then I wouldn't have this problem, if it is a problem. It just looks that way to me.
My problem is in understanding the hashmap. I read up on it but that just sliped right though my consciousness. So if some one could explain it in layman terms to me so I can get it, then I can move on with this project.

Another problem is I get micro focused until I resolve it in some way. I hope that not to many of you have this infliction.

Thanks for all of your help.

Rgds,
Christopher
Okey honestly saying I dont understand your question here specially that your codes above produces errors
because of some codes or file missing, if you dont mind can you just explain it all to me one by one so
that I can help you w/ your problem, thanks.

Friend : Javatm
The program I am writing but not without all of your help and others.

It will be a training program that will be customizable. It will have many different checkboxs that will triger random number generators, pattern sequencers and all the different sections of the workouts. Each section will have a checkbox for each part of the workout. Any or all parts of a workout can be choosen. Each checkbox group (section) will then be passed to a number mixing class that will create patterns. Example (1234,4321,4123,3421, etc in groups of 3,4 or 5 depending what section because of the number of parts in that section. Another class will then choose different 4 number groups and then pass 1 number at a time into an arraylist. These single numbers will corespond will an audio file. Thes audio files the need to be collected from different sections and made into one new concatenated WAVE audio file. All the concatenated audio files from all created workouts will then be burned onto a CD for the trainee to use.

I have increased the points because this just got into a big question. Thanks for helping
Hmp.

This problem of yours is some what hard :) as what I can see you need to specialize your self 1st in this field
of sound programming. I've created sound application before but to tell you the truth, it really gave me a pane
in the head. So if I where you why not get a book about sound programming in Java and study it.

I played music before in Java but it wont play if the sound is so big so I have to strip the quality of the sound
before I can get it to play, either ways you can use JMF to be able to maximize your program :

http://java.sun.com/products/java-media/jmf/ 

This could also help : http://www.jsresources.org/

Hope that helps . . .
Javatm