I’ve spent quite a bit of time dabbling with Couchbase and web applications using the awesome Java SDK, but did you know you can create desktop applications that use Couchbase to store data as well? Java desktop frameworks like JavaFX can make use of the Couchbase Java SDK, but it is probably not a good idea to use an SDK intended for server in your client facing application. Instead, you can actually use Couchbase’s mobile solution to build client facing desktop applications. It uses the same APIs as Android, but was designed with desktops in mind.

We’re going to take a look at building a simple desktop application using JavaFX, Couchbase Lite, and even Couchbase Sync Gateway to synchronize this data between computers.

The Requirements

There are a few requirements that must be met in order to make this project successful.

  • JDK 1.7+
  • Maven
  • Couchbase Sync Gateway

This project will use Maven to gather our dependencies and build a JAR file. While Sync Gateway isn’t truly a requirement, it is if you wish to add synchronization support to your application.

Creating a Fresh JavaFX Project with Maven

We need to create a basic Maven project. This can be done in an IDE of your choice, or it can be done manually. Essentially what we’ll need is the following file and directory structure:

We’ll get into the specifics of what each file contains when we start developing. For now we need to set up our Maven pom.xml file so it will obtain the necessary dependencies.

Open the project’s pom.xml file and include the following:

Without going into too much unnecessary detail in the above Maven file, we want to pay attention to a few things in particular.

The above dependency says we are including Couchbase Lite into our project. Remember, Couchbase Lite is a local database that does not sit on a server somewhere. The data is stored locally and the component is bundled within your application.

We also want to pay attention to the following plugin:

The above plugin is for creating a JavaFX project. Of course this project creation is a lot easier when using an IDE like IntelliJ, even though it isn’t required.

Creating the Couchbase Singleton Class

Before we get invested in creating the UI and controllers for our JavaFX project, let’s worry about how data is going to be handled.

For simplicity, it is a great idea to create a singleton class for managing the data throughout our project. It also works quite well when setting up data listeners to prevent having to write queries everywhere in the application. Let’s go ahead and open the project’s src/main/java/com/couchbase/CouchbaseSingleton.java file and include the following code. We’ll break it down after.

The above was a lot to take in, but it was necessary in order to avoid confusion.

Inside the CouchbaseSingleton class there are four private variables. The database manager will allow us to open our database as well as create it. The replication objects are responsible for synchronization in either direction.

In the constructor method we create and open a database called fx-project and configure a view that we’ll use when it comes to querying for data. This view called todos will emit a key-value pair of document id and document for every document that is stored in the local database. The constructor method is private because we instantiate it through the static method getInstance.

While we won’t be looking at synchronization until later in the guide, it is a good idea to lay the foundation. Essentially we just want to define that we’ll have continuous replication to and from a particular sync gateway URL. We also want to be able to stop replication when the application closes. This brings us to our methods for saving and loading data.

Our save method will take a Todo object and save it into the database. The result of which will be a Todo object that contains the document id that gets returned to the calling method. This Todo class is simple. It accepts basic information like id, title, and description, and has the appropriate getter and setter methods that match. For reference, it looks like the following and exists in the project’s src/main/java/com/couchbase/Todo.java file.

This leaves us with our last data related function, the query function.

Remember that view that we created? This time we’re querying it. The result set will be loaded into an array of Todo objects. This brings our data layer to a close, allowing us to focus on the actual application development.

Designing the Desktop Application

While not required, the JavaFX application, SceneBuilder, makes it very simple to create a graphical UI and corresponding controller class. If you choose not to use it, open your project’s src/main/resources/TodoFX.fxml and include the following XML markup:

Author

Posted by Nic Raboy, Developer Advocate, Couchbase

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

Leave a reply