2017-10-25 9 views
0

私はJavaFXのに新しいですし、私はJavaFXの中でテーブルビューを構築し、ここではサンプルコードです:TableColumnのsetCellFactory

TableView<Person> table = new TableView<>(); 
table.setEditable(true); 
final TableColumn<Person, String>nameCol = new TableColumn<>("Name"); 
nameCol.setCellValueFactory(new PropertyValueFactory<>("name")); 

は私がテーブルにリストを追加した後、すべてがうまく動作します。
しかし、私はNameCol後にこれらのコードを追加するとき:

nameCol.setCellFactory(param -> new XCell()); 

public class XCell extends TableCell<Person, String> { 
    @Override 
    protected void updateItem(String item, boolean empty) { 
     super.updateItem(item, empty); 
     setStyle(empty ? null : "-fx-font-weight: bold; -fx-alignment: center"); 
//... 
    } 
} 

を次に、nameColumnからのデータが行方不明になりました。 しかし、私はそのコードコメントをするとき:

//nameCol.setCellFactory(param -> new XCell()); 

をすべてのデータがもう一度戻って行きました。 それは私が間違っていることを見つけることができないように配線されています。

誰もが起こっているかを説明し、それを修正することができれば、私は感謝します。

答えて

0

問題は@OverrideTableCellupdateItemの方法で、セルに表示される内容とその方法が分かります。 TableCellを拡張する場合は、セル内にグラフィックまたはテキストを表示することに注意する必要があります。はい、

public class XCell extends TableCell<TestApp.Person, String> { 
    @Override 
    protected void updateItem(String item, boolean empty) { 
     super.updateItem(item, empty); 
     if(empty){ // check if the cell contains an item or not, if it not you want an empty cell without text. 
      setText(null); 
     }else { 
      setText(item); 
      setStyle("-fx-font-weight: bold; -fx-alignment: center"); // You can do the styling here. 
      // Any further operations to this cell can be done here in else, since here you have the data displayed. 
     } 
     // Since as I see you don't have any graphics in the cell(like TextField, ComboBox,...) you 
     // don't have to take care about the graphic, but only the displaying of the text. 
    } 
} 
+0

ああ:

ですから、このような何かを行う必要があります。私はちょうど私が各セルにテキストを設定するのを忘れていたことに気づいた。今それは動作します。ありがとう。 – Joe

関連する問題