Create Raspberry Pi SWT GUI Applications with Java and Eclipse

This tutorial describes how to create Raspberry Pi SWT applications in Java which are based on the Standard Widget Toolkit (SWT).

This allows the development of portable applications including Graphic User Interfaces (GUI) running under Windows, Mac, Linux and also the Raspberry Pi.

It is not described here how to create SWT applications in general, but I will concentrate on an existing project which is adapted in order to work with the Raspberry Pi.

USB Analog and Digital IO Modules

LucidControl USB IO Modules

  • Data acquisition and control
  • Cost effective and flexible
  • Platform independent
  • Windows® and Linux
  • Compatible with RPi and Beagle Bone
  • Clippable on DIN-Rail
Read More ...
  • Industrial & home automation
Secure IoT Controller

LucidIoT Secure IoT Network Controller

  • 16 Analog and Digital IO Channels
  • HTTPS, Modbus/TCP, MQTT, FTP
  • SSL/TLS secured
  • Data Logging Function
  • Compatible with LucidControl Software Tools
  • DIN-Rail Enclosure
Read More ...
  • Multi-Protocol
USB Digital Input Module

USB Digital Input Module

LucidControl Product Series

  • 4 / 8 digital inputs
  • For 5V, 10V and 24V signals
  • Opto insulated contacts
  • Counters and edge detectors
Read More ...
USB Digital Output Module

USB Digital Output Module

LucidControl Product Series

  • Opto insulated option
  • 4 / 8 digital outputs
  • Relay module option
  • Pulse width modulation and timers
Read More ...
  • Switching and power control applications
USB Analog Input Module

USB Analog Input Module

LucidControl Product Series

  • Measurement range options
  • 4 analog inputs
  • e.g. 0-10 V or 0-20mA
  • 14 bit resolution
Read More ...
  • Acquisition of sensor signals
USB Analog Output Module

USB Analog Output Module

LucidControl Product Series

  • 4 analog outputs
  • Output range options
  • e.g. 0 - 10 V or 4 - 20 mA
  • 12 bit resolution
Read More ...
  • 4 - 20 mA current interfaces
USB RTD Input Module for Pt100 / Pt1000 with 8 Channels

USB RTD Input Module

LucidControl Product Series

  • 4/8 Pt1000/Pt100 RTD inputs
  • Temperature range: -180 ~ 180 °C or 0 ~ 360°C
  • 0.1 °C resolution
  • Heat control applications
Read More ...
  • Logging of temperatures

[one_third first]

Plug & Play USB IO

[dropcap3]1[/dropcap3]
Turn your computer with LucidControl Plug & Play IO Modules within minutes to a powerful data acquisition, monitoring and control system.
[/one_third]

[one_third]

Data Acquisition & Control

[dropcap3]2[/dropcap3]
Ready for many automation applications. Measure temperatures, control your home, garden or caravan with the outstanding LucidControl IO devices.
[/one_third]

[one_third]

RPi & Beagle Bone

[dropcap3]3[/dropcap3]
LucidControl is compatible with the Raspberry Pi® and Beagle Bone Black which maximizes the number of possible applications.
[button link=”https://www.lucid-control.com/lucidcontrol-usb-io-module-web-demonstration” style=”info” text=”grey” window=”yes”]Read more about …[/button]
[/one_third]


[hr]

Our LucidControl USB IO Devices work in combination with the Raspberry Pi and also the Beaglebone Black (and with a standard PC, of course). By using the provided Java or Python API it is possible to control and monitor analog and digital signals.

LucidControl Temperature Monitoring

LucidControl Temperature Monitoring

There are many applications in the industrial or home automation area where a Raspberry Pi can be used to control lamps, fans or valves, or where temperatures or voltages have to be monitored.

Having a GUI for these application is very beneficial and a big advantage compared to the command line tool because clicking a button is much simpler than entering commands in the shell.

Feel free to check out our article about monitoring 4 Pt1000 temperature sensors with the Raspberry Pi by using the swt-xy-graph diagram tool and find out how easy it is to build your own temperature logging and monitoring!

[hr]

What is necessary for a GUI Application?

  • Since development on the Raspberry Pi is somehow slow, the application is developed on a PC running Eclipse IDE. I’m using Eclipse Juno 64 bit running on Windows 7. When you read this article you should be familiar with Eclipse.
  • The Java JDK must be installed on the development machine. Please ensure to use a 32 bit version of the JDK for this SWT project. The 64 and 32 bit JDK versions can be installed in parallel. Here Java 1.7.0 is used.
  • You need Raspbian on your Raspberry Pi. I’m using #474, which is currently the most recent which can be pulled by rpi-update, but it should work with most versions.
  • The Standard Widget Toolkit should be installed on the development computer.
  • And of course a Java Virtual Machine is necessary on the Raspberry Pi. Here you should be very careful because the selection of the correct JVM is crucial!

Why is the JVM installation important?

The problem with most JVM implementations is that there is no „native“ JIT support of the ARM hard float point unit and this function is emulated by the JVM.
Oracle Java 8, which is available as Early Access version, is currently the best choice and it is a real boost compared to all other implementations like OpenJDK, Cacao or ZeroVM.
Since the advantage can be up to factor 10 don’t think about using another JVM, otherwise you would be disappointed.

Check this impressive benchmark comparing different Java installations on the Raspberry Pi.

Very comprehensive results of the Raspberry Pi from the Roy Longbottom’s PC Benchmark Collection. Please see the Java Whetstone Benchmarks and the JavaDraw Benchmark.

Here you can find an interesting benchmark article about Java 8 on the Raspberry Pi.

Here you can find some installation instructions for Java 8 on the Raspberry Pi.

[hr]

Preparation of the Raspberry Pi

If these prerequisites are fulfilled we can start setting up the Raspberry and install the necessary libraries.
Load and install the SWT libraries

sudo apt-get install libswt-gtk-3-java

It will also copy swt-gtk-3.8.0.jar to the folder /usr/share/java of you system. You should copy this file e.g. via FTP to you development computer since you will need this library in Eclipse later.

That’s all on the Pi so far.

[hr]

Setup of the Eclipse Project

Eclipse Project Explorer

Eclipse Project Explorer

To make it short, it is not necessary to change a single line in your program. The adaptions made here affect only the creation of the JAR file and I’m sure the same result can be achieved by different ways.

The picture shows the Eclipse Project Explorer of the LucidControl Digital Output USB Device Demonstration. The lucidio.example.do4.basics project includes three libraries:

  • lucidio.jar – for LucidControl specific IO device functions. This library gives full access over LucidControl USB IO Devices.
  • jssc.jar – the Java Simple Serial Connector library. Please see my post where I compare the different Java Serial Port Libraries and why I went for jssc.
  • swt.jar – The Standard Widgets Tools library

Now it is only necessary to replace the swt.jar (here the 32 Bit version for Windows is used) by the swt-gtk-3.8.0.jar which you copied from the Raspberry Pi.

Eclipse remove SWT Library

Eclipse remove SWT Library

Open the Project Properties and go to Java Build Path -> Libraries Tab.
Select the swt.jar and click onto “Remove”.


Eclipse add External SWT Library

Eclipse add External SWT Library

Now the new SWT library must be added. Click onto “Add External JARs…” and browse to the swt-gtk-3.8.0.jar location.


Eclipse Project Export

Eclipse Project Export

Export the Runnable JAR File, copy it to the Raspberry Pi and start it.


Note:
Of course, it is not possible to start the application on your development machine as long as the swt-gtk-3.8.0.jar library is included. This is only done in order to make a deployable JAR and should be reverted afterwards.

[hr]

Remarks

The older Raspberry Pi version with 256 MB RAM is limited useful for Java GUI applications. If you only run a simple application (like the example) it might work. But as soon as you have Apache Tomcat or other memory consuming services running in the background, the system would slow down and it would take a minute or more to start an application.
If the Raspberry Pi has enough resources (like 512 MB RAM) the application starts in approx. 5 seconds and the usability is very good also.

[hr]

Further Reading

LucidControl Remote Control

LucidControl Remote Control

Feel free to see the Raspberry Pi Live Demonstration where you can control the lightning in our office by your web browser. Find out how powerful a Raspberry Pi is and watch the live video streaming.

If this article was helpful for you or if you found a mistake in my writings, feel free to leave a comment.

How to access Serial Ports in Java

USB Analog and Digital IO Modules

LucidControl USB IO Modules

  • Data acquisition and control
  • Cost effective and flexible
  • Platform independent
  • Windows® and Linux
  • Compatible with RPi and Beagle Bone
  • Clippable on DIN-Rail
Read More ...
  • Industrial & home automation
Secure IoT Controller

LucidIoT Secure IoT Network Controller

  • 16 Analog and Digital IO Channels
  • HTTPS, Modbus/TCP, MQTT, FTP
  • SSL/TLS secured
  • Data Logging Function
  • Compatible with LucidControl Software Tools
  • DIN-Rail Enclosure
Read More ...
  • Multi-Protocol
USB Digital Input Module

USB Digital Input Module

LucidControl Product Series

  • 4 / 8 digital inputs
  • For 5V, 10V and 24V signals
  • Opto insulated contacts
  • Counters and edge detectors
Read More ...
USB Digital Output Module

USB Digital Output Module

LucidControl Product Series

  • Opto insulated option
  • 4 / 8 digital outputs
  • Relay module option
  • Pulse width modulation and timers
Read More ...
  • Switching and power control applications
USB Analog Input Module

USB Analog Input Module

LucidControl Product Series

  • Measurement range options
  • 4 analog inputs
  • e.g. 0-10 V or 0-20mA
  • 14 bit resolution
Read More ...
  • Acquisition of sensor signals
USB Analog Output Module

USB Analog Output Module

LucidControl Product Series

  • 4 analog outputs
  • Output range options
  • e.g. 0 - 10 V or 4 - 20 mA
  • 12 bit resolution
Read More ...
  • 4 - 20 mA current interfaces
USB RTD Input Module for Pt100 / Pt1000 with 8 Channels

USB RTD Input Module

LucidControl Product Series

  • 4/8 Pt1000/Pt100 RTD inputs
  • Temperature range: -180 ~ 180 °C or 0 ~ 360°C
  • 0.1 °C resolution
  • Heat control applications
Read More ...
  • Logging of temperatures

[one_third first]

Plug & Play USB IO

[dropcap3]1[/dropcap3]
Turn your computer with LucidControl Plug & Play IO Modules within minutes to a powerful data acquisition, monitoring and control system.
[/one_third]

[one_third]

Data Acquisition & Control

[dropcap3]2[/dropcap3]
Ready for many automation applications. Measure temperatures, control your home, garden or caravan with the outstanding LucidControl IO devices.
[/one_third]

[one_third]

RPi & Beagle Bone

[dropcap3]3[/dropcap3]
LucidControl is compatible with the Raspberry Pi® and Beagle Bone Black which maximizes the number of possible applications.
[button link=”https://www.lucid-control.com/lucidcontrol-usb-io-module-web-demonstration” style=”info” text=”grey” window=”yes”]Read more about …[/button]
[/one_third]


LucidControl goes JAVA

LucidControl goes JAVA

I was a bit of surprised a few weeks ago that Java has only a very limited native access to the serial comports.

We use the serial ports to communicate with our USB IO Modules. These modules have analog and digital IO functionality and they are compatible with USB CDC (Communication Device Class) which means that most operating systems provide a driver which installs a virtual comport.

Microsoft Windows enumerates them as a COMxx device. Linux operating systems give the devices usually names like /dev/ttyACMxx where xx is the identifier number of the port.

I’m familiar with C# which provides the SerialPort class. This is supported by dotNet and also by Mono and runs under Linux as well. I used this class several times for portable applications running mostly under Windows and Linux. Because of this I expected easy access to the serial port under Java as well and I didn’t waste much thought on doing such a common task in Java. But in reality Java does not support them – at least under Microsoft Windows.

In the following I will explain which options are available in order to access serial ports in Java.

CommAPI – Limited native support

The Java Communications API package javax.comm provides an interface to access serial ports. This sounds very good, but has one significant drawback because support for Microsoft Windows was dropped years ago and it is not possible to write portable applications anymore.
Because of this severe restriction I dropped this solution without further investigation.

RXTX

RXTX is the first option which comes one in mind when talking about alternatives of CommAPI. It is a comprehensive and reliable interface which is used in many projects. I used this in our pre-release revisions of JLucidIo Library. JLucidIo is our JAVA API which implements classes encapsulating the functionality of our LucidControl USB IO Modules.

I thought this was a good option because it is relatively easy to install and it is working reliably. So why didn’t we go for it? This has mainly three reasons:

  1. RXTX is not pure java. The hardware dependent part is written in a different language.

    • For Windows the hardware dependent layer is called rxtxSerial.dll and you can see that this part is not Java code. Maybe it was written in C, but I didn’t research this further.
    • This makes it necessary to install different software on different computers which is not what a Java programmer prefers.
  2. I find the API of RXTX needs getting used to. It is different to the SerialPort class in C# and the implementation has one severe drawback.

    • In C a port can be opened by using fopen(comportName) and the comport must be handy when opening the device which is the normal way to do it.
    • RXTX uses a different approach. Before a port can be opened ALL ports must be enumerated and a list of all serial ports is created. This is done by calling CommPortIdentifier.getPortIdentifiers() which isn’t the best idea in my eyes. Enumerating all serial ports by default is pointless when you only intend to open one specific port. And the implementation in RXTX is done a resource consuming way because it makes the operating system to create this list by querying all ports. This may take a long time especially when Bluetooth is involved. The operating system may ask all (Bluetooth) devices for their ability which can take several seconds per port! Have you counted the number Bluetooth ports of your computer? I have to admit that this behavior was only reported for Microsoft Windows, but in my case it took approx. 5 seconds to open a comport. For my tool I couldn’t accept this.
  3. Last but not least the development of RXTX seems to be neglected since 2009.

After all these findings I continued my search and found jSSC eventually.

jSSC (Java Simple Serial Connector)

The jSSC Java Simple Serial Connector is available in version 2.6.0 which is dated on June 2013. It consists only of one JAR file which has to be included to your project.

Like RXTX jSSC has also a OS dependent part but all is included in a single JAR and you do not have to care about the system you want to use your software.

jSSC provides a very simple API. In my case it took less than 30 minutes the replace RXTX by jSSC and it worked out of the box under Microsoft Windows, Ubuntu and on the Raspberry Pi.

There is no more to write because it does exactly what it should – not more not less 😉

Conclusion

I cannot say much about CommAPI but RXTX and jSSC are worth a look. RXTX is a good approach but because of the reasons I explained it was not the right library for our project and we went for jSSC.