Creating the Select Help Voice Interface

The file allows end users to select the voice for reading out help information.

Listing 4-2 shows the contents of the file:

Listing 4-2: The File
start example
 /*Import required swing classes.*/ import javax.swing.JRadioButton; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.ButtonGroup; import javax.swing.JOptionPane; import javax.swing.event.*; /*Import required awt classes.*/ import java.awt.Font; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /*Import required util classes.*/ import java.util.Enumeration; import java.util.Locale; import java.util.Hashtable; /*Import required speech classes.*/ import javax.speech.Central; import javax.speech.Engine; import javax.speech.EngineList; import javax.speech.synthesis.Synthesizer; import javax.speech.synthesis.SynthesizerModeDesc; import javax.speech.synthesis.SynthesizerProperties; import javax.speech.synthesis.Voice; /* Class: SelectHelpVoice-Selects the voice in which the synthesizer speaks the text. Fields: retValue: Represents the voice that is currently selected by the user. Methods: getVoices():Retrieves all the voices in the general locale that are available on the user's computer. actionPerformed(): Determines the operations to be performed when the user clicks any button. */ public class SelectHelpVoice extends JFrame implements ActionListener {    /*Declare variables.*/    JLabel pageTitle;    JRadioButton rb;    ButtonGroup bg;    JButton ok,cancel;    JPanel voicePanel;    JPanel buttonPanel;    JPanel mainPanel;    Font f,f1;    Hashtable voicehash;    SpeakHelp speakhelp = null;    /*Set the default value for the selected voice.*/    public static String retValue = "kevin16";    public static String retkey=null;    /*    SelectHelpVoice: Define the default constructor to create the user interface for the    SelectHelpVoice.    Parameters:    speakhelp: An object of class    Return Type: NA    */    public SelectHelpVoice(SpeakHelp speakhelp)    {       this.speakhelp=speakhelp;       this.setSize(150,200);       this.setResizable(false);       /*Initialize the variables.*/       pageTitle=new JLabel("Select the voice");       voicePanel=new JPanel();       buttonPanel=new JPanel();       mainPanel=new JPanel();       bg=new ButtonGroup();       /*Set layout for the panels.*/       buttonPanel.setLayout(new GridLayout(1,2));       mainPanel.setLayout(new BorderLayout());       /*Create objects of Font class.*/       f=new Font("Verdana", Font.BOLD,12);       pageTitle.setFont(f);       f1=new Font("Verdana", Font.BOLD,10);       /*Initialize the OK button.*/       ok=new JButton("OK");       ok.setFont(f1);       ok.addActionListener(this);       /*Initialize the cancel button.*/       cancel=new JButton("Cancel");       cancel.setFont(f1);       cancel.addActionListener(this);       /*        Invoke the getVoices() method to retrieve all available voices.       */       getVoices("general");       /*Add JButtons to buttonPanel.*/       buttonPanel.add(ok);       buttonPanel.add(cancel);       /*Add contents to the mainPanel.*/       mainPanel.add(pageTitle, BorderLayout.NORTH);       mainPanel.add(voicePanel, BorderLayout.CENTER);       mainPanel.add(buttonPanel, BorderLayout.SOUTH);       /*Add mainPanel to the main window.*/       getContentPane().add(mainPanel);       /*Add window listener to this window.*/       addWindowListener(new WindowAdapter()       {          public void windowClosing(WindowEvent we)          {             setVisible(false);          }       });    }    /*    getVoices(): Retrieves all the voices available in the general locale of user computer.    Parameters:    modename: String representing the selected mode.    Return Type: NA    */    public void getVoices(String modename)    {       /*       Create an object of SynthesizerModeDesc class       */       SynthesizerModeDesc required = new SynthesizerModeDesc       (           null,          modename,          Locale.US,          null,          null);          /*          Retrieve available synthesizers in an abject of EngineList class          */          EngineList engineList = Central.availableSynthesizers(required);          /*          Run a loop from 1 to the size of EngineList object          */          voicehash=new Hashtable();          for(int i = 0; i<engineList.size(); i++)           {             /*             Initialize an object of SynthesizerModeDesc class             */             SynthesizerModeDesc desc = (SynthesizerModeDesc) engineList.get(i);             /*             Retrieve the voices available in the current synthesizer             */             Voice[] voices = desc.getVoices();             /*             Set layout of the voicePanel             */             voicePanel.setLayout(new GridLayout(voices.length,1));             /*             Add all the available voices to the voicePanel             */             for (int j = 0; j < voices.length; j++)              {                String temp=voices[j].getName().toString();                int x=j+1;                String voiceno="Voice "+x;                voicehash.put(voiceno,temp);                System.out.println(temp);                /*                Initialize an object of JRadioButton class                */                rb=new JRadioButton(voiceno);                /*                Set kelvin16 as the default selected voice                */                if(temp.equals("kevin16"))                rb.setSelected(true);                /*                Add JRadioButton to a buttopngroup                */                bg.add(rb);                /*                Add JRadioButton to voicePanel                */                voicePanel.add(rb);             }          }       }       /*       actionPerformed(): Defines the operations to be performed when the user clicks any button.       Parameters:       ev: An object of ActionEvent class       Return Type:NA       */       public void actionPerformed(ActionEvent ev)       {          /*          This code executes when the user clicks the OK button.          */          if(ev.getSource()==ok)          {          /*          Retrieve all the elements in the button group in an enumeration.          */          Enumeration enum=bg.getElements();          /*          Run until the enumeration has any element.          */          while(enum.hasMoreElements())          {             JRadioButton button=(JRadioButton)enum.nextElement();             /*             Retrieve the label of the JRadioButton that is currently selected.             */             if(button.isSelected())             {                retkey=button.getText();                retValue=(String)voicehash.get(retkey);                this.setVisible(false);                speakhelp.setvoice(retValue);             }          }       }       else       /*       This part of code executes when the user clicks cancel button.       */       if(ev.getSource()==cancel)       {          /*          Hide the user interface of file.          */          this.setVisible(false);       }    } } 
end example

Download this listing .

In the above code, the constructor of the SelectHelpVoice class takes an object of the SpeakHelp class as an input parameter to invoke the methods of the SpeakHelp class.

The methods defined in Listing 4-2 are:

  • actionPerformed (): Acts as an event listener and activates an appropriate method based on the action an end user performs .

  • getVoices (): Retrieves all the voices available on an end user's computer.

The file creates the user interface for selecting a voice.

Select Voice->Select Voice to select the help voice. The user interface to select a voice appears, as shown in Figure 4-5:

this figure shows the dialog box containing radio buttons, labels, and buttons.
Figure 4-5: Selecting a Voice

Select Voice-> Speak to convert text to speech. The speech conversion occurs in the voice selected by an end user.

Java InstantCode. Developing Applications using Java Speech API
Java InstantCode. Developing Applications using Java Speech API
Year: 2004
Pages: 46 © 2008-2017.
If you may any questions please contact us: