JavaFX TableView - Center the Content of a Table Column

downdrown picture downdrown · Sep 13, 2013 · Viewed 11.4k times · Source

I have a big Problem.

I try to center the Content of a TableColumn in a TableView.

I already tried everything I found on the net but really nothing of that worked!

Is anyone there who had/has the same problem? any Solutions?

Hope for help!

Edit:

Well I was able to center the content of a static cell with this code:

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

But the content should come from a database and i really hav no idea how to get the data from the ObservableList Object that i use in the TABLEVIEWNAME.setItems method...

I first used this code:

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

but there was no way to center that content!

Please could anyone help me?

Edit:

Thanks to this Great Answer, I did it!

Code below:

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"));

BEST THANKS!!!

Answer

Rasha picture Rasha · Sep 13, 2013

CellValueFactory and CellFactory are two different things. CellValueFactory is used to specify where do the values come from, while CellFactory specifies how do they get displayed.

Use both at the same time. But in the setCellFactory code you should not do setText . Setting the text will be taken care of by the TableCell code inside the updateItem() method. This method will use the value that is provided from the ' cellValueFactory ' and take care of setting it inside its own label.

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