javafx.scene.control.TableCell;
カスタム(再利用可能な)cellFactoriesをまたはに簡単に適用するにはどうすればよいjavafx.scene.control.TableView;
ですか?
4308 次
1 に答える
15
セル内に表示されるテキストやその色などをフォーマットするカスタムCellFactoriesを適用するには、次の例が役立つ場合があります。
初期状況
Bean / POJOがあると仮定しましょうPerson
:
public class Person {
private double levelOfGrowth = 0;
public Person() {};
public Person(double levelOfGrowth) {
this.levelOfGrowth = levelOfGrowth;
};
public double getLevelOfGrowth() {
return levelOfGrowth;
}
}
levelOfGrowth
人の成長がどれだけ完了したかのパーセント値はどこにありますか。
0.00から1.00の間に設定される場合があります。
また、コントローラーにバインドされているFXML内にビューを作成したと仮定しますMainWindowController
。levelOfGrowth
また、IDを表示する列を設定しますlevelOfGrowthColumn
public class MainWindowController implements Initializable {
@FXML
public TableColumn levelOfGrowthColumn;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person(0));
persons.add(new Person(0.5));
persons.add(new Person(1));
levelOfGrowthColumn.setCellValueFactory(new PropertyValueFactory<Person, Double>("levelOfGrowth"));
}
}
意図
上記の例の問題は、ビューに表示される値が50%ではなく0.5のようになっていることです。また、100%などの値の色を緑に変更したい場合もあります。
ソリューション
必要なのは、ビューでdouble値をフォーマット/印刷する方法と、そのクラスを。に接続する方法を説明する1つの(再利用可能な)クラスですlevelOfGrowthColumn
。
'フォーマッタ'クラス
public class PercantageFormatCell extends TableCell<Object, Double> {
public PercantageFormatCell() {
}
@Override
protected void updateItem(Double item, boolean empty) {
super.updateItem(item, empty);
// If the row is not empty but the Double-value is null,
// we will always display 0%
if (!empty && null == item) {
item = new Double(0.0d);
}
// Here we set the displayed text to anything we want without changing the
// real value behind it. We could also have used switch case or anything you
// like.
setText(item == null ? "" : NumberFormat.getPercentInstance().format(item));
// If the cell is selected, the text will always be white
// (so that it can be read against the blue background),
// if the value is 1 it will be green.
if (item != null) {
double value = item.doubleValue();
if (isFocused() || isSelected() || isPressed()) {
setTextFill(Color.WHITE);
} else if (value < 1) {
setTextFill(Color.BLACK);
} else {
setTextFill(Color.GREEN);
}
}
}
}
コントローラ内で使用してください
public class MainWindowController implements Initializable {
@FXML
public TableColumn levelOfGrowthColumn;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person(0));
persons.add(new Person(0.5));
persons.add(new Person(1));
// New code START
// In case we have multiple columns with percent-values it
// might come in handy to store our formatter
Callback<TableColumn, TableCell> percantageCellFactory =
new Callback<TableColumn, TableCell>() {
public TableCell call(TableColumn p) {
return new PercantageFormatCell();
}
};
// Now all we have to do is to apply it
levelOfGrowthColumn.setCellFactory(percantageCellFactory);
// New code END
levelOfGrowthColumn.setCellValueFactory(new PropertyValueFactory<Person, Double>("levelOfGrowth"));
}
}
于 2012-08-19T14:29:46.843 に答える