Home Java Example Java Awt Beep Java



Beep Java
Posted on: June 19, 2007 at 12:00 AM
Lets learn how to create beep using java. We can create a beep sound to indicate a warning or error.

Beep Java

     

Lets learn how to create beep using java.

We can create a beep sound to indicate a warning or error by using java. However, it is often better to give a warning dialog box with a description about  the problem using GUI interface.

There are  two ways to produce the sound beep:
1.Using Toolkit class: We can create a beep using Toolkit class. For this we first need to import java.awt.Toolkit. This class provides beep() method to create beep sound. The following syntax is used to create beep using Toolkit class:
import java.awt.Toolkit;
Use this statment in your code:
Toolkit.getDefaultToolkit().beep().
This is not always a sufficient way to alert the user as there may not be speakers on the user's computer.
2.Using ASCII value: ASCII refer as American Standard Code for Information Interchange. We use ASCII value "\007" to produce bee
p. As we are using in our example.

Here is the code of the program :

import java.util.*;
public class Beep{

 
  public static void main(String[] args) {

  if(args[0].equals("1"))
  System.out.println("\007");
  if(args[0].equals("2"))
  System.out.println("\007\007");
  if(args[0].equals("3"))
  System.out.println("\007\007\007");
  if(args[0].equals("4"))
  System.out.println("\007\007\007\007");
  if(args[0].equals("5"))
  System.out.println("\007\007\007\007\007");
  if(args[0].equals("6"))
  System.out.println("\007\007\007\007\007\007");
  if(args[0].equals("7"))
  System.out.println("\007\007\007\007\007\007\007");
  if(args[0].equals("8"))
  System.out.println("\007\007\007\007\007\007\007\007");
  }

}

Download this example.

Related Tags for Beep Java:
javacideclassimportsoundmethodviportawtusingthisidtooloocreatetoolkitfortoeitdescanimfirstceinasbeepmcajclesmeprossoeeatkisirkiteaduarrtwtvatwssthavstprodprndolo


More Tutorials from this section

Ask Questions?    Discuss: Beep Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.