Question

I am trying to restrict the characters typed in a Table cell in Java fx 8

My question is specific to the TextFieldTableCell and TextFieldTreeTableCell

I have implemented restricted text entry for a standalone textfield by adding a actionlistener to

textfield.textProperty.addListener(new Listener())

Is there a way that I can get hold of the textfield which is used to render the above two classes, so that I can reuse my listener code as above.

If this is not possible, is there a way of achieving restricted text input by extending the TextFieldTreeTableCell

I tried this but I am getting a NullPointer exception

DEBUG [JavaFX Application Thread] (RestrictedTreeTableCell.java:23) - coming here Exception in thread "JavaFX Application Thread" java.lang.NullPointerException at >com.sun.javafx.scene.control.skin.TreeTableRowSkin.getCell(TreeTableRowSkin.java:210) at >com.sun.javafx.scene.control.skin.TreeTableRowSkin.getCell(TreeTableRowSkin.java:54)

public class RestrictedTreeTableCell <S,T> extends TextFieldTreeTableCell<S, T>
{
  private final int maxChars;
  private final String pattern;
  private TextField textField;
  private RestrictedTextField rtf ;
  public RestrictedTreeTableCell(int maxVal,String disallowed)
  {

      super();
      log.debug("coming here");
      this.maxChars = maxVal;
      this.pattern = disallowed;


  }
  @Override public void startEdit() {

      log.debug("Inside over ridden edit");
      if (! isEditable() 
              || ! getTreeTableView().isEditable() 
              || ! getTableColumn().isEditable()) {
          return;
      }

      if (isEditing()) {
          if (textField == null) {
              textField = new TextField();
          }
          rtf = new RestrictedTextField(textField, 5);
          textField.textProperty().addListener(rtf);

      }
  }
}
Was it helpful?

Solution

The working code is here

public class RestrictedTreeTableCell <S,T> extends TextFieldTreeTableCell<S, T>
{
  private final int maxChars;
  private final String pattern;
  private TextField textField = new TextField();
  private RestrictedTextField rtf ;
  private static final Logger log = Logger.getLogger(RestrictedTreeTableCell.class);

  public RestrictedTreeTableCell(int maxVal,String disallowed)
  {

      super();

      this.maxChars = maxVal;
      this.pattern = disallowed;
      if(pattern!=null || pattern.length()>0)
          rtf = new RestrictedTextField(textField, maxChars,pattern);
      else            
          rtf = new RestrictedTextField(textField, maxChars);

  }
  @Override public void startEdit() {

      log.debug("Inside over ridden edit");
      super.startEdit();
      if (isEditing()) {
          log.debug("Inside is editing  ");

         textField.textProperty().addListener(rtf);
         if(getItem()!=null)
          textField.setText(getItem().toString());
            setGraphic(textField);
            textField.selectAll();

      }
  }

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top