0

I have to populate a 2 column table in javafx and I was wondering what was the best way of doing it?

Basically I have this class

public class Name {

    private String _name;
    private Rating _rating;

    public Name(String name , Rating rating) {
        _name = name;
        _rating = Rating.NO_RATING;
    }

    public String getName() {
        return _name;
    }

    public Rating getRating() {
        return _rating;
    }
}

I want to populate a table in javafx to have one column with the name and one column with a rating. I have tried populating through name class and using getName() getRating() methods but that is not working. I searched online and there are a thing called properties but how would I use properties in this case since I don't have any properties?

2
  • Please format your code. " but that is not working" - show what you did. Commented Oct 15, 2018 at 3:49
  • Your fields seem to be immutable; I recommend adding the final keyword in cases like this. I'm not sure why you don't use the constructor parameter for the _rating field though. If you're ok with the immutability of your items, just use new PropertyValueFactory("name") and new PropertyValueFactory("rating") as cellValueFactorys in your columns. Commented Oct 15, 2018 at 10:45

1 Answer 1

1

In the data model, warp the attributes you want to show in the table with properties :

//data model 
public class Name {

    private SimpleStringProperty name;
    private SimpleIntegerProperty rating;

    public Name(String name , int rating) {
        this.name = new SimpleStringProperty(name);
        this.rating = new SimpleIntegerProperty(rating);
    }

    public String getName() {
        return name.get();
    }

    public int getRating() {
        return rating.get();
    }
}

And use those properties in the table columns

import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class TableViewDemo extends Application {

    private final ObservableList<Name> data =
            FXCollections.observableArrayList(
                    new Name("Jacob", 12),
                    new Name("Isabella", 14),
                    new Name("Ethan", 5),
                    new Name("Emma", 17),
                    new Name("Michael",9));

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {

        stage.setTitle("Table View Sample");

        TableColumn<Name, String> nameCol = new TableColumn<>("Name");
        nameCol.setMinWidth(100);
        nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));

        TableColumn<Name, Integer> ratingCol =  new TableColumn<>("Rating");
        ratingCol.setMinWidth(100);
        ratingCol.setCellValueFactory(new PropertyValueFactory<Name, Integer>("rating"));
        final TableView<Name> table = new TableView<>();
        table.setItems(data);
        table.getColumns().addAll(nameCol, ratingCol);

        Scene scene = new Scene(table);
        stage.setScene(scene);
        stage.show();
    }
}


Edit in response to kleopatra's comment :

You can also use properties as cell value factories. Change data model to :

public class Name {

    private SimpleStringProperty name;
    private SimpleIntegerProperty rating;

    public Name(String name , int rating) {
        this.name = new SimpleStringProperty(name);
        this.rating = new SimpleIntegerProperty(rating);
    }

    public SimpleStringProperty nameProperty() { return name ; }

    public SimpleIntegerProperty ratingProperty() { return rating; }
}

and define column by:

    TableColumn<Name, String> nameCol = new TableColumn<>("Name");
    nameCol.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
    nameCol.setMinWidth(100);

    TableColumn<Name, String> ratingCol =   new TableColumn<>("Rating");
    ratingCol.setCellValueFactory(cellData -> cellData.getValue().ratingProperty().asString()); 
Sign up to request clarification or add additional context in comments.

1 Comment

no need for internal properties if you don't use them as such, that is expose as properties to allow getting rid off the errorprone PropertyValueFactory ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.