Published in


Experimenting with Java enums, emojis, and ComboBox in JavaFX

Learn how to use Java enums and emojis with a JavaFX ComboBox.

ToDoCategory Java enum with emojis

Upgrading my JavaFX ToDoList application

In the second iteration of my Java TodoListApplication, I used the JavaFX DatePicker and TableView components. I also introduced a Java record named ToDoItem to see how I could use records in a JavaFX application.

I said in my previous blog that I was going to make my ToDoList persistent. Iโ€™m still planning on doing this (next blog!), but I realized I wanted to add one more piece of information first to my ToDoItem before making it persistent.

I wanted to add a category to my ToDoItem record to keep track of to do items by category. So I created a ToDoCategory enum with categories that match things that I do on a regular basis.

I added a getter for the emoji String named getEmoji. This will be important later.

It was a fairly straightforward change to update my ToDoItem record with the ToDoCategory. The important bit is that while the ToDoItem holds onto a reference to a ToDoCategory, it will return the emoji from the category in the getCategory method.

The next step I wanted to add a ComboBox to my todolist-view.fxml. I placed the todoCategory ComboBox between the todoItem TextField and todoDate DatePicker.

I bound the ComboBox to a variable named todoCategory and added it to my TodoListController.

Then I needed to write some code to populate the values in the ComboxBox with all of the ToDoCategory enum values. So I updated the initialize method of the TodoListController.

I wrapped the array returned from the values method of the ToDoCategory enum with an Eclipse Collections MutableList, and then wrapped that in an ObservableList using the FXCollections utility class.

The text that is displayed in the ComboBox is the return value for the toString implementation for each of the enums, which will be the enum text itself. This works as I had hoped.

When the add button is clicked, I now need to construct the ToDoItem instance with all three UI component values. This was straightforward to add the todoCategory ComboBox selected value.

Finally, I wanted to add the category column to the TableView. I decided it would be cool to display the emoji value for the ToDoCategory enum here.

I added the category column between the name and date columns and made it sortable.

My updated JavaFX TodoListApplication View

This is what my TodoListApplication looks like running now.

You may note that I have a category for reading, but not for writing. I may change this, but decided for this iteration, I would just categorize writing this blog under RELAX. Writing is a form of mental relaxation for me. I keep very focused, sort my thoughts, and write them down relieving my brain of having to hold onto too much information. Writing is my mental equivalent of Yoga.

My todolist-view.fxml & TodoListController Source

Out of convenience, I have put most of my code including the ToDoItem and ToDoCategory types in the TodoController class. I may change this in future iterations. The TodoListApplicaton code hasnโ€™t changed since my last blog, so please refer to it there if you want.



Next Steps

My next step will be to make my ToDo List persistent. Iโ€™ve used enough JavaFX components now and want to experience more of the lifecycle of a JavaFX application. I hope you enjoyed this brief detour of using Java enum, emojis and ComboBox in JavaFX. ๐Ÿ˜€

Thank you for reading!

I am the creator of and a Committer for the Eclipse Collections OSS project which is managed at the Eclipse Foundation. Eclipse Collections is open for contributions.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Donald Raab

Java Champion. Creator of the Eclipse Collections OSS Java library ( Inspired by Smalltalk. Opinions are my own.