title: JavaFx TableView is not sorting the table
body: <p>I have put together a simple JavaFx with FXML to display a sorted address book (sorting by the first name column). I have defined a simple sorting rule for the first name column, however, the table loads unsorted. I know how to sort a column in the FXML, however, my objective is to see if I can also accomplish the sorting through the controller.</p>
  <p>Thanks in advance.</p>
  <p>FXML file:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.Label?&gt;
  &lt;?import javafx.scene.control.TableColumn?&gt;
  &lt;?import javafx.scene.control.TableView?&gt;
  &lt;?import javafx.scene.control.TextField?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  &lt;?import javafx.scene.text.Font?&gt;
  
  &lt;AnchorPane id=&quot;AnchorPane&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;org.openjfx.mavenfxaddressbooktable.AddressbookController&quot;&gt;
      &lt;children&gt;
          &lt;TableView fx:id=&quot;tableid&quot; layoutX=&quot;133.0&quot; layoutY=&quot;62.0&quot; prefHeight=&quot;200.0&quot; prefWidth=&quot;353.0&quot;&gt;
              &lt;columns&gt;
                  &lt;TableColumn fx:id=&quot;firstnameid&quot; prefWidth=&quot;75.0&quot; text=&quot;First Name&quot; /&gt;
                  &lt;TableColumn fx:id=&quot;lastnameid&quot; prefWidth=&quot;75.0&quot; text=&quot;Last Name&quot; /&gt;
                  &lt;TableColumn fx:id=&quot;emailid&quot; prefWidth=&quot;201.3333028157552&quot; text=&quot;Email Address&quot; /&gt;
              &lt;/columns&gt;
              &lt;!-- following is the easiest way to sort a column in the table or it can be done in the controller
              &lt;sortOrder&gt;
                  &lt;fx:reference source=&quot;firstnameid&quot;/&gt;
              &lt;/sortOrder&gt; --&gt;
          &lt;/TableView&gt;
          &lt;Label layoutX=&quot;192.0&quot; layoutY=&quot;6.0&quot; text=&quot;Sorted Address Book&quot; textFill=&quot;#da1b1b&quot;&gt;
              &lt;font&gt;
                  &lt;Font name=&quot;System Bold&quot; size=&quot;24.0&quot; /&gt;
              &lt;/font&gt;
          &lt;/Label&gt;
          &lt;TextField fx:id=&quot;addfirstname&quot; layoutX=&quot;42.0&quot; layoutY=&quot;297.0&quot; promptText=&quot;First name&quot; /&gt;
          &lt;TextField fx:id=&quot;addlastname&quot; layoutX=&quot;206.0&quot; layoutY=&quot;299.0&quot; promptText=&quot;Last name&quot; /&gt;
          &lt;TextField fx:id=&quot;addemail&quot; layoutX=&quot;370.0&quot; layoutY=&quot;299.0&quot; promptText=&quot;Email address&quot; /&gt;
          &lt;Button fx:id=&quot;addbuttonid&quot; layoutX=&quot;277.0&quot; layoutY=&quot;361.0&quot; mnemonicParsing=&quot;false&quot; `onAction=&quot;#addContact&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;53.0&quot; text=&quot;Add&quot; textFill=&quot;#08ab2c&quot;&gt;`
              &lt;font&gt;
                  &lt;Font name=&quot;Tahoma Bold&quot; size=&quot;14.0&quot; /&gt;
              &lt;/font&gt;
          &lt;/Button&gt;
      &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  <p>The Application code</p>
  <pre><code>import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  import java.io.IOException;
  
  /**
   * JavaFX AddressBookApp
   */
  public class AddressBookApp extends Application {
  
      private static Scene scene;
  
      @Override
      public void start(Stage stage) throws IOException {
          scene = new Scene(loadFXML(&quot;addressbook&quot;));
          stage.setScene(scene);
          stage.setTitle(&quot;Address Book&quot;);
          stage.show();
      }
  
      static void setRoot(String fxml) throws IOException {
          scene.setRoot(loadFXML(fxml));
      }
  
      private static Parent loadFXML(String fxml) throws IOException {
          FXMLLoader fxmlLoader = new FXMLLoader(AddressBookApp.class.getResource(fxml + &quot;.fxml&quot;));
          return fxmlLoader.load();
      }
  
      public static void main(String[] args) {
          launch();
      }
  
  }
  </code></pre>
  <p>The data bean:</p>
  <pre><code>import javafx.beans.property.SimpleStringProperty;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  
      public class Person implements Comparable{
      
          private final SimpleStringProperty name = new SimpleStringProperty(&quot;&quot;);
          private final SimpleStringProperty last = new SimpleStringProperty(&quot;&quot;);
          private final SimpleStringProperty email = new SimpleStringProperty(&quot;&quot;);
      
          public Person(String first, String last, String email) {
              setName(first);
              setLast(last);
              setEmail(email);
          }
      
          public final void setEmail(String email) {
              this.email.set(email);
          }
      
          public final void setName(String name) {
              this.name.set(name);
          }
      
          public final void setLast(String last) {
              this.last.set(last);
          }
      
          public String getName() {
              return name.get();
          }
      
          public String getLast() {
              return last.get();
          }
      
          public String getEmail() {
              return email.get();
          }
      
          public static ObservableList&lt;Person&gt; getList() {
              ObservableList&lt;Person&gt; list = FXCollections.observableArrayList();
              list.add(new Person(&quot;Zuke&quot;, &quot;Adams&quot;, &quot;zadam@yahoo.com&quot;));
              list.add(new Person(&quot;Bettie&quot;, &quot;Burns&quot;, &quot;bburns@gmail.com&quot;));
              list.add(new Person(&quot;Sal&quot;, &quot;Ottaviano&quot;, &quot;sottaviano@ibm.com&quot;));
              list.add(new Person(&quot;Cindy&quot;, &quot;Moraz&quot;, &quot;cmoraz@aol.com&quot;));
              list.add(new Person(&quot;Alison&quot;, &quot;Goldman&quot;, &quot;agoldman@sachs.com&quot;));
              
              return list;
          }
  </code></pre>
  <p>The controller code</p>
  <pre><code>import java.net.URL;
  import java.util.Comparator;
  import java.util.ResourceBundle;
  import javafx.collections.ObservableList;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.control.Button;
  import javafx.scene.control.TableColumn;
  import javafx.scene.control.TableView;
  import javafx.scene.control.TextField;
  import javafx.scene.control.cell.PropertyValueFactory;
  public class AddressbookController implements Initializable {
  
      @FXML
      private TableView&lt;Person&gt; tableid;
      @FXML
      private TableColumn&lt;TableView, String&gt; firstnameid;
      @FXML
      private TableColumn&lt;?, ?&gt; lastnameid; 
      @FXML
      private TableColumn&lt;?, ?&gt; emailid;
      @FXML
      private TextField addfirstname;
      @FXML
      private TextField addlastname;
      @FXML
      private TextField addemail;
      @FXML
      private Button addbuttonid;
      
      Comparator&lt;String&gt; columnComparator = (String v1 , String v2)-&gt;{
          return v1.toLowerCase().compareTo(v2.toLowerCase());
      };
  
      /**
       * Initializes the controller class.
       */
      @Override
      public void initialize(URL url, ResourceBundle rb) {
          firstnameid.setCellValueFactory(new PropertyValueFactory&lt;&gt;(&quot;name&quot;));
          lastnameid.setCellValueFactory(new PropertyValueFactory&lt;&gt;(&quot;last&quot;));
          emailid.setCellValueFactory(new PropertyValueFactory&lt;&gt;(&quot;email&quot;));
          firstnameid.setComparator(columnComparator);
          firstnameid.setSortable(true);
          ObservableList&lt;Person&gt; list = Person.getList();   
          tableid.getItems().addAll(list);
          tableid.sort();
      }    
  
      @FXML
      private void addContact(ActionEvent event) {
          addbuttonid.setOnAction(new EventHandler&lt;ActionEvent&gt;(){
              @Override
              public void handle(ActionEvent t) {
                  Person p = new Person(addfirstname.getText()
                          ,addlastname.getText()
                          ,addemail.getText());
                  tableid.getItems().add(p);
               //   ObservableList observe= tableid.getItems();
                 // observe.add(p);
                  
                  tableid.sort();
                  addfirstname.clear();
                  addlastname.clear();
                  addemail.clear();
                  
              }//handle()
              
          }//EventHandler()
          
          
          );//addbuttonid.setOnAction()
      }
  }
  
      @Override
      public int compareTo(Object o) {
          return this.getName().compareTo(((Person) o).getName());
      }
  }
  </code></pre>