title: JavaFX: Prevent selection of a different tab if the data validation of the selected tab fails
body: <p>I'm creating a CRUD application that store data in a local h2 DB. I'm pretty new to JavaFX. I've created a <code>TabPane</code> to with 3 <code>Tab</code> using an <code>jfxml</code> created with <code>Scene Builder 2.0</code>. Each Tab contains an <code>AncorPane</code> that wrap all the controls: <code>Label</code>, <code>EditText</code>, and more. Both the <code>TabPane</code> and the <code>Tabs</code> are managed using one controller. This function is used to create and to update the data. It's called from a grid that display all the data. A pretty basic CRUD app. </p>
  
  <p>I'm stuck in the validation phase: when the user change the tab, by selecting another tab, it's called a validation method of the corresponding tab. If the validation of the Tab fails, I want that the selection remains on this tab.</p>
  
  <p>To achieve this I've implemented the following <code>ChangeListener</code> on the <code>SelectionModel</code> of my <code>TabPane</code>:</p>
  
  <pre><code>boolean processingTabValidationOnChange = false;
  
  tabPane.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener&lt;Number&gt;() {
  
              @Override
              public void changed(ObservableValue&lt;? extends Number&gt; ov, Number t, Number t1) {
                  if (processingTabValidationOnChange == false) {
                      boolean success;
  
                      switch (t.intValue()) {
                          case 0: success = validationTab1Passed();
                                  break;
                          case 1: success = validationTab2Passed();
                                  break;
                          case 1: success = validationTab3Passed();
                                  break;
                          default: success = false;    
                      }
                      if (success == false) {
                          processingTabValidationOnChange = true;
                          // select the previous tab
                          tabPane.getSelectionModel().select(t.intValue());
                          processingTabValidationOnChange = false;
                      }
                  }
              }
          });
  </code></pre>
  
  <p>I'm not sure that this is the right approach because:</p>
  
  <ol>
  <li>The event <code>changed</code> is fired two times, one for the user selection and one for the <code>.select(t.intValue())</code>. To avoid this I've used a global field boolean <code>processingTabValidationOnChange</code>... pretty dirty I know.</li>
  <li>After the <code>.select(t.intValue())</code> the TabPane displays the correctly Tab as selected but the content of the tab is empty as if the <code>AnchorPane</code> was hidden. I cannot select again the tab that contains the errors because it's already selected. </li>
  </ol>
  
  <p>Any help would be appreciated.</p>
  
  <p>Elvis</p>