コード例 #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) {
                  }
              }
            });
  }
コード例 #3
0
  private void createZoomTextField() {
    mZoomTextField = new JFormattedTextField(new DecimalFormat("##0.00%"));
    mZoomTextField.setFocusLostBehavior(JFormattedTextField.REVERT);
    mZoomTextField.setHorizontalAlignment(SwingConstants.CENTER);
    mZoomTextField.setColumns(6);
    mZoomTextField.setMinimumSize(mZoomTextField.getPreferredSize());

    mZoomTextField.addActionListener(
        (x) -> mImagePresentationModel.setZoom(((Number) mZoomTextField.getValue()).doubleValue()));

    mImagePresentationModel.addListener(
        new ImagePresentationModel.Listener() {
          @Override
          public void onVisibleImageContentUpdate() {
            mZoomTextField.setValue(mImagePresentationModel.getZoom());
          }

          @Override
          public void onImageChange() {
            onVisibleImageContentUpdate();
          }
        });
  }
コード例 #4
0
  /**
   * @param tf
   * @param format
   */
  public TableCellEditorForDezimal(final JFormattedTextField tf, final NumberFormat format) {
    super(tf);
    super.setClickCountToStart(1);
    tf.setFocusLostBehavior(JFormattedTextField.COMMIT);
    tf.setHorizontalAlignment(SwingConstants.LEFT);
    tf.setBorder(null);
    delegate =
        new EditorDelegate() {

          boolean isMousePressed = false;

          @Override
          public void setValue(Object param) {
            if (isMousePressed
                && param != null
                && (param.getClass() == Double.class || param.getClass() == BigDecimal.class)) {
              SwingUtilities.invokeLater(
                  new Runnable() {

                    public void run() {
                      tf.selectAll();
                    }
                  });
              try {
                tf.setText(
                    format == null
                        ? FormatNumber.formatDezimal((Number) param)
                        : format.format((Number) param));
              } catch (Exception e) {
                try {
                  param = new BigDecimal(String.valueOf(param));
                  tf.setText(
                      format == null
                          ? FormatNumber.formatDezimal((Number) param)
                          : format.format((Number) param));
                } catch (Exception ex) {
                  tf.setText(String.valueOf(param));
                }
              }
            } else {
              tf.setText("");
            }
          }

          @Override
          public Object getCellEditorValue() {
            try {
              String _field = tf.getText();
              Number _number =
                  (format == null ? FormatNumber.parseDezimal(_field) : format.parse(_field));
              if (_number != null) {
                double _parsed = _number.doubleValue();
                BigDecimal d = BigDecimal.valueOf(_parsed);
                //                        tf.setBackground(Color.white);
                return d;
              } else {
                //                        tf.setBackground(Color.white);
                return BigDecimal.ZERO;
              }
            } catch (ParseException e) {
              Log.Debug(this, e);
              //                    tf.setBackground(Color.red);
              return BigDecimal.ZERO;
            }
          }

          @Override
          public boolean isCellEditable(EventObject anEvent) {
            if (anEvent instanceof MouseEvent) {
              isMousePressed = true;
              return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
            }
            isMousePressed = false;
            return true;
          }
        };
  }
コード例 #5
0
  public FormatTestFrame() {
    JPanel buttonPanel = new JPanel();
    okButton = new JButton("Ok");
    buttonPanel.add(okButton);
    add(buttonPanel, BorderLayout.SOUTH);

    mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout(0, 3));
    add(mainPanel, BorderLayout.CENTER);

    JFormattedTextField intField = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField.setValue(new Integer(100));
    addRow("Number:", intField);

    JFormattedTextField intField2 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField2.setValue(new Integer(100));
    intField2.setFocusLostBehavior(JFormattedTextField.COMMIT);
    addRow("Number (Commit behavior):", intField2);

    JFormattedTextField intField3 =
        new JFormattedTextField(
            new InternationalFormatter(NumberFormat.getIntegerInstance()) {
              protected DocumentFilter getDocumentFilter() {
                return filter;
              }
            });
    intField3.setValue(new Integer(100));
    addRow("Filtered Number", intField3);

    JFormattedTextField intField4 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField4.setValue(new Integer(100));
    intField4.setInputVerifier(
        new InputVerifier() {
          public boolean verify(JComponent component) {
            JFormattedTextField field = (JFormattedTextField) component;
            return field.isEditValid();
          }
        });
    addRow("Verified Number:", intField4);

    JFormattedTextField currencyField = new JFormattedTextField(NumberFormat.getCurrencyInstance());
    currencyField.setValue(new Double(10));
    addRow("Currency:", currencyField);

    JFormattedTextField dateField = new JFormattedTextField(DateFormat.getDateInstance());
    dateField.setValue(new Date());
    addRow("Date (default):", dateField);

    DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT);
    format.setLenient(false);
    JFormattedTextField dateField2 = new JFormattedTextField(format);
    dateField2.setValue(new Date());
    addRow("Date (short, not lenient):", dateField2);

    try {
      DefaultFormatter formatter = new DefaultFormatter();
      formatter.setOverwriteMode(false);
      JFormattedTextField urlField = new JFormattedTextField(formatter);
      urlField.setValue(new URL("http://java.sun.com"));
      addRow("URL:", urlField);
    } catch (MalformedURLException ex) {
      ex.printStackTrace();
    }

    try {
      MaskFormatter formatter = new MaskFormatter("###-##-####");
      formatter.setPlaceholderCharacter('0');
      JFormattedTextField ssnField = new JFormattedTextField(formatter);
      ssnField.setValue("078-05-1120");
      addRow("SSN Mask:", ssnField);
    } catch (ParseException ex) {
      ex.printStackTrace();
    }

    JFormattedTextField ipField = new JFormattedTextField(new IPAddressFormatter());
    ipField.setValue(new byte[] {(byte) 130, 65, 86, 66});
    addRow("IP Address:", ipField);
    pack();
  }