• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Custom CellValueFactory vs Automatic Update

 
Mike Matthews
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Ranchers,

I'm trying to populate a TableView with data (class Expense: name, quantity, unit price, total price). I had no trouble when using the default implementation of PropertyValueFactory. However, as I see it, there's no reason to add total price to the class if I can simply calculate it from unit price and quantity.

I can use the code below to achieve what I want:

but whenever quantity or unit price for a specific expense changes, the total price doesn't update automatically.

Now, according to the API:
PropertyValueFactory wrote:
In addition, the TableView will automatically add an observer to the returned value, such that any changes fired will be observed by the TableView, resulting in the cell immediately updating.

Does anyone know how to achieve this?
 
Dave Tolls
Ranch Hand
Posts: 2095
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you add a getTotal method to the expense class and do the calculation in there?
It strikes me as odd to do expose that calculation outside the class it (IMO) belongs to.
 
Paul Clapham
Sheriff
Posts: 21559
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good point, Dave, but it still doesn't address the issue of how to get some object in the data layer to notify a JavaFX object when it changes. Presumably there are listeners which can be used to do this, but knowing nothing about JavaFX I couldn't say what they are.

Speaking of JavaFX, it appears that this question is about JavaFX. So I've moved it into the JavaFX forum.
 
Dave Tolls
Ranch Hand
Posts: 2095
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point (and it wouldn't actually help anyway due to how bindings work).

What class is returned by getValue?
And the associated unitPriceProperty or qualityProperty?

Also, what does the code look like where you change either of those two values?
 
Mike Matthews
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Speaking of JavaFX, it appears that this question is about JavaFX. So I've moved it into the JavaFX forum.

I don't know how I got it posted to General forums; I was thinking of JavaFX in the first place. Thanks for moving it!

I seem to have forgotten the basic idea behind TableView – it should be used only to visualize data! I'm thinking about doing calculations within the class after all.

Still, to answer your questions, Dave:
1) What class is returned by getValue? It returns the Expense class, which populates the table.
2) And the associated unitPriceProperty or quantityProperty? These two are the price and quantity of the expense, so the table may look like this:
NameQuantityUnit priceTotal price
Bicycle2.0$300.00$600.00

(yet I'm not sure if I understand your question.)
3) Also, what does the code look like where you change either of those two values? These are changed based on user input. Again, if that's what you're asking for, the code looks as follows:

The thing is, like in the table above, my TableView displays the price with a currency, and each expense might have a different one. So, now I'm thinking about creating a custom class Price, which would store value and currency, which I could later extract for populating the table. Would it be better this way?

Another question is: is there a way to populate a column via a method? So, in other words, I'm thinking of calling a method like calculateTotalPrice() instead of using a propertyTotalPrice.

I hope it's not too much for one message...
 
Paul Clapham
Sheriff
Posts: 21559
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Matthews wrote:Would it be better this way?


Dunno. At this point in time I don't know what your problem is. Initially I thought that your Expense data was changing, and you wanted the TableView to change to match it. But now it looks like you might just be having trouble populating the TableView from a group of Expense objects. Maybe you could start over and clarify what you're trying to do, since if my assumption was wrong then I have led the thread down the garden path.
 
Mike Matthews
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Maybe you could start over and clarify what you're trying to do, since if my assumption was wrong then I have led the thread down the garden path.

Okay, here it goes:

  • Everything works fine if I simply populate the table with class Expense with following properties: name, quantity, unitPrice, totalPrice.
  • Obviously, totalPrice is simply quantity * unitPrice.
  • I believe there's no point in having a totalPrice property if I can simply calculate it, but maybe I'm wrong.
  • If I remove totalPrice property and calculate it within setCellValueFactory() method (as below), then it doesn't update automatically when either quantity or unitPrice is edited.

  •  
    Paul Clapham
    Sheriff
    Posts: 21559
    33
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Matthews wrote:
  • Everything works fine if I simply populate the table with class Expense with following properties: name, quantity, unitPrice, totalPrice.


  • Well, if it were me I would go with this solution. I don't see a problem with having a totalPrice property, especially if not having one requires code outside the Expense class to do the calculation. In fact I would have a problem with NOT having a totalPrice property if I found myself writing that code outside the Expense class. Violates encapsulation of the class.
     
    Mike Matthews
    Ranch Hand
    Posts: 49
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you. For now I'll stick to this basic solution, and keep exploring and experimenting with TableView. There's still a lot for me to learn.
     
    Dave Tolls
    Ranch Hand
    Posts: 2095
    15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Matthews wrote:
    3) Also, what does the code look like where you change either of those two values? These are changed based on user input. Again, if that's what you're asking for, the code looks as follows:



    What does setQuanity (quantity presumably being a binding property) look like?

    If it's something like this:

    then that's your issue.

    The property is being watched by the thing you've bound to.
    Replacing it entirely with a new one means that binding is lost.
    You should use the setValue method on the existing property.
     
    Mike Matthews
    Ranch Hand
    Posts: 49
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This isn't the issue since the method looks like this:


    All is working correctly now. It's just that if I wanted to get rid of totalPrice property and populate the column via a method, the table wouldn't update automatically upon value change.

    Still, thank you for your effort.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic