Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8401151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:43:56+00:00 2026-06-09T21:43:56+00:00

How does one easily apply custom (reusable) cellFactories to javafx.scene.control.TableCell; or javafx.scene.control.TableView; ?

  • 0

How does one easily apply custom (reusable) cellFactories to javafx.scene.control.TableCell; or javafx.scene.control.TableView;?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:43:57+00:00Added an answer on June 9, 2026 at 9:43 pm

    To apply custom CellFactories which format the displayed text within a cell, its color and so on, the following example might be good help.

    Initial situation

    Let’s assume you have a Bean/POJO Person:

    public class Person {
        private double levelOfGrowth = 0;
    
        public Person() {};
    
        public Person(double levelOfGrowth) {
            this.levelOfGrowth = levelOfGrowth;
        };
    
        public double getLevelOfGrowth() {
            return levelOfGrowth;
        }
    }
    

    Where levelOfGrowth is the percent-value of how much the growth of a person has been completed.

    It might be set between 0.00 to 1.00.

    Lets also assume you have created your view inside a FXML which is bound to a controller MainWindowController. You also set the column to display the levelOfGrowth to the id levelOfGrowthColumn

    public class MainWindowController implements Initializable {
        @FXML
        public TableColumn levelOfGrowthColumn;
    
        /**
         * Initializes the controller class.
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            ObservableList<Person> persons = FXCollections.observableArrayList();
    
            persons.add(new Person(0));
            persons.add(new Person(0.5));
            persons.add(new Person(1));
    
            levelOfGrowthColumn.setCellValueFactory(new PropertyValueFactory<Person, Double>("levelOfGrowth"));
    
        }
    }
    

    The intention

    The problem in above example is that the values shown in the view are like 0.5 instead of 50%. We also might like to change the color of values like 100% to green.

    The solution

    What we need is one (reusable) class that describes how the double-values have to be formatted/printed in the view and to connect that class with the levelOfGrowthColumn.

    The ‘formatter’-class

    public class PercantageFormatCell extends TableCell<Object, Double> {
    
        public PercantageFormatCell() {
        }
    
        @Override
        protected void updateItem(Double item, boolean empty) {
            super.updateItem(item, empty);
    
            // If the row is not empty but the Double-value is null,
            // we will always display 0%
            if (!empty && null == item) {
                item = new Double(0.0d);
            }
    
            // Here we set the displayed text to anything we want without changing the
            // real value behind it. We could also have used switch case or anything you
            // like.
            setText(item == null ? "" : NumberFormat.getPercentInstance().format(item));
    
            // If the cell is selected, the text will always be white
            // (so that it can be read against the blue background),
            // if the value is 1 it will be green.
            if (item != null) {
                double value = item.doubleValue();
                if (isFocused() || isSelected() || isPressed()) {
                    setTextFill(Color.WHITE);
                } else if (value < 1) {
                    setTextFill(Color.BLACK);
                } else {
                    setTextFill(Color.GREEN);
                }
            }
        }
    }
    

    Use it within your controller

    public class MainWindowController implements Initializable {
        @FXML
        public TableColumn levelOfGrowthColumn;
    
        /**
         * Initializes the controller class.
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            ObservableList<Person> persons = FXCollections.observableArrayList();
    
            persons.add(new Person(0));
            persons.add(new Person(0.5));
            persons.add(new Person(1));
    
            // New code START
    
            // In case we have multiple columns with percent-values it
            // might come in handy to store our formatter
            Callback<TableColumn, TableCell> percantageCellFactory =
                new Callback<TableColumn, TableCell>() {
                    public TableCell call(TableColumn p) {
                        return new PercantageFormatCell();
                    }
                };
    
            // Now all we have to do is to apply it
            levelOfGrowthColumn.setCellFactory(percantageCellFactory);
    
            // New code END
    
            levelOfGrowthColumn.setCellValueFactory(new PropertyValueFactory<Person, Double>("levelOfGrowth"));
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

does one perform better over the other in terms of indexing/quering etc ? e.g.
How does one wait until all of the Javascript is loaded before curling a
How does one implement a multithreaded single process model in linux fedora under c
How does one use rm to delete a file named '--help'? When I try,
How does one write a (Intel) F90 function that converts a string into lowercase
How does one optimize if the parameter space is only integers (or is otherwise
How does one determine what is the trigger of an event (close browser, close
How does one alternate row colors in a table in django that's generated using
How does one revert a remote git repo after a reset done locally? We
How does one access a member variable when using placeholders as functors in Thrust

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.