JavaFX and Couchbase Mobile is a great combo for creating desktop class applications with rich user interfaces and fast data access. In this tutorial, you will learn the following core concepts:

  • Learn about replication, security and deployment using the TodoLite sample
  • How to start a new JavaFX project with Couchbase Mobile as a dependency

We won’t cover TodoLite for Android in this post but you could have both applications running side by side and replicating documents and attachments via Sync Gateway:

You can find the source code of TodoLite JavaFX on github.

TodoLite JavaFX

To speed things up, you will clone some source code with the following:

Open this folder in IntelliJ and click the 실행 button in the upper right corner. A new window should open (of course your’s won’t have any data in it :D):

Go ahead and create a new List and Task. At the time of this writing, the JavaFX version of TodoLite doesn’t implement all of the features. Right now, you’re logged in as the user with name wayne and the password is pass. So if you run the TodoLite Android version, you can login with those credentials or create a new user on the Sign Up page and share Lists with other Users. Both applications would be syncing to a Sync Gateway instance running at http://9cec7a6e-jamiltz.node.tutum.io:4984.

데이터 모델

TodoLite has the following data model. Notice how documents are kept small in size and can reference other documents’ _id field (you could think of them as foreign keys):

배포

The back-end for TodoLite is composed of Sync Gateway and an App Server to allow users to Sign Up. Again, at the time of this writing, the Login and Sign Up screens have not been implemented for the JavaFX version of TodoLite. You can follow the README in the todolite-development repository to get both servers running locally.

How-To Creating a new JavaFX + Couchbase Mobile application

Open IntelliJ IDEA and choose the Create New Project menu. On the left pane, select the JavaFX application template and set the Project SDK to 1.8. Name the application as you wish and click 완료, this will open your newly created project in a new window. Before we begin writing code, there are a couple of configuration settings to change. Select the Edit Configurations… menu in the top right corner which will open a new window. Check the Single instance only box to ensure that the IDE doesn’t start a new instance of the application every time you click the Run button. With that, let’s turn our attention to adding Couchbase Lite as a dependency to the project. Select File\Project Structure… from the top menu bar, a new window will open and on the Modules tab, add a new Library from Maven: A search field will appear in a popup window, type com.couchbase.lite:couchbase-lite-java:1.1.0 를 클릭하고 확인. This will download the library and add it to the project. Next, you will add a JAR file that contains the native library for the platform you’re running the application on (Windows, Mac…). For OS X users, download this JAR file and add it in a new directory called libraries in your project. Return the Modules window and add this JAR file from the Add > JARs or directories… menu: 클릭 확인 and run the application. You should see a blank window with the message 헬로 월드 in the status bar:

Open a new Database and save a Document

In your new project, open Main.java and create a new method called startCouchbase 를 다음과 같이 설정합니다:

Here, you’re opening a new database called myapp and persisting a new document with a session and conference field. You can read more about the different APIs in the documentation guides or refer to the TodoLite-JavaFX sample app.

전화로 startCouchbase 메서드의 시작 method of Main.java

동기화 게이트웨이

The quickiest way to get started with replication is to download the latest version of Sync Gateway:

http://www.couchbase.com/nosql-databases/downloads#Couchbase_Mobile

And use one of the provided configuration template as the command line argument when starting Sync Gateway:

The specified configuration file has the following:

Here, you’re creating a database called db and enable the 게스트 which means that unauthenticated requests should be allowed and processed.

That’s it! You now have a Sync Gateway database running on your machine that can be reached at http://localhost:4984/db/.

Adding Sync

Head back to the JavaFX application and add a new 시작복제 메서드를 사용합니다:

전화로 시작복제 below the startCouchbase method and restart the application. You should now see the document you created previously in the Sync Gateway Admin UI (http://localhost:4985/_admin/).

다음 단계로 이동

Congratulations! You’ve built your first JavaFX + Couchbase Mobile application with replication. You’re now ready to learn the following concepts:

아래 댓글이나 포럼에서 여러분의 의견과 결과를 공유하거나 궁금한 점이 있으면 언제든지 질문해 주세요. 곧 연락드리겠습니다!

작성자

게시자 제임스 노센티니, 모바일 테크니컬 라이터, Couchbase

제임스 노센티니는 카우치베이스 모바일의 문서를 담당하는 테크니컬 라이터입니다. 이전에는 개발자 지원 담당자로 일했으며 그 전에는 HouseTrip의 프론트엔드 개발자로 일했습니다. 또한 여가 시간에는 raywenderlich.com에 안드로이드 튜토리얼을 작성하는 것을 즐깁니다.

댓글 남기기