abdou amer
abdou amer

Reputation: 849

JavaFX Update tabs when content of database changes

I have devlopped a desktop application, using JavaFX framework. My main Window containt a tabpane, witch hold many subwindow (tabs), with diffrent controllers. When the user make CRUD operation for a particular sub window, the database content will change. As result other sub window may use a content updated by a subwindow, that makes a changes. So, what the write approche to update UI on other sub windows, when a change in database occurs.

Here is an fxml file that illusatres the mainWindow structure:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <TabPane layoutX="38.0" layoutY="14.0" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <tabs>
          <Tab text="subWindow 1">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TableView prefHeight="371.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                          <columns>
                            <TableColumn prefWidth="75.0" text="C1" />
                            <TableColumn prefWidth="75.0" text="C2" />
                          </columns>
                        </TableView>
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
          <Tab text="subWindow 2">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <ComboBox prefWidth="150.0" />
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
            <Tab text="subWindow 3">
               <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
               </content>
            </Tab>
            <Tab text="subWindow n">
               <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
               </content>
            </Tab>
        </tabs>
      </TabPane>
   </children>
</AnchorPane>

Upvotes: 0

Views: 690

Answers (1)

Timo
Timo

Reputation: 11

A common way to solve this problem is to make extensive use of property binding. Which is supported very well in JavaFX. Using this way you would not listen to updates to your DB, but all SubWindows would read and write data by using the same component and JavaFX-Properties. Which has the same effect if there is only one user writing in that DB. Have a look at the MVVM Pattern. There is even a framework, supporting MVVM for JavaFX: mvvmFX I have not tried it yet, please let me know when you evaluated it!

Upvotes: 0

Related Questions