TableView does not display the content of ObservableList objects

后端 未结 1 1136
一个人的身影
一个人的身影 2021-01-23 11:51

I have two almost similar custom classes for storing simple String data - they are called \"Patient\" and \"Trace\".They differ from eachother only by the number of defined fiel

相关标签:
1条回答
  • 2021-01-23 12:39

    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.
    0 讨论(0)
提交回复
热议问题