JavaFX TableView Explanation

JavaFX TableViews are for editing and displaying data. Think of the TableView as a very raw spreadsheet for JavaFX. If you are familiar with the TableView from Swing or another windowing toolkit, then you’re well on your way to understanding the JavaFX TableView.

If you are looking for a tutorial with JavaFX TableView code, check out my Simple JavaFX TableView Example. This post has more to do with explaining how basic TableViews fit together. Think of it as a companion to the Simple JavaFX TableView Example. I wrote this post, because just looking at a JavaFX TableView example is not nearly enough for the learning programmer to understand everything going on. JavaFX TableViews have a steep learning curve to put it mildly. Don’t feel bad if you’re having a hard time wrapping your mind around the TableView in JavaFX. It takes a little getting use to.

In JavaFX, all tables are enclosed in TableViews objects. If you’ve done a little JavaFX already, you could think of JavaFX TableViews as scenes for holding TableColumns. JavaFX TableColumns in turn are associated with specialized data objects called ObservableLists.

JavaFX ObservableLists contain the actual data that you display and manipulate. This is an important concept to remember. JavaFX TableViews and TableColumns do NOT contain data. When you want to add, edit, and delete data from a table in JavaFX, you are actually doing it to a specialized data container called an ObservableList.

A JavaFX ObservableList uses data defined in specialized Plain Old Java Objects (POJOs). The type of POJO used by an ObservableList is defined using generics. For example, if the POJO containing a row of data was named Account, then the ObservableList would be defined as an ObservableList<Account>.

The specialized POJOs contain properties defined in classes such as SimpleStringProperty, SimpleIntegerProperty, SimpleFloatProperty, SimpleDoubleProperty and SimpleBooleanProperty.

Simple right?

You’ll find those simple properties all implement<T>. Note that we do not use primitives (int, double, boolean) or even their boxed counterparts (Integer, Double, Boolean) to contain the data that is ultimately displayed in the JavaFX TableView.

Okay. That’s tons of information all at once. Learning all that at once is kinda like taking a drink from a firehose. So to sum up what I’ve covered so far …

  • A JavaFX scene or group holds a controller called a TableView
  • TableViews are like raw unimplemented spreadsheets for JavaFX
  • A JavaFX TableView holds JavaFX TableColumns
  • JavaFX TableViews do NOT contain data temselves
  • JavaFX TableColumns do NOT contain data themselves
  • JavaFX TableColumns are associated with properties from a POJO
  • A specialized list called an ObservableList contains a list of POJOs
  • Each POJO in the ObservableList represents one row of data in the JavaFX TableView

Just a note, … remember not to confuse JavaFX UI elements with their Swing or AWT counterparts. Swing and AWT are older Java UI APIs, and really shouldn’t be used in a Java application that uses JavaFX UI elements. You might gain insights into JavaFX leaf nodes by reading about their Swing or AWT counterparts, but don’t expect everything from Swing and AWT to have a direct correlation in JavaFX.

Now let’s discuss how all the pieces of JavaFX TableView are held together. Specifically, let’s look a little closer at how JavaFX TableViews and TableColumns know what data to display.

One of the keys to associating the POJOs with columns and rows from the displayed data is the PropertyValueFactory. JavaFX’s PropertyValueFactory’s are constructed using the POJO’s class name, the a value type that describes a private attribute contained in the POJO, and the name of the private attribute. Without getting bogged down in unnecessary implementation details, the PropertyValueFactory performs all kinds of cool magic setting up a description of each of the POJO’s encapsulated attributes. One PropertyValueFactory is constructed for each of the POJO’s attributes.

After all the PropertyValueFactory objects are constructed and associated with the POJO’s attributes, then the PropertyValueFactory objects are passed to the TableView’s TableColumns. This process tells each table column which of the POJO’s attributes it is responsible for displaying, and what type of value the TableColumn is responsible for displaying.

Finally, the JavaFX TableView needs to know what list of POJOs it is displaying. This is done by taking the ObservableList that represents rows of data, and passing it to the TableView with the setItems() method.

One last explanation. TableViews and TableColumns don’t contain the data they display. This is tricky for the beginner. Why don’t they contain the data, is a common question. JavaFX is all about separation of concerns using MVC and MVP type design patterns. UI and data are very separate concerns, so the data and display elements are kept separate with TableViews. This way one chunk of code focuses on displaying pretty data, and the other chunk of code focuses on holding and manipulating the data. This is very good Object Oriented design.

That’s it. This post should help explain a little bit about the code you’ll see in basic JavaFX TableView examples whether on this site or another site. If you want to see the other JavaFX and TableView tutorials and explanations on this site, be sure to read glance at the following pages. JavaFX, JavaFX 8, and JavaFX TableView.