Javafx tableview not showing data in all columns

后端 未结 2 604
无人共我
无人共我 2020-11-21 05:41

OK, new to java by several weeks, but have been programming for 30 years. The following code executes, but only the first column is showing anything. The data object is sh

2条回答
  •  广开言路
    2020-11-21 06:01

    This question is really a duplicate of: Javafx PropertyValueFactory not populating Tableview, but I'll specifically address your specific case, so it's clear.

    Background

    PropertyValueFactory uses reflection to determine the methods to get and set data values as well as to retrieve bindable properties from your model class. The pattern followed is:

    PropertyValueType getName()
    void setName(PropertyValueType value)
    PropertyType nameProperty()
    

    Where "name" is the string specified in the PropertyValueFactory constructor. The first letter of the property name in the getter and setter is capitalized (by java bean naming convention).

    Why your application doesn't work

    You have these three expressions:

    new PropertyValueFactory("DateEntered")
    new PropertyValueFactory("cDesc")
    new PropertyValueFactory("CreatedBy")
    

    For your sample properties, the PropertyValueFactory will look for these methods:

    "DateEntered" => getDateEntered()
    "cDesc" => getCDesc()
    "CreatedBy" => getCreatedBy()
    

    And you have these three getters on your sresult class:

    getDateEntered()
    getcDesc()
    getEnteredBy()
    

    Only getDateEntered() is going to be picked up by the PropertyValueFactory because that is the only matching method defined in the sresult class.

    Advice

    You will have to adopt Java standards if you want the reflection in PropertyValueFactory to work (the alternative is to not use the PropertyValueFactory and instead write your own cell factories from scratch).

    Adopting Java camel case naming conventions also makes it easier for Java developers to read your code.

提交回复
热议问题