Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8122708

FX 8 Tree/TableCell.setAlignment(..) doesn't change the default state.

    XMLWordPrintable

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;
              }
          }
      }

      Attachments

        Issue Links

          Activity

            People

              jgiles Jonathan Giles
              alammersdjfx August Lammersdorf (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported: