The SelectVoiceType.java file creates the user interface to select a voice. Listing 5-2 shows the contents of the SelectVoiceType.java file:
/* 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 java.awt.Container; 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 java.util.Enumeration; 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; import java.util.Enumeration; import java.util.Locale; import java.util.Vector; class SelectVoiceType extends JFrame implements ActionListener { /*Declare object of JRadioButton class.*/ JRadioButton voiceradiobutton; /* Declare object of voiceradiobuttongroup class. */ ButtonGroup voiceradiobuttongroup; /*Declare object of JLabel class.*/ JLabel pageTitlelabel; /*Declare objects of String class.*/ JButton okbutton; JButton cancelbutton; /*Declare objects of JPanel class.*/ JPanel buttonpanel; JPanel voicepanel; /* Declare and initialize object of String class. */ public static String retValue = "kevin16"; Hashtable voicehash; public static String retkey=null; SpeechCounting speechcount=null; public SelectVoiceType() { /*Sets the window title.*/ super(" Select Voice "); /* Declare and initialize object of Container class. */ Container contentpane=getContentPane(); /*Initialize object of ButtonGroup class.*/ voiceradiobuttongroup=new ButtonGroup(); /*Initialize object of JLabel class.*/ pageTitlelabel=new JLabel("Select the voice"); /*Initialize object of JPanel class.*/ voicepanel=new JPanel(); /*Adds label to contentpane.*/ contentpane.add(pageTitlelabel,BorderLayout.NORTH); /*Sets label font.*/ pageTitlelabel.setFont(new Font("Verdana", Font.BOLD, 12)); /*Initialize objects of JButton class.*/ okbutton=new JButton("OK"); cancelbutton=new JButton("Cancel"); /*Sets action command for buttons.*/ okbutton.setActionCommand("ok"); cancelbutton.setActionCommand("cancel"); /*Adds action listener for buttons.*/ okbutton.addActionListener(this); cancelbutton.addActionListener(this); /*Sets mnemonic for buttons.*/ okbutton.setMnemonic('o'); cancelbutton.setMnemonic('c'); getVoices("general"); setResizable(false); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { setVisible(false); } }); buttonpanel=new JPanel(new GridLayout(1, 4, 5, 5)); buttonpanel.add(okbutton); buttonpanel.add(cancelbutton); contentpane.add(buttonpanel,BorderLayout.SOUTH); contentpane.add(voicepanel,BorderLayout.CENTER); pack(); } /* getVoices: This method is used to get all available voices. Parameter: modename - object of String class. Return Value: 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); voicehash=new Hashtable(); /* Run a loop from 1 to the size of EngineList object. */ 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. */ voiceradiobutton=new JRadioButton(temp); /* Set kelvin16 as the default selected voice. */ if(temp.equals("kevin16")) voiceradiobutton.setSelected(true); /* Add JRadioButton to a buttongroup. */ voiceradiobuttongroup.add(voiceradiobutton); /*Add JRadioButton to voicePanel.*/ voicepanel.add(voiceradiobutton); } } } /* getOkButton: This method is used to get object of ok button. Parameter: NA Return Value: JButton */ public JButton getOkButton() { return okbutton; } /* getVoiceName: This method is used to get select voice name. Parameter: NA Return Value: getVoiceName - object of string type. */ public String getVoiceName() { /* Retrieve all the elements in the button group in an enumeration. */ Enumeration enum=voiceradiobuttongroup.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()) { retValue=button.getText(); this.setVisible(false); return retValue; } } return ""; } public void actionPerformed(ActionEvent ae) { String actioncommand=ae.getActionCommand(); /* This code executes when the user clicks the OK button. */ if(actioncommand=="ok") { /* Retrieve all the elements in the button group in an enumeration. */ Enumeration enum=voiceradiobuttongroup.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); speechcount.setvoice(retValue); } } } /*This part of code executes when the user clicks cancel button.*/ if(actioncommand=="cancel") { setVisible(false); } } }
Download this listing .
In the above code, the constructor of the SelectVoiceType class creates the user interface to select a voice.
The methods defined in Listing 5-2 are:
getOkButton: Returns an object of the OK button.
getVoices(): Retrieves all the voices available in an end user's computer.
getVoiceName: Retrieves the name of the selected voice.
Select File-> Select Voice to read out the text in the selected voice. The Select Voice dialog box appears, as shown in Figure 5-5: