コード例 #1
0
    /** Constructs cell editor. */
    public CellEditor() {
      super(new JFormattedTextField());
      final JFormattedTextField ftf = (JFormattedTextField) getComponent();

      // Set GUI behaviour of text field
      ftf.setValue(null);
      ftf.setHorizontalAlignment(JTextField.LEADING);
      ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);

      // Set that one click on cell is enough for editing
      setClickCountToStart(1);

      // Special handling code for ENTER
      ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
      ftf.getActionMap()
          .put(
              "check",
              new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                  if (!ftf.isEditValid()) {
                    if (askEditOrRevert(ftf, null)) {
                      ftf.setValue(ftf.getValue());
                      ftf.postActionEvent();
                    }
                  } else
                    try {
                      ftf.commitEdit();
                      ftf.postActionEvent();
                    } catch (java.text.ParseException exc) {
                      // nothing to do
                    }
                }
              });
    }
コード例 #2
0
ファイル: FloatEditor.java プロジェクト: miguelmeca/sippi
  /**
   * Constructor con dos parámetros para definir los límites que queremos definirle al número a
   * ingresar.
   *
   * @param min (Float) Permite un NULL como valor lo que no definirá un valor para el límite
   *     inferior.
   * @param max (Float) Permite un NULL como valor lo que no definirá un valor para el límite
   *     superior.
   */
  public FloatEditor(Float min, Float max) {
    super(new JFormattedTextField(new DecimalFormat("####.##")));
    ftf = (JFormattedTextField) getComponent();
    this.minimum = min;
    this.maximum = max;

    // Set up the editor for the float cells.
    floatFormat = new DecimalFormat("####.##");
    NumberFormatter floatFormatter = new NumberFormatter(floatFormat);
    floatFormatter.setFormat(floatFormat);
    floatFormatter.setMinimum(minimum);
    floatFormatter.setMaximum(maximum);

    ftf.setFormatterFactory(new DefaultFormatterFactory(floatFormatter));
    ftf.setValue(minimum);
    ftf.setHorizontalAlignment(JTextField.TRAILING);
    ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);

    // React when the user presses Enter while the editor is
    // active.  (Tab is handled as specified by
    // JFormattedTextField's focusLostBehavior property.)
    ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
    ftf.getActionMap()
        .put(
            "check",
            new AbstractAction() {
              @Override
              public void actionPerformed(ActionEvent e) {
                if (!ftf.isEditValid()) { // The text is invalid.
                  if (userSaysRevert()) { // reverted
                    ftf.postActionEvent(); // inform the editor
                  }
                } else
                  try { // The text is valid,
                    ftf.commitEdit(); // so use it.
                    ftf.postActionEvent(); // stop editing
                  } catch (java.text.ParseException exc) {
                  }
              }
            });
  }