23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

17.5.5.1 Step 1<br />

<strong>Java</strong> I/O<br />

As you would probably guess, you listen for parallel port events with a Parallel-<br />

PortEventListener:<br />

public interface ParallelPortEventListener extends EventListener<br />

This interface declares a single method, parallelEvent():<br />

public abstract void parallelEvent(ParallelPortEvent ppe)<br />

Inside this method, you generally use the getEventType() method of Parallel-PortEvent<br />

to determine exactly what caused the parallel port event:<br />

public int getEventType()<br />

This should return ParallelPortEvent.PAR_EV_BUFFER to signal an empty output buffer or<br />

ParallelPortEvent.PAR_EV_ERROR to signal some other sort of error.<br />

The getNewValue() and getOldValue() methods tell you what the state of the parallel port<br />

was before and after the event:<br />

public boolean getNewValue()<br />

public boolean getOldValue()<br />

17.5.5.2 Step 2<br />

Once you've constructed a ParallelPortEventListener, you need to pass it to the<br />

ParallelPort object's addEventListener() method:<br />

public abstract void addEventListener(ParallelPortEventListener listener)<br />

throws TooManyListenersException<br />

You are limited to one event listener per port. Attempting to add a second event listener<br />

throws a java.util.TooManyListenersException. If this is a problem, you can install a<br />

single intermediate event listener directly with the ParallelPort object, which keeps a list of<br />

ParallelPortEventListener objects and dispatches events it receives to the other event<br />

listeners.<br />

Should you need to, you can remove a listener from the port with the ParallelPort object's<br />

removeEventListener() method:<br />

public abstract void removeEventListener()<br />

This method takes no arguments, because there's never more than one event listener registered<br />

directly with the port.<br />

456

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!