0

I'm a little bit confused on how to make a notification that can be received by any component in my application.Let's assume that i have an application like so: the main window have a tabpane with 3 tabs, each tab hold a window, and is owned by a single controller. So, i want to make it possible to notify other tabs in my application with changes that occurs in a specific tab.

I Tried to use use Google Guava, but i don't find a way to get a refrence to the controller.

Here is the code that illustrates the above:

<?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="64.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="Tab1">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
          <Tab text="Tab2">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
            <Tab text="Tab3">
               <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
               </content>
            </Tab>
        </tabs>
      </TabPane>
   </children>
</AnchorPane>
abdou amer
  • 819
  • 2
  • 16
  • 43
  • The MVC approach is to share a model (implemented with observable properties) instance among all three controllers. See e.g. http://stackoverflow.com/questions/32342864/applying-mvc-with-javafx. Then each controller can just observe the same properties for changes. (You can also do this with an event bus, as you suggest, though I am far less familiar with that pattern.) – James_D Apr 26 '16 at 22:02
  • but the three controllers doesn't use the same model, for each controller we can find diffrent data models (combobox model, tableview model...etc). for example, tab 1 have a "combobox, tableview" and tab2 have a "combobox," when a tableview of tab1 changes the combobox must be updated in tab2. – abdou amer Apr 27 '16 at 18:09
  • Well, if you want the different views to share the same data, they need to share the same model. That's kind of the whole point of the MVC architecture :). The model is *just* the data, probably in the cases you cite represented by an `ObservableList`. When the tableview in tab1 changes, the controller updates the data in the model, and the controller for tab2 sees the change. – James_D Apr 27 '16 at 18:30

0 Answers0