Selecting a Port


The PortChoice.java file allows an end user to display and select the port used to print the text in the document. The PortChoice.java file creates a user interface that displays the list of ports on a computer. The end user can select the port to which the printer is connected from the list of ports. The user interface of the PortChoice.java file appears when the end user selects the Choose COM Port to Print menu option on the File menu of the Printing application.

Listing 4-2 shows the PortChoice.java file:

Listing 4-2: The PortChoice.java File

start example
 /* Imports required Comm classes */ import javax.comm.*; /* Imports required I/O classes */ import java.io.*; /* Imports required AWT classes  */ import java.awt.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; /* Imports required Util classes */ import java.util.*; /* class PortChoice - This class is the main class of the application. This class initializes the interface and loads all components like the dropdown listbox, button  before displaying the result.    Constructor:       PortChoice-This constructor creates GUI.    Methods: getPortDescription - This method gives port description. openPort - This method opens a selected port. getSerialPorts - This method fill list of serial ports. getCommPorts - This method fill list of comm ports. emptyList - This method clear a list. main - This method creates the main window of the application and displays all the components. */ public class PortChoice extends JDialog implements ItemListener  {    protected String selectedPortName;    /*        Declare object of JButton class.    */    JButton okButton;    JButton cancelButton;    /*        Declare object of JComboBox class.    */    protected JComboBox commChoice;    /*        Declare objects of JLabel class.    */    JLabel frametextlabel;    JLabel parallelportlabel;    /*     Declare object of CommPortIdentifier class.    */    protected CommPortIdentifier selectedPortIdentifier;    /*        Declare object of CommPrinting class.    */    CommPrinting commprint;    protected final int PAD = 5;    /*    itemStateChanged: This method is called where end user select item from dropdown listbox.    Parameter: e - an ItemEvent object containing details of the event.    Return Value: N/A    */    public void itemStateChanged(ItemEvent e)     {       selectedPortName = (String)((JComboBox)e.getSource()).getSelectedItem();       try       {          selectedPortIdentifier = javax.comm.CommPortIdentifier.getPortIdentifier(selectedPortName);       }       catch(NoSuchPortException pe)       {          System.out.println("No port exists"+pe);       }    }    /*    itemStateChanged: This method is called to get selected port name.    Parameter: N/A    Return Value: String    */    public String getSelectedName()     {       return selectedPortName;    }    /*    itemStateChanged: This method is called to get selected port.    Parameter: N/A    Return Value: CommPortIdentifier    */    public CommPortIdentifier getSelectedIdentifier()     {       return selectedPortIdentifier;    }    public PortChoice(JFrame parent, CommPrinting cprint)     {       super(parent, "COM Port Selection", true);       commprint=cprint;       makeGUI();       populate();       finishGUI();    }    /*    makeGUI: This method is called to create GUI.    Parameter: N/A    Return Value: N/A    */    protected void makeGUI()     {       /*        Initialize and set the Look and Feel of the application to Windows Look and Feel.       */       try       {          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());       }       catch(Exception e)       {          System.out.println("Exception in setting WLAF"+e);       }       Container cp = getContentPane();       setResizable(false);       /*        Initialize the object of JLabel class.       */       frametextlabel=new JLabel("Choose COM Port to Print");       Dimension  scrnSize = Toolkit.getDefaultToolkit().getScreenSize();       /*       Set the location at which window will be displayed.       */       setLocation((scrnSize.width / 2) - 350, (scrnSize.height / 2) - 250);       frametextlabel.setHorizontalAlignment(JLabel.CENTER);       frametextlabel.setFont(new Font("Verdana", Font.BOLD, 14));       cp.add(BorderLayout.NORTH,frametextlabel);       /*        Declare object of centerPanel class and initialize it.       */       JPanel centerPanel = new JPanel();       cp.add(BorderLayout.CENTER, centerPanel);       /*          Set frame size.       */       setSize(250,110);       GridBagLayout gblayout=new GridBagLayout();       /*          Set frame layout to GridBagLayout.       */       centerPanel.setLayout(gblayout);       GridBagConstraints gbconstraints=new GridBagConstraints();       gbconstraints.fill=GridBagConstraints.HORIZONTAL;       gbconstraints.gridx=0;       gbconstraints.gridy=0;       gbconstraints.weightx=1.0;       gbconstraints.weighty=1.0;       gbconstraints.anchor=GridBagConstraints.CENTER;       parallelportlabel=new JLabel("COM Ports");       parallelportlabel.setHorizontalAlignment(JLabel.CENTER);       parallelportlabel.setFont(new Font("Verdana", Font.PLAIN, 12));       gblayout.setConstraints(parallelportlabel,gbconstraints);       centerPanel.add(parallelportlabel);       gbconstraints.gridx=1;       gbconstraints.gridy=0;       gbconstraints.weightx=1.0;       gbconstraints.weighty=1.0;       gbconstraints.gridwidth=1;       gbconstraints.gridheight=1;       gbconstraints.ipadx=0;       gbconstraints.ipady=0;       gbconstraints.anchor=GridBagConstraints.WEST;       commChoice = new JComboBox();       gblayout.setConstraints(commChoice,gbconstraints);       centerPanel.add(commChoice);       commChoice.setEnabled(false);       addWindowListener(new WindowAdapter()       {          public void windowClosed(WindowEvent we)          {             PortChoice.this.dispose();          }       });       JPanel jbuttonpanel=new JPanel(new FlowLayout());       jbuttonpanel.add(okButton = new JButton("OK"));       jbuttonpanel.add(cancelButton = new JButton("Cancel"));       cp.add(BorderLayout.SOUTH, jbuttonpanel);       okButton.addActionListener(new ActionListener()       {          public void actionPerformed(ActionEvent e)           {             commprint.printDoc(getSelectedName(),getSelectedIdentifier());          }       });       cancelButton.addActionListener(new ActionListener()       {          public void actionPerformed(ActionEvent ae)          {             setVisible(false);          }       });    }    /*    populate: This method is called to populate COM port in dropdown listbox.    Parameter: N/A    Return Value: N/A    */    protected void populate()     {       Enumeration pList = CommPortIdentifier.getPortIdentifiers();       while (pList.hasMoreElements())        {          CommPortIdentifier cpi = (CommPortIdentifier)pList.nextElement();          if (cpi.getPortType() == CommPortIdentifier.PORT_PARALLEL)           {             commChoice.setEnabled(true);             commChoice.addItem(cpi.getName());          }        }       commChoice.setSelectedIndex(-1);    }    /*    finishGUI: This method is called to add event listener to dropdown listbox    Parameter: N/A    Return Value: N/A    */    protected void finishGUI()     {       commChoice.addItemListener(this);    } } 
end example

Download this listing.

The above listing allows an end user to select the port used to print the text in the document. The PortChoice.java file uses the CommPortIdentifier class of the javax.comm package to retrieve the list of ports on a computer. Figure 4-5 shows the interface of the PortChoice.java file:

this figure shows a list of the com ports on a computer.
Figure 4-5: The Interface of the PortChoice.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