I have an application which i am going to install on Linux touch system. The touch system is not giving me the touch sound so i decided to have that feature on my application. What is the best way to do it ? I dont want to go through each and every buttons and other components and write the codes there. Is there any global way to handle that so that the sound works throughout the application when ever the screen is touched or mouse is clicked ??
-
why? If it's not the standard on that platform, your users most probably will be annoyed ... If it is kind-of standard on the platform and you can manually add it in your application as well, make sure it respects a per-system disable of the sound – kleopatra Apr 26 '11 at 13:20
3 Answers
Try this
Toolkit.getDefaultToolkit().beep();
To play it in all MouseEvent make your MouseEvent listener single and everytime when you need use that listener and write above code in that mouse listener.

- 58,650
- 30
- 162
- 207
-
but i have completed 90% of my application its almost impossible to redesign the whole thing to listen to just one mouse listener. SO i need some other alternative!\ – Deepak Apr 26 '11 at 14:19
-
@Deepak: Then you have to copy/paste same code every where you want sound effect. – Harry Joy Apr 27 '11 at 03:52
-
-
@Deepak: I don't think so. May be you should start bounty to get more good answers. – Harry Joy Apr 27 '11 at 08:44
-
but this is not workingon linux ? which sound setting should i enable for getting the sound on linux using ur code bit ? – Deepak Apr 27 '11 at 10:26
-
1@Deepak: I'm using this. For me it's working on both windows and linux without changing any sound settings. – Harry Joy Apr 27 '11 at 10:31
-
i am using Fedora 14 but its not working there... i am not getting any sound – Deepak Apr 27 '11 at 12:35
-
@Deepak: as a option what you can do is: include a sound clip in your app and play that audio clip when some event happens. But also for this you have to either add code all event or make your event handler single. – Harry Joy May 05 '11 at 03:55
-
@Deepak: Also try ASCII value to play beep sound like this `System.out.println("\007");` I'm not sure about this but might work in fedora. – Harry Joy May 05 '11 at 03:58
-
this is ok but not good enough in general because the beep is generic default and could signal any number of ongoing events. A more customized sound for the specific event is much better – Sep 11 '20 at 08:35
Take this answer: How can I play sound in Java?
... and trigger the playback in the mouse click event.
Assuming that you are using the JFC/Swing toolkit, you might want to read a bit about using the Multiplexing Look & Feel. Even though I don't know of any ready-to-use auxiliary look and feel(s) that might fit your needs, it should be possible to write your own auxiliary look and feel which behaves as you describe above ...
... after you have created your own auxiliary look and feel make sure to start your java interpreter with the "-Dswing.auxiliarylaf=your.auxiliary.look.and.feel.Classname" parameter.

- 308
- 2
- 8
-
-
Maybe I misunderstood your question. I assumed, that you would want a sound ONLY if a component (such as a button) was being used. Playing a sound, everytime the user touches the screen makes no sense (in my opinion at least) ... if that assumption was wrong you may disregard my suggestion entirely! ;) – headcr4sh Apr 27 '11 at 14:40