0

I write my application that uses Tableview in which I want to represent and edit list of data.

I have data model. Something like

public class CModel 
{
    private List<CItem> m_lstItems;

    public List<CItem> getList()
    {
        return m_lstItems;
    }

}

public class CItem 
{

    private String m_sName;
    private String m_sType;

    public void setName(String s)
    {
        m_sName = s;
    }

    public String getName()
    {
        return new String(m_sName);
    }

}

If I need to bind my data model I can create observableList(). But this doesn’t allow me to observe items editing. To make editing possible I need to inherit CItem members from Observable. If I declare it as Property TableView observes items changes.

The problem is that if CModel is pure data model I shouldn’t inherit it from Observable (because data and its view should be separated).

How can I wrap every list item with Observable or what is best approach?

Yura
  • 969
  • 14
  • 33
  • See [this answer](http://stackoverflow.com/a/23644018/4185959) – Itai Feb 29 '16 at 10:16
  • 1
    Why do you use the `String(String)` constructor. Is there some requirement not to return a `String` with reference equality to the string passed to the setter? – fabian Feb 29 '16 at 13:51
  • 1
    http://stackoverflow.com/questions/23522130/javafx-properties-wrapping-bean may also be relevant – James_D Feb 29 '16 at 15:10
  • 1
    And also http://stackoverflow.com/questions/23187989/using-javafx-beans-properties-in-model-classes – James_D Feb 29 '16 at 15:58

1 Answers1

-1

For each column, create a CellValueFactory that wraps your POJO properties in a JavaFX Property. The example below does that on a fictive Person object that has a String property called name:

TableColumn<Person, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getName()));
Edvin Syse
  • 7,267
  • 18
  • 24
  • 2
    This doesn't bind the table to the data though. Changes in the data will not be automatically reflected in the table. – James_D Feb 29 '16 at 13:24
  • To bind the data, do tableView.setItems(yourListOfItems). If your data is not observable, do tableView.getItems().addAll(yourListOfItems). PS: This was not your question, please accept my answer to the original question :) – Edvin Syse Feb 29 '16 at 14:23
  • By "bind" I understand the table should update with, e.g. `tableView.getItems().get(0).setName(...)` which doesn't happen if you don't use observable properties. But not my question, so I can't accept it either way. – James_D Feb 29 '16 at 14:57
  • Ah, then I understand what you mean. Unless your domain object implements PropertyChangeSupport, what you want to to do impossible. If you implement PropertyChangeSupport you can add listeners that updates the ObjectProperty. It's also possible to automate this. If you don't already implement PropertyChangeSupport, you might as well change your domain objects to use JavaFX Properties. They will still be backwards compatible because you'd still expose normal getters and setters as well. – Edvin Syse Feb 29 '16 at 15:33
  • By the way, even if Observable and friends are defined in javafx.beans, these objects have no GUI related code, so you can use them in your domain objects. Alternatively, you can create a DTO with observable properties that copies over the properties from your POJO, and knows how to copy them back when you're done. – Edvin Syse Feb 29 '16 at 15:49
  • That's essentially what it says in my answer to the question @sillyfly linked. The `PropertyChangeSupport` approach is outlined in detail in the question I linked. – James_D Feb 29 '16 at 15:54
  • Gentlemen, thank you for the answers. I’m new in Java so I need time to understand your answers. Now I trying to understand your answers and implement it in simple application. I will appreciate if I can ask you again when I’ll make homework. – Yura Feb 29 '16 at 17:16