Creating the User Interface for the Auto Answering Machine Application


Creating the User Interface for the Auto Answering Machine Application

The AutoAnswerGUI.java file creates the user interface for the Auto Answering Machine application. Listing 8-1 shows the contents of the AutoAnswerGUI.java file:

Listing 8-1: The AutoAnswerGUI.java File
start example
 /*Import required JTAPI classes*/ import javax.telephony.*; import javax.telephony.events.*; import javax.telephony.media.*; import javax.telephony.media.events.*; import javax.swing.*; /*Import required swing and awt classes*/ import java.awt.event.*; import javax.swing.event.*; import java.awt.*; /* This class extends the JFrame class and provides the GUI for the Auto Answer application. Class:AutoAnswerGUI-GUI for the Auto Answer application Methods: buildGUI():Used to create GUI terminate():Closes the application */ public class AutoAnswerGUI extends JFrame implements ActionListener {    /*Declaring the GUI variables.*/    Container cont = null;    JButton getProvider_button, onoff_button, addlist_button, removelist_button, exit;    JComboBox lines_combo;    JMenu file_menu;    JMenuItem exit_menuitem;    String audioFilePath = null;    /*Declaring the JTAPI variables.*/    AutoAnswerObserver replyObserver = null;    Provider myprovider = null;    JtapiPeer peer = null;    Terminal terminal = null;    /*    Public constructor takes the title string as parameter.    Public constructor takes the String parameter.    AutoAnswerGUI():    Parameters:    title:Title of window    Return Type:NA    */    public AutoAnswerGUI(String title)    {       super(title);       setSize(380, 310);       addWindowListener(new WindowAdapter()       {          public void windowClosing(WindowEvent we)          {             terminate();          }       });       replyObserver = new AutoAnswerObserver(this);       lines_combo = new JComboBox();       /*       Creating the provider by obtaining the XTAPI implementation of JTAPI.       */       try       {          peer = JtapiPeerFactory.getJtapiPeer("net.xtapi.XJtapiPeer");          /*          Obtaining the default provider of the selected implementation.          */          myprovider =peer.getProvider(null);          Terminal []terminals = myprovider.getTerminals();          System.out.println("Terminal"+terminals);          for(int i = 0; i <terminals.length; i++)          {             lines_combo.insertItemAt(terminals[ i], i);             lines_combo.setSelectedIndex(0);             terminal = (Terminal)lines_combo.getSelectedItem();          }          //getProvider_button.setEnabled(false);       }       catch (Exception excp)       {          excp.printStackTrace();          JOptionPane.showMessageDialog(this, "Unable to get the provider.");       }       buildGUI();       setVisible(true);    }    /*    This method is used to build the GUI of the Auto Answer application.    Parameters:NA    Return Value:void    */    public void buildGUI()    {       cont = getContentPane();       cont.setLayout(null);       cont.setBackground(Color.white);       JLabel label = new JLabel("Select a provider from the given list");       label.setBounds(10,10,380,20);       cont.add(label); lines_combo.setBounds(10, 35, 235, 20);       lines_combo.addActionListener(this);       cont.add(lines_combo);       onoff_button = new JButton("Start Service");       onoff_button.addActionListener(this);       exit=new JButton("Exit");       exit.setBounds(230, 220, 130, 20);       onoff_button.setBounds(230, 180, 130, 20);       exit.addActionListener(this);       cont.add(onoff_button);       cont.add(exit);    }    /*     This method invokes when end user clicks on any button provided in the user interface.    */    public void actionPerformed(ActionEvent ae)    {       if (ae.getSource() == exit)       {          System.exit(0);       }       else if (ae.getSource()== lines_combo)       {          try          {             if (terminal!= null)             {                /*                Removing the call observer object associated with that terminal.                */terminal.removeCallObserver(replyObserver);             }             /*Instantiate the terminal object.*/             terminal = (Terminal)lines_combo.getSelectedItem();          }          catch (Exception e)          {             JOptionPane.showMessageDialog(this, "Unable to add the call observer to the selected             terminal.");          }       }       /*       Checking if user clicks the Start Service or Stop Service button.       */       else if (ae.getSource() == onoff_button)       {          try          {             if (onoff_button.getText().equals("Start Service"))             {                if (terminal == null)                {                   JOptionPane.showMessageDialog(this, "Please select a provider.", "Error",                   JOptionPane.ERROR_MESSAGE);                   return;                }                terminal.addCallObserver(replyObserver);                System.out.println("Service Started");             }             else             {                /*                If user clicks the Stop Service button, then remove the call observer from the                selected                terminal and set the text of the button to Start Service.                */ terminal.removeCallObserver(replyObserver);                onoff_button.setText("Start Service");                System.out.println("Service Stopped");             }          }          catch (Exception ex)          {             System.out.println("Exception occurred while add and removing the call observer to              the selected terminal." + ex.toString());          }       }    }    /* Used to terminate the application.    Parameters:NA    Return Value:Void    */    public void terminate()    {       System.exit(0);    }    /*Main method.*/    public static void main(String[] args)    {       AutoAnswerGUI frame = new A utoAnswerGUI("Auto Answer Application");    } } 
end example
 

Download this listing .

In the above code, the main() method creates an instance of the AutoAnswerGUI class.

The methods defined in Listing 8-1 are:

  • buildGUI() : Creates the user interface for the Auto Answering Machine application.

  • terminate (): Closes the application.

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

The AutoAnswerGUI.java file generates the main window of the Auto Answering Machine application, as shown in Figure 8-2:

click to expand: this figure shows the user interface of the auto answering machine application that contains a combo box to select the connection provider and the start service and exit buttons.
Figure 8-2: User Interface of the Auto Answering Machine Application

If there is no connection provider visible in the combo box or an end user starts the application without selecting any connection provider, the Error dialog box appears, as shown in Figure 8-3:

this figure shows the error dialog box that appears when an end user clicks the start service button without selecting a provider.
Figure 8-3: The Error Dialog Box

Click the Exit button to close the application.




Java InstantCode. Developing Applications using Java Speech API
Java InstantCode. Developing Applications using Java Speech API
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 46

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net