Locked History Actions

Diff for "JavaFX/TextAlignmentInTableView"

Differences between revisions 4 and 5
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= TableView中のテキストの右詰め = この内容は以下に移動しました。
Line 3: Line 3:
TableView中のテキストを右詰め表示したいとき、検索すると以下のような例が見つかるが、これは全くうまくいかない。
テキストの長さがセル領域より大きいときには、右詰めにならない。

When you want texts in tableview being right aligned in the cell. you'll find the following solution on the web.
But it doesn't work properly, because when the text length overflows that area of the cell, it won't be right aligned.

{{{
    TableColumn<E, T> col = ...;
    col.setStyle("-fx-alignment: CENTER-RIGHT;");
}}}

正しい解決法は以下。

The correct solution is the following.

{{{
    TableColumn<E, String> col = ...;
    Callback<TableColumn<E, String>, TableCell<E, String>> cellFactory =
      new Callback<TableColumn<E, String>, TableCell<E, String>>() {
        @Override
        public TableCell<E, String> call(final TableColumn<E, String> param) {
          TableCell<E, String>cell = new TableCell<E, String>() {
            public void updateItem(String item, boolean empty) {
              super.updateItem(item, empty);
              if (empty) {
                setText(null);
                return;
              }
              this.setText(item);
            }
          };
          cell.setTextOverrun(OverrunStyle.LEADING_ELLIPSIS);
          cell.setTextAlignment(TextAlignment.RIGHT);
          return cell;
        }
      };
    col.setCellFactory(cellFactory);
}}}

Labelにも同じメソッドが存在するので、これらのメソッドを使ってLabelでも同じことができる。

Also Label(s) has setTextOverrun() and setTextAlignment() methods, so you can do the same thing on Label(s).
https://www.gwtcenter.com/align-right-in-textview

この内容は以下に移動しました。

https://www.gwtcenter.com/align-right-in-textview