title: Javafx: fail to display content properly in table cell
body: <p>I have the following <code>Trade</code> object class. </p>
  
  <pre><code>public class Trade implements Comparable&lt;Trade&gt;{
  
        // attributes of each trade that go into the tableViewTransaction log
  
        // properties
        private StringProperty itemID;
  
        public Trade(int itemID){
            this.itemID = new SimpleStringProperty(String.format("%04d",itemID));
        }
  
        public String getItemID(){
            return this.itemID.get();
        }
  
  
        public StringProperty itemIDProperty(){
            return this.itemID;
        }
  
        public void setItemID(String itemID){
              int id = Integer.parseInt(itemID);
              this.itemID.set(String.format("%04d",id));
        }
  }
  </code></pre>
  
  <p>Now in my <code>Controller</code> class, I have a tableView <code>TransactionLog</code> and a table column for <code>itemID</code>. </p>
  
  <pre><code>public TableView&lt;Trade&gt; fxTransactionLog;
  public TableColumn&lt;Trade, String&gt; fxTransactionLogItemID;
  </code></pre>
  
  <p>The tableView is editable, so is the table column using the following code.</p>
  
  <p><strong>Here is where the problem is</strong>: The tableView is able to display itemID perfectly.  For example say when I create a new <code>Trade</code> object with itemID = 1, the table cell will display <code>0001</code>, then I decide to edit the <code>itemID</code> of a Trade object and type in to a <strong>new ID</strong> of 13, it will show up as <code>0013</code> like below.</p>
  
  <p><code>0001 -&gt; 0013     // This works fine, if I edit the cell and assign a DIFFERENT value to the cell.</code></p>
  
  <p>However, if I click to edit the <code>itemID</code> and assign the same value it already has, which is 1 in this case. It displays <code>1</code>, which is not what I want, as it is missing the leading zeros. I looked through my code and just couldn't figure out why this is happening. This is more of an <strong>aesthetic</strong> issue. </p>
  
  <p><code>0001 -&gt; 1       // edit and reassign same value to the cell</code></p>
  
  <p>How can I make it display <code>0001</code> instead of just <code>1</code> even if I assign the SAME value to the cell ? </p>
  
  <p><strong>Secondly</strong>, what code and where should I write to prevent the user from typing in <code>String</code> for itemID ? </p>
  
  <p><strong>UPDATE:</strong> So I followed this<a href="https://docs.oracle.com/javafx/2/ui_controls/table-view.htm" rel="nofollow">link</a> Example 12-11 . I created a separate class for <code>EditingItemIDCell</code>.</p>
  
  <p>import javafx.scene.control.TableCell;
  import javafx.scene.control.TextField;
  public class EditingItemIDCell extends TableCell{
      private TextField textField;</p>
  
  <pre><code>public EditingItemIDCell() {
  }
  
  @Override
  public void updateItem(String item, boolean empty) {
      super.updateItem(item, empty);
  
      if (empty) {
          setText(null);
          setGraphic(null);
      } else {
          if (isEditing()) {
              if (textField != null) {
                  textField.setText(String.format("%04d",Integer.parseInt(getString())));
              }
              setText(null);
              setGraphic(textField);
          } else {
              setText(getString());
              setGraphic(null);
          }
      }
  }
  
  private String getString() {
      return getItem() == null ? "" : getItem().toString();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And in my <code>Controller</code> class, I made the following changes. </p>
  
  <p>But I am getting an error saying:</p>
  
  <pre><code>The method setCellFactory(Callback&lt;TableColumn&lt;Trade,String&gt;,TableCell&lt;Trade,String&gt;&gt;) in the type TableColumn&lt;Trade,String&gt; is not applicable for the arguments (Callback&lt;TableColumn,TableCell&gt;).
  </code></pre>