TableView<T> does not display the content of ObservableList<T> objects

心已入冬 提交于 2019-12-02 04:44:37

The problem is that the values you are passing to the PropertyValueFactory do not match the names of the properties, which are defined by the get...() and set...(...) methods.

Since you are using reflection to look up the names of the fields (not the properties) you have defined, for the Trace class the values passed to PropertyValueFactory are "action", "status", "message", and "time". So the table view will attempt to populate the values for the columns by calling getAction(), getStatus(), getMessage(), and getTime() on the Trace objects in each row. Since there are no such methods, you don't get any values displayed.

To fix this, you can do one of the following:

  1. Hard code the values defined in your getVariablesNames() method to return the names of the properties (i.e. return new String[] {"actionText", "statusText", "messageText", "timeText"};). Since you repeat the method in each class rather than reusing it, you don't make things more verbose, and this would potentially perform better (though you are unlikely to observe any differences in practice).
  2. Use reflection, but look up the names of the declared methods, find all those beginning "get" or "is", strip off that prefix, and lower-case the first character of what remains.
  3. Make the field names match the property names (i.e. declare the fields as actionText, etc.). This of course imposes convention requirements on your class definition.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!