JavaFX 8 TextFieldTableCell

时间:2014-05-11 14:40:09

标签: java javafx-2 javafx-8

我试图限制在Java fx 8

中的表格单元格中键入的字符

我的问题特定于TextFieldTableCell和TextFieldTreeTableCell

我已经通过向

添加actionlistener为独立文本字段实现了受限文本输入
textfield.textProperty.addListener(new Listener())

有没有办法可以获取用于呈现上述两个类的文本字段,以便我可以重用上面的侦听器代码。

如果无法做到这一点,是否有办法通过扩展TextFieldTreeTableCell来实现受限制的文本输入

我尝试了这个但是我得到了一个N​​ullPointer异常

  

DEBUG [JavaFX应用程序线程](RestrictedTreeTableCell.java:23) - 来这里   线程中的异常" JavaFX应用程序线程"显示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);

      }
  }
}

1 个答案:

答案 0 :(得分:0)

工作代码在这里

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();

      }
  }

}