Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!

Mike Matthews

Ranch Hand
+ Follow
since Jan 11, 2015
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mike Matthews

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.
3 years ago
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.
3 years ago

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.

  • 3 years ago

    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

    (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...
    3 years ago
    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?
    3 years ago
    Huge thanks! This is perfect. I still need to analyse your code further, but I already learned new things, some of which I might use elsewhere in my code. I'll need a closer look at Runnable interface (something I avoided in the past) and the framework you referred to. I can now mark this thread as resolved.

    I adjusted your code slightly to fit in my code and allow reusability. It works wonders. Thank you kindly.

    The only (small) issue is that the indeterminate checkbox is smaller than its checked and unchecked states, which causes the GridPane to resize upon changing the state of Select All checkbox. It doesn't influence the usability of checkboxes so I'll let it be for the time being.
    4 years ago
    Hey Ranchers,

    I have a number of checkboxes that I want to bind to another Select All checkbox. Here's what I'm trying to achieve:
  • Checked state for Select All - all checkboxes get checked,
  • Unchecked state for Select All - all checkboxes get unchecked,
  • Indeterminate state for Select All - no change to other checkboxes,
  • A checkbox gets checked so that all are checked - Select All checkbox gets checked,
  • A checkbox gets unchecked so that all are unchecked - Select All checkbox gets unchecked,
  • A checkbox gets checked/unchecked so that states of checkboxes differ - Select All checkbox's state changes to indeterminate.

  • I tinkered with bidirectional binding, binding and adding a listener. I believe that adding a listener could help, but the code would be most likely unnecessarily long. Thank you in advance.
    4 years ago
    Hi John,

    Thank you kindly for your reply. I already solved the problem using exactly this solution (extractor.) It worked perfectly. I added the extractor() method to my Item class so now it looks as follows:

    I used ListProperty<Item> to store items. Hopefully it will help someone else in the future.
    4 years ago
    Hello Ranchers,

    I want objects listening to changes in my ObservableList to be notified of changes in one of its elements without needing to remove the element and add a new one in its place. I hope I make myself clear. Right now, to trigger change event, I need to write the code as follows (create a tempItem):

    4 years ago

    Paul Clapham wrote:You could consider a factory pattern:

    This has an advantage over an array or list of buttons, namely that you can use variables with meaningful names.

    Thanks. I haven't thought of it. Would it be even more efficient to pass the new button as an argument (as below), the method serving the purpose of only configuring it? I guess it can't be called a factory method in such case, however...

    Thanks for the suggestion Carey. I might not use this exact method, but I'll consider using this approach elsewhere in my small app.
    4 years ago
    Thanks. I thought there was some way to make it cleaner code-wise. I'll consider making a list (maybe temporary) as I have a few similar operations to make on these same buttons.
    4 years ago
    Hello Ranchers,

    Is there a way to call the same method for multiple objects without re-writing the same code as in the example below?

    I'm using GridPane and want the same width for all my buttons (preferrably as low as possible).
    4 years ago
    Thank you, Alejandro! I've already seen most of the videos from the series you mentioned and I admit they're helpful.

    I learned plenty since I made this thread. I also solved the problem. Instead of adding separate remove button for each object, I added controls below the table. I might try separate buttons for the sake of learning something new, especially since I managed to find similar solutions on the web.

    Also, thanks, John! I'll definitely have a look at the tutorial by Makery.
    4 years ago
    Dear Ranchers,

    I'm making a small app for myself to review what I have learned in Java before. It's a simple daily expenditure calculator. Basically it should allow to add and remove entries (e.g. trousers, dinner), the app should then present them in a table and give an average daily expenditure below. I have all classes and methods ready. Now I only need to lay everything out.

    I want to be able to add and remove entries easily and effectively. I was considering using JavaFX' TableView with separate remove button for each entry, but I have no clue how.

    Can anyone offer a solution? Perhaps there's a better way than using JavaFX. (I want to learn to make apps that are visually attractive and intuitive.)
    4 years ago
    Hey Ranchers,

    I won't be boring you with my life story. My knowledge about Java might be above beginner's level, but I know nothing when it comes to actual programming. I believe there were already questions like this one before, for which I apologise. Even I actually asked once about this so-called "next step". Then again, this topic is a little more personalised.

    Where to begin a project? How?

    I heard it's not good practice to:
  • start with creating UI,
  • make each step of development too big,
  • include any crucial code in the class with the main() method (or start() method in case of JavaFX apps, which are more in my interest.)

  • Maybe now is the time to read a book like "Effective Programming" by Joshua Bloch or "Head First! Design Patterns"? Or maybe there is a place where I can learn from sample applications?
    4 years ago