5 Replies Latest reply: Apr 7, 2013 12:44 PM by 977405 RSS

    Refreshing components after action

    977405
      Hello, today i have a next problem:

      I have TreeView which contains rekords from SQLite database and Dialog, which adding records for SQLite database. But when i'm adding a new record to db i must restart application to get a new added rekord in Tree. Situation looks the same for PieChart and label.

      It is any way to refresh TreeView after adding somethink to db?

      I need refresh too label and PieChart.

      Can someone help mi in this situation?

      Greetings!
        • 1. Re: Refreshing components after action
          James_D
          The data displayed in a TreeView is organized as a hierarchy of TreeItems. The TreeItem has a getChildren() method that gives a list of its child nodes in the tree. So all you need do is call treeItem.getChildren().add(...) to add new nodes to the tree.

          {thread:id=2460705} has an example of dynamically adding and removing tree nodes.

          For a label, you just need to call label.setText(). For a PieChart, call getData() and manipulate the list that is returned.
          • 2. Re: Refreshing components after action
            977405
            I don't asked about it. I know what to do to add a new item to tree. I do it dynamically from db. But when i add a new item to DB i must RESTART PROGRAM to get visiblity of new item. My question is how "refresh", render again a tree, because the tree must again brings the list with records from db after changes.

            Edited by: 974402 on 2013-04-07 07:48
            • 3. Re: Refreshing components after action
              James_D
              OK, maybe I am not fully understanding what you're trying to do.

              When you add new items to the database, are you doing that from within your JavaFX application, or are you accessing the database from somewhere else?

              Edited by: James_D on Apr 7, 2013 8:31 AM
              • 4. Re: Refreshing components after action
                977405
                Yes i'm adding to db from somewhere else. From other class extends Stage (dialog window) and there is properly interface for db accesing.

                Edited by: 974402 on 2013-04-07 10:44
                • 5. Re: Refreshing components after action
                  James_D
                  If the database is being updated from outside of your application, then it can be difficult or impossibile to keep your application completely in sync with the database. If the database has a mechanism for notifying external applications when changes are made, then you could program your application to hook into that; that's really a question about your database engine, and not about JavaFX.

                  Otherwise there are really two choices: have a "Refresh" button in your application, or poll the database at regular intervals and refresh automatically. In either case, you can just invoke the same code that you use to build the tree from the database in order to refresh the tree. You might want to add some (likely fairly complex) logic to maintain the expanded state and selection of the TreeItems. You could also retrive the data from the database and compare it to what's displayed in the tree, making changes only where necessary (this would, in theory, be more efficient at the expense of more complex code).

                  In applications I write, I simply try to avoid this situation completely. In large, distributed applications, where multiple actors access the same database, on option use a JEE interface to the database and make all database access pass through that. The JEE DAO can then be coded to notify any live actors that need to be in sync with the database when changes happen. This is a large undertaking and requires in-depth knowledge of JEE.