JavaFX TableView - Center the Content of a Table Column


Question

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!!!

1
3
9/13/2013 12:52:40 PM

Accepted Answer

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;
      }
   }
);
3
1/8/2015 11:09:33 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon