Details
Description
TableCell.setAlignment(..) and TreeTableCell.setAlignment(..) don't change the default state.
Neither
TableCell.setStyle("-fx-alignment: center-right;");
nor
.table-cell {
-fx-alignment: center;
}
work.
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableViewApp extends Application {
public Parent createContent() {
final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Jacob", "Smith"),
new Person("Isabella", "Johnson"),
new Person("Ethan", "Williams"),
new Person("Emma", "Jones"),
new Person("Michael", "Brown")
);
TableColumn<Person, String> firstNameCol = new TableColumn<>();
firstNameCol.setText("First");
firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
firstNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
public TableCell<Person, String> call(TableColumn<Person, String> p) {
return new TableCell<Person, String>() {
{
this.setBackground(new Background(new BackgroundFill(Color.YELLOW, null, null)));
this.setAlignment(Pos.CENTER); // TODO doesn't work !!
}
@Override protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
this.setText("");
}
else {
this.setText(item);
}
}
};
}
});
TableColumn<Person, String> lastNameCol = new TableColumn<>();
lastNameCol.setText("Last");
lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
lastNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
public TableCell<Person, String> call(TableColumn<Person, String> p) {
return new TableCell<Person, String>() {
{
this.setBackground(new Background(new BackgroundFill(Color.ORANGE, null, null)));
this.setAlignment(Pos.CENTER_RIGHT); // TODO doesn't work !!
}
@Override protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
this.setText("");
}
else {
this.setText(item);
System.out.println("Alignment = " + this.getAlignment());
}
}
};
}
});
TableView<Person> tableView = new TableView<>();
tableView.setItems(data);
tableView.getColumns().addAll(firstNameCol, lastNameCol);
return tableView;
}
@Override public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(createContent()));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public static final class Person {
private StringProperty firstName;
private StringProperty lastName;
public Person(String fName, String lName) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public StringProperty lastNameProperty() {
return lastName;
}
}
}
Neither
TableCell.setStyle("-fx-alignment: center-right;");
nor
.table-cell {
-fx-alignment: center;
}
work.
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableViewApp extends Application {
public Parent createContent() {
final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Jacob", "Smith"),
new Person("Isabella", "Johnson"),
new Person("Ethan", "Williams"),
new Person("Emma", "Jones"),
new Person("Michael", "Brown")
);
TableColumn<Person, String> firstNameCol = new TableColumn<>();
firstNameCol.setText("First");
firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
firstNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
public TableCell<Person, String> call(TableColumn<Person, String> p) {
return new TableCell<Person, String>() {
{
this.setBackground(new Background(new BackgroundFill(Color.YELLOW, null, null)));
this.setAlignment(Pos.CENTER); // TODO doesn't work !!
}
@Override protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
this.setText("");
}
else {
this.setText(item);
}
}
};
}
});
TableColumn<Person, String> lastNameCol = new TableColumn<>();
lastNameCol.setText("Last");
lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
lastNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
public TableCell<Person, String> call(TableColumn<Person, String> p) {
return new TableCell<Person, String>() {
{
this.setBackground(new Background(new BackgroundFill(Color.ORANGE, null, null)));
this.setAlignment(Pos.CENTER_RIGHT); // TODO doesn't work !!
}
@Override protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
this.setText("");
}
else {
this.setText(item);
System.out.println("Alignment = " + this.getAlignment());
}
}
};
}
});
TableView<Person> tableView = new TableView<>();
tableView.setItems(data);
tableView.getColumns().addAll(firstNameCol, lastNameCol);
return tableView;
}
@Override public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(createContent()));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public static final class Person {
private StringProperty firstName;
private StringProperty lastName;
public Person(String fName, String lName) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public StringProperty lastNameProperty() {
return lastName;
}
}
}
Attachments
Issue Links
- relates to
-
JDK-8096083 TableView -fx-alignment: center-left in TableCell doesn't work
-
- Resolved
-