The default JavaFX tableview example does not go very far with regards to showing you how to format cells that contain Integers (you typically want them to align right) or Decimal values (aligned right and formatted properly).
I'm posting this here as a reminder to myself, as well as to anyone that might have come across the same issue. This is just a code posting, explanations on how to use JavaFX and the rest of the code can be found in the JavaFX sample code provided by Oracle.
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.beans.property.StringProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;
public class TableSample extends Application {
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setScene(new Scene(root));
final ObservableList<Person> data = FXCollections.observableArrayList();
data.add(new Person("Jacobz", "Smithy", "jacobz.smith@example.com", 78, 19.99));
data.add(new Person("Jacob", "Smith", "jacob.smith@example.com", 23, 116.0));
data.add(new Person("Isabella", "Johnson", "isabella.johnson@example.com", 101, 20.1));
data.add(new Person("Ethan", "Williams", "ethan.williams@example.com", 43, 18.0));
data.add(new Person("Emma", "Jones", "emma.jones@example.com", 44, 23.88));
data.add(new Person("Michael", "Brown", "michael.brown@example.com", 56, 19.5));
TableColumn firstNameCol = new TableColumn();
firstNameCol.setText("First");
firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
TableColumn lastNameCol = new TableColumn();
lastNameCol.setText("Last");
lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
TableColumn emailCol = new TableColumn();
emailCol.setText("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(new PropertyValueFactory("email"));
TableColumn ageCol = new TableColumn();
ageCol.setText("Age");
ageCol.setMinWidth(100);
ageCol.setCellValueFactory(new PropertyValueFactory("age"));
TableColumn pricePaidCol = new TableColumn();
pricePaidCol.setText("PricePaid");
pricePaidCol.setMinWidth(100);
pricePaidCol.setCellValueFactory(new PropertyValueFactory("pricePaid"));
//Formatting decimal columns
pricePaidCol.setCellFactory(new Callback<TableColumn, TableCell>() {
public TableCell call(TableColumn p) {
TableCell cell = new TableCell<Person, Double>() {
@Override
public void updateItem(Double item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : getString());
setGraphic(null);
}
private String getString() {
String ret = "";
if (getItem() != null) {
String gi = getItem().toString();
NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(2);
df.setRoundingMode(RoundingMode.DOWN);
ret = df.format(Double.parseDouble(gi));
} else {
ret = "0.00";
}
return ret;
}
};
cell.setStyle("-fx-alignment: top-right;");
return cell;
}
});
//Formatting decimal columns end
//Formatting integer columns
ageCol.setCellFactory(new Callback<TableColumn, TableCell>() {
public TableCell call(TableColumn p) {
TableCell cell = new TableCell<Person, Integer>() {
@Override
public void updateItem(Integer item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : getString());
setGraphic(null);
}
private String getString() {
String ret = "";
if (getItem() != null) {
ret = getItem().toString();
} else {
ret = "0";
}
return ret;
}
};
cell.setStyle("-fx-alignment: top-right;");
return cell;
}
});
//Formatting integer columns end
TableView tableView = new TableView();
tableView.setItems(data);
tableView.getColumns().addAll(firstNameCol, lastNameCol, emailCol, ageCol, pricePaidCol);
root.getChildren().add(tableView);
}
public static class Person {
private StringProperty firstName;
private StringProperty lastName;
private StringProperty email;
private IntegerProperty age;
private DoubleProperty pricePaid;
private Person(String fName, String lName, String email, int age, Double pPaid) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
this.age = new SimpleIntegerProperty(age);
this.pricePaid = new SimpleDoubleProperty(pPaid);
}
public StringProperty firstNameProperty() {
return firstName;
}
public StringProperty lastNameProperty() {
return lastName;
}
public StringProperty emailProperty() {
return email;
}
public IntegerProperty ageProperty() {
return age;
}
public DoubleProperty pricePaidProperty() {
return pricePaid;
}
}
@Override
public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}