FileDocCategorySizeDatePackage
PortChooser.javaAPI DocExample4977Tue Aug 14 23:33:56 BST 2001None

PortChooser

public class PortChooser extends JDialog implements ItemListener
Choose a port, any port! Java Communications is a "standard extention" and must be downloaded and installed separately from the JDK before you can even compile this program.
author
Ian F. Darwin, ian@darwinsys.com
version
$Id: PortChooser.java,v 1.6 2001/08/15 02:33:56 ian Exp $

Fields Summary
protected HashMap
map
A mapping from names to CommPortIdentifiers.
protected String
selectedPortName
The name of the choice the user made.
protected CommPortIdentifier
selectedPortIdentifier
The CommPortIdentifier the user chose.
protected JComboBox
serialPortsChoice
The JComboBox for serial ports
protected JComboBox
parallelPortsChoice
The JComboBox for parallel ports
protected JComboBox
other
The JComboBox for anything else
protected SerialPort
ttya
The SerialPort object
protected JLabel
choice
To display the chosen
protected final int
PAD
Padding in the GUI
Constructors Summary
public PortChooser(JFrame parent)
Construct a PortChooser --make the GUI and populate the ComboBoxes.

		super(parent, "Port Chooser", true);

		makeGUI();
		populate();
		finishGUI();
	
Methods Summary
protected voidfinishGUI()

		serialPortsChoice.addItemListener(this);
		parallelPortsChoice.addItemListener(this);
		other.addItemListener(this);
		pack();
		addWindowListener(new WindowCloser(this, true));
	
public CommPortIdentifiergetSelectedIdentifier()

		return selectedPortIdentifier;
	
public java.lang.StringgetSelectedName()

		return selectedPortName;
	
public voiditemStateChanged(java.awt.event.ItemEvent e)
This will be called from either of the JComboBoxen when the user selects any given item.


	                	 
	    
		// Get the name
		selectedPortName = (String)((JComboBox)e.getSource()).getSelectedItem();
		// Get the given CommPortIdentifier
		selectedPortIdentifier = (CommPortIdentifier)map.get(selectedPortName);
		// Display the name.
		choice.setText(selectedPortName);
	
public static voidmain(java.lang.String[] ap)
A test program to show up this chooser.

		PortChooser c = new PortChooser(null);
		c.setVisible(true);	// blocking wait
		System.out.println("You chose " + c.getSelectedName() +
			" (known by " + c.getSelectedIdentifier() + ").");
		System.exit(0);
	
protected voidmakeGUI()
Build the GUI. You can ignore this for now if you have not yet worked through the GUI chapter. Your mileage may vary.

		Container cp = getContentPane();

		JPanel centerPanel = new JPanel();
		cp.add(BorderLayout.CENTER, centerPanel);

		centerPanel.setLayout(new GridLayout(0,2, PAD, PAD));

		centerPanel.add(new JLabel("Serial Ports", JLabel.RIGHT));
		serialPortsChoice = new JComboBox();
		centerPanel.add(serialPortsChoice);
		serialPortsChoice.setEnabled(false);

		centerPanel.add(new JLabel("Parallel Ports", JLabel.RIGHT));
		parallelPortsChoice = new JComboBox();
		centerPanel.add(parallelPortsChoice);
		parallelPortsChoice.setEnabled(false);

		centerPanel.add(new JLabel("Unknown Ports", JLabel.RIGHT));
		other = new JComboBox();
		centerPanel.add(other);
		other.setEnabled(false);

		centerPanel.add(new JLabel("Your choice:", JLabel.RIGHT));
		centerPanel.add(choice = new JLabel());

		JButton okButton;
		cp.add(BorderLayout.SOUTH, okButton = new JButton("OK"));
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PortChooser.this.dispose();
			}
		});

	
protected voidpopulate()
Populate the ComboBoxes by asking the Java Communications API what ports it has. Since the initial information comes from a Properties file, it may not exactly reflect your hardware.

		// get list of ports available on this particular computer,
		// by calling static method in CommPortIdentifier.
		Enumeration pList = CommPortIdentifier.getPortIdentifiers();

		// Process the list, putting serial and parallel into ComboBoxes
		while (pList.hasMoreElements()) {
			CommPortIdentifier cpi = (CommPortIdentifier)pList.nextElement();
			// System.out.println("Port " + cpi.getName());
			map.put(cpi.getName(), cpi);
			if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) {
				serialPortsChoice.setEnabled(true);
				serialPortsChoice.addItem(cpi.getName());
			} else if (cpi.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
				parallelPortsChoice.setEnabled(true);
				parallelPortsChoice.addItem(cpi.getName());
			} else {
				other.setEnabled(true);
				other.addItem(cpi.getName());
			}
		}
		serialPortsChoice.setSelectedIndex(-1);
		parallelPortsChoice.setSelectedIndex(-1);