3

私には大きな問題があります。

TableView の TableColumn のコンテンツを中央に配置しようとしています。

ネットで見つけたものはすべて試しましたが、実際には何も機能しませんでした。

同じ問題を抱えている/持っている人はいますか? 解決策はありますか?

助けてください!

編集:

さて、私はこのコードで静的セルのコンテンツを中央に配置することができました:

tc_customer.setCellFactory(new Callback<TableColumn<TvAccounting, String>, TableCell<TvAccounting, String>>() {
                @Override
                public TableCell<TvAccounting, String> call(TableColumn<TvAccounting, String> p) {
                    TableCell<TvAccounting, String> tc = new TableCell<TvAccounting, String>();
                    tc.setAlignment(Pos.CENTER);
                    tc.setText("SOMETEXT");
                    return tc;
                }
            });

しかし、コンテンツはデータベースから取得する必要があり、メソッドで使用する ObservableList オブジェクトからデータを取得する方法が本当にわかりませんTABLEVIEWNAME.setItems...

私は最初にこのコードを使用しました:

tc_customer.setCellValueFactory(new PropertyValueFactory<TvAccounting, String>("Customer"));

しかし、そのコンテンツを中央に配置する方法はありませんでした!

誰か助けてください。

編集:

この偉大な答えのおかげで、私はそれをやった!

以下のコード:

tc_customer.setCellFactory(new Callback<TableColumn<TvAccounting, String>, TableCell<TvAccounting, String>>() {
                @Override
                public TableCell<TvAccounting, String> call(TableColumn<TvAccounting, String> p) {
                    TableCell<TvAccounting, String> tc = new TableCell<TvAccounting, String>(){
                        @Override
                        public void updateItem(String item, boolean empty) {
                            if (item != null){
                                setText(item);
                            }
                        }
                    };
                    tc.setAlignment(Pos.CENTER);
                    return tc;
                }
            });

            tc_customer.setCellValueFactory(new PropertyValueFactory<TvAccounting, String>("Customer"));

最高の感謝!!!

4

1 に答える 1

5

CellValueFactory と CellFactory は 2 つの異なるものです。CellValueFactory は値の取得元を指定するために使用され、CellFactory は値の表示方法を指定します。

両方を同時に使用してください。しかし、setCellFactory コードではすべきではありませsetText。テキストの設定は、メソッドTableCell内のコードによって処理されます。updateItem()このメソッドは、' cellValueFactory ' から提供される値を使用し、それを独自のラベル内に設定します。

tc_customer.setCellFactory(
   new Callback< TableColumn<TvAccounting, String>,
                 TableCell<TvAccounting, String>>()
   {
      @Override public TableCell<TvAccounting, String>
      call(TableColumn<TvAccounting, String> p) {
         TableCell<TvAccounting, String> tc =
            new TableCell<TvAccounting, String>();
         tc.setAlignment(Pos.CENTER);
         // tc.setText("SOMETEXT"); This line should be removed
         return tc;
      }
   }
);
于 2013-09-13T11:52:42.493 に答える