Question

How I can generate different beep sounds for different types of messages (e.g. Warning, information, question) using JOptionPane in java. As in windows OS there are different beep sound for each message that appeared to user. Currently I am using the following to generate beep, but it beeps same every time.

Toolkit.getDefaultToolkit().beep();
JJOptionPane.showMessageDialog( myFrame, "Message", "Title", JOptionPane.INFORMATION_MESSAGE );

I want a different beep for warning message, information message and question message. Is there any way to do that?

Was it helpful?

Solution

beep issues a request to the underlying operating system to emit an audio beep. On certain platforms such as Windows, the "default sound" is played.

Rather than attempting to interface with specific platforms, it is simpler to play a corresponding sound file prior to displaying the dialog:

AudioInputStream ais = 
     AudioSystem.getAudioInputStream(getClass().getResource("/information.wav"));
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.start();            
JOptionPane.showMessageDialog(...);
clip.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top