Go Down

Topic: Help with Java GUI. (Read 1 time) previous topic - next topic


I finally got RXTX working.  Now I'm looking for a way to get output from and Arduino Uno streaming text into a TextArea rather than console.
I've looked here: http://docs.oracle.com/javase/tutorial/uiswing/index.html   but I can not find an example that could output:
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));  into a GUI text area.

What wold be nice if I could use a button to generate a text stream from the RXTX code from the Arduino.
Any hint would be appreciated:

Code: [Select]
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;

public class SerialTest implements SerialPortEventListener {
SerialPort serialPort;
        /** The port we're normally going to use. */
private static final String PORT_NAMES[] = { "/dev/ttyUSB0"};
* A BufferedReader which will be fed by a InputStreamReader
* converting the bytes into characters
* making the displayed results codepage independent
private BufferedReader input;
/** The output stream to the port */
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;

public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

//First, Find an instance of serial port as set in PORT_NAMES.
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
if (portId == null) {
System.out.println("Could not find COM port.");

try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),

// set port parameters

// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();

// add event listeners
} catch (Exception e) {

* This should be called when you stop using the port.
* This will prevent port locking on platforms like Linux.
public synchronized void close() {
if (serialPort != null) {

* Handle an event on the serial port. Read the data and print it.
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String inputLine=input.readLine();
} catch (Exception e) {
// Ignore all the other eventTypes, but you should consider the other ones.

public static void main(String[] args) throws Exception {
SerialTest main = new SerialTest();
Thread t=new Thread() {
public void run() {
//the following line will keep this app alive for 1000 seconds,
//waiting for events to occur and responding to them (printing incoming messages to console).
try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
cwc 1885


Read Strings from your 'input' Reader and append( ... ) the String to your JTextArea. Take care that the document doesn't become too large.

kind regards,



If you haven't looked at it already, there is an example program called Bluetooth Chat. You don't need the bluetooth function, just the layout. This program uses bluetooth serial to send and receive messages between two devices, including an Arduino with a serial.read/writen message.

Give it a try.
My GitHub:


Mar 15, 2013, 08:57 pm Last Edit: Mar 15, 2013, 08:58 pm by mistergreen Reason: 1
Processing which is built on top of processing, has a read serial function and has built in GUI for text and graphics. You can port that into your java app.

Go Up