0

I actually want a JFrame in which there is a combobox.

There is a folder which has 3 sound files named:

  • sound1.wav
  • sound2.wav
  • sound3.wav

The combobox should display these 3 file titles and when I click one of them it plays that sound file.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"I want to make a java program.."* Good for you. Feel free to pop back by if you should have a *question* about that, but not if the question is "Can you give me an example?". – Andrew Thompson Jun 30 '11 at 17:13
  • This problem should be broken into different parts: E.G. 1) Do a directory listing to find available files. 2) Load 3 items into a combo-box and detect selections. 3) Play a sound. Then plug the results of (1) into (2), and when a selection is detected, do (3). – Andrew Thompson Jun 30 '11 at 17:19

2 Answers2

1

You could simply search the folder and populate the combobox with the values (Example: http://www.exampledepot.com/egs/java.io/GetFiles.html).

To play the soundfiles you may want to look here: How can I play sound in Java?

Community
  • 1
  • 1
daniel.herken
  • 1,095
  • 7
  • 19
  • i am not able to understand the example. could you please elaborate – Amogh Gupta Jun 29 '11 at 07:23
  • 2
    which example? What don't you understand? – daniel.herken Jun 29 '11 at 07:26
  • The accepted answer on [How can I play sound in Java?](http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java) is horrid. See the [JavaSound info page](http://stackoverflow.com/tags/javasound/info) for an example of playing a sound using a `Clip`. – Andrew Thompson Jun 30 '11 at 17:25
1

You can use .listFiles() to return the list of files in a particular folder

File someFolder = new File("pathname");

Object[] wavFiles = someFolder.listFiles(wavExtensionFilenameFilter);
JComboBox songComboBox = new JComboBox(wavFiles);

That should get you started on the UI at least, btw is this a homework question?

D-zer0
  • 63
  • 6
  • Andrew I was double checking and there is an implementaion of listFiles() that does not require FileFilter as an argument, are you certain the code as it stands will not compile. If it does i suppose there would be performance benefits to be gained by specifying the desired filetype? ref: http://download.oracle.com/javase/6/docs/api/java/io/File.html#listFiles%28%29 – D-zer0 Jun 30 '11 at 19:52
  • Andrew I have edited my answer to the best of my ability. I have been trying to figure out how to integrate AudioSystem.getAudioFileTypes() into my answer and am quite curious as to how this would work, should i post this as a separate questions? – D-zer0 Jun 30 '11 at 20:38
  • Edited answer +1. *"..should i post this as a separate questions?"* Sounds like a good idea. – Andrew Thompson Jun 30 '11 at 20:57