Selecting a Port


The PortChooser.java file creates and displays a user interface to select the port to which the modem is connected. The modem makes a phone call to the phone number specified by the end user.

Listing 3-2 shows the PortChooser.java file:

Listing 3-2: The PortChooser.java File

start example
 /*Imports required Comm package classes.*/ import javax.comm.*; /*Imports required I/O package classes.*/ import java.io.*; /*Imports required AWT classes.*/ import java.awt.*; import java.awt.event.*; /*Imports required javax.swing package classes.*/ import javax.swing.*; /*Imports required java.util package classes.*/ import java.util.*; /* class PortChooser - This class is the main class of the application.  This class initializes the interface and loads all components like the button, combobox before displaying the result. Constructor: PortChooser - This constructor calls methods to create GUI and populating port list. Methods:    getSelectedName - Gives comm port name.    getSelectedIdentifier - Gives comm port.    makeGUI - Creates GUI.  populate - Generates list of serial and parallel ports itemStateChanged: This method is called when end user select item from combobox. */ public class PortChooser extends JFrame implements ItemListener  {    /*     Declare and initialize object of HashMap class.    */    protected HashMap map = new HashMap();    /*     Declare and initialize object of CommPortIdentifier class.    */    protected CommPortIdentifier selectedportidentifier=null;    /* Declare objects of JComboBox class.*/    protected JComboBox serialportschoice;    protected JComboBox parallelportschoice;    /* Declare objects of JButton class.*/    private JButton okbutton;    private JButton cancelbutton;    /* Declare objects of JPanel class.*/    private JPanel bottompane;     private JPanel centerpanel;    /*Declare constraints.*/    protected String selectedportname=null;    protected final int PAD =15;    private final int BUTTON_WIDTH=80;    private final int BUTTON_HEIGHT=23;    private boolean itemchangeflag=true;    /*    getSelectedName: It is called to get selected port name    Parameter: NA    Return Value: String    */    public String getSelectedName()     {       return selectedportname;    }    /*    getSelectedIdentifier: It is called to get selected port.     Parameter: NA    Return Value: CommPortIdentifier     */    public CommPortIdentifier getSelectedIdentifier()     {       return selectedportidentifier;    }    public PortChooser(JFrame parent)     {       /*Set window title.*/       super(" Selecting Port ");       /*calls makeGUI method.*/       makeGUI(parent);       /*calls populate method.*/       populate();    }    /*    makeGUI: This method creates GUI.    Parameter: parent - Object of JFrame.    Return Value: NA    */    public void makeGUI(JFrame parent)     {       /*       Declare and initialize object of Container class.       */       Container cp = getContentPane();       /*Set window's location on screen.*/    setLocation(parent.getLocation().x,parent.getLocation().y);       /*Initialize object of JPanel.*/       centerpanel = new JPanel();       /*Add centerpanel to ContentPane.*/       cp.add(BorderLayout.CENTER, centerpanel);       /*Set layout as GridLayout.*/       centerpanel.setLayout(new GridLayout(0, 2, PAD, PAD));       /*       Add object of JLabel class to ContentPane.       */       centerpanel.add(new JLabel("Serial Ports", JLabel.RIGHT));       /*       Initialize object of JComboBox class.       */       serialportschoice = new JComboBox();       /*       Add action listener to serialportschoice Combobox.       */       serialportschoice.addItemListener(this);       /*       Add serialportschoice Combobox to centerpanel panel.       */       centerpanel.add(serialportschoice);       /*       Add object of JLabel class to ContentPane.       */       centerpanel.add(new JLabel("Parallel Ports", JLabel.RIGHT));       /*Initialize object of JComboBox class.*/       parallelportschoice = new JComboBox();       /*       Add action listener to parallelportschoice Combobox.       */    parallelportschoice.addItemListener(this);       /*       Add serialportschoice Combobox to centerpanel panel.       */       centerpanel.add(parallelportschoice);       /*Initialize object of JPanel class*/       bottompane=new JPanel();       /*       Initialize objects of JButton class       */       okbutton = new JButton("OK");       cancelbutton = new JButton("Cancel");       /*Set buttons size.*/       okbutton.setPreferredSize(new Dimension(BUTTON_WIDTH,BUTTON_HEIGHT));       cancelbutton.setPreferredSize(new Dimension(BUTTON_WIDTH,BUTTON_HEIGHT));       /*Add buttons to bottompane panel.*/       bottompane.add(okbutton);       bottompane.add(cancelbutton);       /*       Add bottompane panel to cp ContentPane.        */       cp.add(bottompane,BorderLayout.SOUTH);       /* Set size of window.*/       setSize(280,120);       /*Add actionlistener to cancelbutton.*/       cancelbutton.addActionListener(new ActionListener()        {          public void actionPerformed(ActionEvent e)           {          PortChooser.this.dispose();          }       });       }    /*    populate: This method generates ports list.    Parameter: NA    Return Value: NA    */    public void populate()     {       serialportschoice.addItem("");       parallelportschoice.addItem("");       /* Declare object of Enumeration class and fills it with the ports obtains  by getPortIdentifiers method of CommPortIdentifier.       */       Enumeration pList = CommPortIdentifier.getPortIdentifiers();       while (pList.hasMoreElements())        {    CommPortIdentifier cpi = (CommPortIdentifier)pList.nextElement();          /*          Put comm port in to object of hashmap class.          */          map.put(cpi.getName(), cpi);          /*          This will execute if port is serial.          */          if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL)           {    serialportschoice.addItem(cpi.getName());          }          /*          This will execute if port is parallel.          */          else if (cpi.getPortType() == CommPortIdentifier.PORT_PARALLEL)           {    parallelportschoice.addItem(cpi.getName());          }          }       serialportschoice.setSelectedIndex(0);       parallelportschoice.setSelectedIndex(0);    }    /*    itemStateChanged: This method is called when end user select item from combobox.    Parameter: e - object of ItemEvent class.    Return Value: NA    */    public void itemStateChanged(ItemEvent e)     {    if ((String)((JComboBox)e.getSource()).getSelectedItem()!="")       {          /*Get selected item name. */    selectedportname = (String)((JComboBox)e.getSource()).getSelectedItem();    selectedportidentifier = (CommPortIdentifier)map.get(selectedportname);    if (serialportschoice.equals((JComboBox)e.getSource()))          {    if (parallelportschoice.getSelectedIndex()>0)             {    parallelportschoice.setSelectedIndex(0);             }          }          else          {    serialportschoice.setSelectedIndex(0);          }       }    }    /*    getOkButton: This method returns an object of OK button.    Parameter: NA    Return Value: JButton    */    public JButton getOkButton()    {       return okbutton;    } } 
end example

Download this listing.

The above listing displays a user interface that lists the ports on a computer. From this list, the end user can select the port to which the modem is connected. The PortChooser.java file uses the CommPortIdentifier class of javax.comm to retrieve the list of ports on a computer.

The user interface of the PortChooser.java file appears when an end user clicks Browse in the user interface of the Modem Dialer application. Figure 3-3 shows the user interface of the PortChooser.java file:

this figure shows the user interface of the portchooser.java file, which provides options to select a port.
Figure 3-3: The User Interface of the PortChooser.java File

In the PortChooser.java file, the default constructor of the PortChooser class accepts an object of the PortModem class as an input parameter. The various methods defined in the PortChooser class are:

  • makeGUI(): Creates the user interface to select the Serial port to which the modem is connected.

  • Populate(): Generates a list of serial and parallel ports.

  • itemStateChanged(): Retrieves the item that is currently selected in the Serial Ports or Parallel Ports drop-down list box.

  • getSelectedName(): Returns the name of the selected Serial port.

  • getSelectedIdentifier(): Returns an object of the CommPortIdentifier class that represents the selected Serial port.

  • actionPerformed(): Acts as an event listener and activates an appropriate method based on the button that the end user clicks. When the end user clicks the OK button, the actionPerformed() method opens the selected port . If the end user clicks the Cancel button, the actionPerformed() method closes the user interface of the PortChooser.java file.




Developing Applications Using JCA
Developing Applications Using JCA
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 43

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