0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

FXML上のTableViewのCellValueFactoryをカスタマイズクラスで用意

Posted at

目的

FMXLを利用しながら、TableViewの各POJO行のうち、特定カラムの値生成ロジックをカスタマイズする。

fxml

<?import practice.fx.property.CustomPropertyFactory?>
...省略
<TableColumn text="customProperty">
    <cellValueFactory>
        <CustomPropertyFactory initialValue="test"/>
    </cellValueFactory>
</TableColumn>

CustomPropertyFactory

package practice.fx.property

import javafx.beans.NamedArg
import javafx.beans.property.SimpleStringProperty
import javafx.beans.value.ObservableValue
import javafx.scene.control.TableColumn.CellDataFeatures
import javafx.util.Callback

class CustomPropertyFactory<S, T>(
    @NamedArg("initialValue") val initialValue: String
) :
    Callback<TableColumn.CellDataFeatures<POJO, String>, ObservableValue<String>> {

    override fun call(param: TableColumn.CellDataFeatures<POJO, String>?): ObservableValue<String> {

        val customValue = UUID.randomUUID().toString() // custom point

        return SimpleObjectProperty(customValue)
    }
}

S,Tの型引数がないと、FXML上で、以下警告がIntelliJの場合は、発生する。

javafx.util.Callback に強制できません

型引数を使うと、実装が大変面倒なので、最上位の部分だけ、S,Tの型引数を用意する。
これをする理由は、FXMLファイル上で警告にさせないためだけを目的にしている。

シンプルな実装のPropertyValueFactoryでは、PropertyReferenceを用いて、プロパティの取り出しを行っているが、
PropertyReference上で、キャストしているため、この型引数はあまり意味がなさそうである。

結果

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?