0

I am trying to display in treetableview all the members of a Citizen Class instance, but for some reason the it wont display a specific member while it displays other members! The member that isn't being displayed is: cWeapon under the column Weapon

there are 2 Classes:

1.Citizen

public class Citizen implements Comparable<Citizen> {
private String fullName, id;
private int birthYear, daysInQuarentine;
private boolean cWeapon;
private boolean inQuarantine;

public Citizen() {
    super();
}

public Citizen(String fullName, String id, int birthYear) {
    this.fullName = fullName;
    this.id = id;
    this.birthYear = birthYear;
    this.cWeapon = true;
}

public String getId() {
    return id;
}

public int getBirthYear() {
    return birthYear;
}

public String getFullName() {
    return fullName;
}

public int getDaysInQuarentine() {
    return daysInQuarentine;
}

public boolean iscWeapon() {
    return cWeapon;
}

public void setDaysInQuarentine(int daysInQuarentine) {
    this.daysInQuarentine = daysInQuarentine;
}

public boolean isInQuarantine() {
    return inQuarantine;
}

public void carryWeapon() {
    this.cWeapon = true;
}

@Override
public int compareTo(Citizen o) {
    return this.id.compareTo(o.getId());
}

@Override
public String toString() {
    return "ID: " + id + ", " + "FullName: " + fullName + ", " + "BirthYear: " + birthYear + ", " + ", "
            + "inQuarentine: " + inQuarantine;
}

2.Testing (JAVAFX)

public class Testing extends Application {

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

@Override
public void start(Stage primaryStage) throws Exception {
    TreeTableView<Citizen> treeTableView = new TreeTableView<Citizen>();

    TreeTableColumn<Citizen, ?> col1 = new TreeTableColumn<>("Full Name");
    TreeTableColumn<Citizen, ?> col2 = new TreeTableColumn<>("ID");
    TreeTableColumn<Citizen, ?> col3 = new TreeTableColumn<>("Quarantine");
    TreeTableColumn<Citizen, ?> col4 = new TreeTableColumn<>("Carry Weapon");

    col1.setCellValueFactory(new TreeItemPropertyValueFactory<>("fullName"));
    col2.setCellValueFactory(new TreeItemPropertyValueFactory<>("id"));
    col3.setCellValueFactory(new TreeItemPropertyValueFactory<>("inQuarantine"));
    col4.setCellValueFactory(new TreeItemPropertyValueFactory<>("cWeapon"));

    treeTableView.getColumns().add(col1);
    treeTableView.getColumns().add(col2);
    treeTableView.getColumns().add(col3);
    treeTableView.getColumns().add(col4);

    TreeItem<Citizen> citizen = new TreeItem<Citizen>(new Citizen("Jason Bourne", "458878968", 1970));
    treeTableView.setRoot(citizen);

    VBox vBox = new VBox(5);
    vBox.getChildren().add(treeTableView);
    Scene scene = new Scene(vBox, 400, 200);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Testing");
    primaryStage.show();
}

GUI Picture

Yaacov Biblow
  • 31
  • 1
  • 3
  • Following standard Java convention (which the `TreeItemPropertyValueFactory` assumes), your method should be called `isCWeapon()`. – James_D Jun 27 '20 at 17:17
  • @James_D That fixed it Thank you Sir! The getter was auto generated by eclipse so why is eclipse generating a wrong java method name? – Yaacov Biblow Jun 27 '20 at 17:27
  • It's a slightly weird case given the second letter of the variable name is capitalized; so it's arguable that it's not really clear what the "correct" name is. But property value factories will simply capitalize the first letter and prepend `get` or `is`. I don't recommend using `PropertyValueFactory` (or `TreeItemPropertyValueFactory`) at all - they are really legacy classes from pre-Java 8 days. Just use a lambda expression, so you can avoid any assumptions about method names. – James_D Jun 27 '20 at 17:30
  • i.e. just use `data -> new SimpleBooleanProperty(data.getValue().getValue().iscWeapon())` (now the compiler can check the method name, which doesn't have to conform to any convention). – James_D Jun 27 '20 at 17:32
  • it is not working and i get an error, where can i learn more about what you explained? i also have my real code which uses the treetableview of type Object which will cause the "data" to be of type object and i wont be able to the the iscWeapon – Yaacov Biblow Jun 27 '20 at 17:53
  • What’s the error? You probably shouldn’t use wildcards in the definitions of the columns, ie it should be `TreeTableColumn col4 ;` and then `col4.setCellValueFactory(data -> new SimpleBooleanProperty(data.getValue().getValue().iscWeapon()))` – James_D Jun 27 '20 at 18:00
  • i tried your solution on the simple project i wrote here and the lambda expression worked after changing the column definitions. my real project uses treetableview definition of Object and combines two classes together in one big treetableview, i cant seem to work with lambda expressions with casting and when i do the casting it will try to convert from one class to another class and through an exeption. but the first solution you gave me (working with TreeItemPropertyValueFactory) actually works smooth. where can i read more about this stuff? – Yaacov Biblow Jun 27 '20 at 20:12
  • About what? There's nothing much to read here; the cell value factory just has to be a callback which maps the cell data features to an observable value of the correct type for the column (see [docs](https://openjfx.io/javadoc/14/javafx.controls/javafx/scene/control/TreeTableColumn.html#setCellValueFactory(javafx.util.Callback))). So if you have a `TreeTableView` (which I don't really recommend), `data.getValue().getValue()` gives you an object, which you'll have to query somehow to produce the correct kind of `ObservableValue`. – James_D Jun 27 '20 at 20:15

0 Answers0