Hello all.
In my Apache derby db I set up Decimal data types like this:

admin_pay_rate DECIMAL(5,2),

I use JFormattedTextFields to filter decimal usage in text fields concerning money
Like admin pay rate would be 150.00: this is good.

In the JFormattedTextField it will allow 1,000.00 but the db crashes when this
Decimal is presented.

Will I have to change the db data type ?

admin_pay_rate DECIMAL(5,2),

I am not sure about maxValue in my class. Because I am not sure what is a good value
Or how to change maxValue to restrict a value that exceeds what the data base considers
A double.

Would someone know the best way to coordinate the field with the db ?

It works ok for now but the user has the ability to present number like
1,000,000,000.00 and in most of the values I am working with are not
that big.

package view.utils.formattedFields;

import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.text.DecimalFormat;
import javax.swing.JFormattedTextField;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

/**
 * Provides a JFormattedTextField that accepts only doubles. Allows for the setting of the number
 * of decimal places displayed, and min/max values allowed.
 * 
 * @author Mark Pendergast
 * Copyright Mark Pendergast
 */
public class FormattedDoubleField extends JFormattedTextField implements Serializable{

    public static final String PROP_MINVALUE_PROPERTY = "minimum value";
    public static final String PROP_MAXVALUE_PROPERTY = "maximum value";
    public static final String PROP_DECIMALPLACES_PROPERTY = "decimal places";
    
    public static final String displayFormat = "#,###,##0.000000000000000";
    public static final String editFormat = "#.###############";
    
     
    private double minValue = -Double.MAX_VALUE;
    private double maxValue = Double.MAX_VALUE;
    private int decimalPlaces = DEFAULT_DECIMALPLACES;
    private DefaultFormatterFactory dff;
    private view.utils.formattedFields.verifiers.DoubleVerifier verifier;

    
    private static final int DEFAULT_DECIMALPLACES = 2;
    private static final int MAX_DECIMALPLACES = 15;
    private static final int POSITIONS_LEFT_OF_DECIMALDISPLAY = displayFormat.indexOf('.');
    private static final int POSITIONS_LEFT_OF_DECIMALEDIT = editFormat.indexOf('.');

    private PropertyChangeSupport propertySupport;
/**
 * Default constructor
 */
    public FormattedDoubleField() {
       this(-Double.MAX_VALUE, Double.MAX_VALUE,0, DEFAULT_DECIMALPLACES);
    }
/**
 * Constructor that accepts the number of decimal places to display 
 *
 * @param places number of decimal places to display
 */
    public FormattedDoubleField(int places) {
       this(-Double.MAX_VALUE, Double.MAX_VALUE,0, places);
    }
/**
 *  Constructor that accepts a min and max allowed value
 * 
 * @param min min allowed value
 * @param max max alloed value
 * @throws java.lang.IllegalArgumentException
 */
     public FormattedDoubleField(double min, double max) throws IllegalArgumentException{
        this(min,max,0,DEFAULT_DECIMALPLACES);
    }
  /**
   *  Working Constructor
   * 
   * @param min min allowed value
   * @param max max allowed value
   * @param value inital value
   * @param places number of decimal places to display
   * @throws java.lang.IllegalArgumentException
   */   
     public FormattedDoubleField(double min, double max, double value, int places) throws IllegalArgumentException{
        propertySupport = new PropertyChangeSupport(this);

        setValue(new Double(value));
        minValue = min;
        maxValue = max;
        if(min > max)
            throw new IllegalArgumentException("min value cannot be greater than max value");
        verifier = new view.utils.formattedFields.verifiers.DoubleVerifier(min, max);
        setInputVerifier(verifier);
        NumberFormatter def = new NumberFormatter();
        def.setValueClass(Double.class);
        NumberFormatter disp = new NumberFormatter((new DecimalFormat(displayFormat.substring(0,POSITIONS_LEFT_OF_DECIMALDISPLAY+places+1))));
        disp.setValueClass(Double.class);
        NumberFormatter ed = new NumberFormatter((new DecimalFormat(editFormat.substring(0,POSITIONS_LEFT_OF_DECIMALEDIT+places+1))));
        ed.setValueClass(Double.class);
        dff = new DefaultFormatterFactory(def,disp,ed);
        setFormatterFactory(dff);
      
    }
     /*
      * Accessor methods
      */
    public double getMinValue() {
        return minValue;
    }
    
    public double getMaxValue() {
        return maxValue;
    }
    
    public int getDecimalPlaces()
    {
      return decimalPlaces;
    }
   /*
    * mutator methods
    */
    public void setMinValue(double value) throws IllegalArgumentException{
        if(value > maxValue)
               throw new IllegalArgumentException("min value cannot be greater than max value");
        double oldValue = minValue;
        minValue  = value;
        propertySupport.firePropertyChange(PROP_MINVALUE_PROPERTY, oldValue, minValue);
        verifier.setMinValue(value);
    }
    
    public void setMaxValue(double value) throws IllegalArgumentException {
        if(value < minValue)
               throw new IllegalArgumentException("max value cannot be less than min value");
        double oldValue = maxValue;
        maxValue  = value;
        propertySupport.firePropertyChange(PROP_MAXVALUE_PROPERTY, oldValue, maxValue);
        verifier.setMaxValue(value);
    }
    
    public void setDecimalPlaces(int value) throws IllegalArgumentException {
        if(value < 0 || value > MAX_DECIMALPLACES)
               throw new IllegalArgumentException("decimal places cannot be negative");
        int oldValue = decimalPlaces;
        decimalPlaces  = value;
        propertySupport.firePropertyChange(PROP_DECIMALPLACES_PROPERTY, oldValue, decimalPlaces);
        dff.setDisplayFormatter(new NumberFormatter((new DecimalFormat(displayFormat.substring(0,POSITIONS_LEFT_OF_DECIMALDISPLAY+value+1)))));
        dff.setEditFormatter(new NumberFormatter((new DecimalFormat(editFormat.substring(0,POSITIONS_LEFT_OF_DECIMALEDIT+value+1)))));
        setFormatterFactory(dff);
        repaint();
    }    
}

error

CLASS 
 public class PaymentRegisterForm extends MasterPaymentForm implements ActionListener {: 
validateBtnAction() : 2: txtFieldArray[UIDSEARCHFIELD].getText() : 
CONFIRM!: The Payment amount does not match the Instructors Lesson rate.
Error: Empty field: All Fields are required
CLASS 
 public class PaymentRegisterForm extends MasterPaymentForm implements ActionListener {: 
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1,000.00"
chkEmptyFields() : 2: txtFieldArray[UIDSEARCHFIELD].getText() : 
CLASS 
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
 public class PaymentRegisterForm extends MasterPaymentForm implements ActionListener {: 
chkEmptyFields() : true: returns : 
CLASS 
        at java.lang.Double.parseDouble(Double.java:510)
 public class PaymentRegisterForm extends MasterPaymentForm implements ActionListener {: 
        at view.content.entities.payment.PaymentRegisterForm.validateBtnAction(PaymentRegisterForm.java:1423)
validateBtnAction() : 2: txtFieldArray[UIDSEARCHFIELD].getText() : 
        at view.content.entities.payment.PaymentRegisterForm.actionPerformed(PaymentRegisterForm.java:728)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6288)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6053)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4651)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4481)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4481)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
        at java.awt.EventQueue.access$000(EventQueue.java:84)
        at java.awt.EventQueue$1.run(EventQueue.java:602)
        at java.awt.EventQueue$1.run(EventQueue.java:600)
        at java.security.AccessController.doPrivileged(Native Method)

1) admin_pay_rate DECIMAL(5,2)

returns by defauld 99,999.99 that top, otherwise cut all numeric possitions
1,000,000.99 is there 00,000.99, remove that because that is ***

2) for formating is there

String.format("%.2f", myNumber);

or

DecimalFormat decFormat = new DecimalFormat("0.00");

or

BigDecimal bd = new BigDecimal(d);
bd = bd.round(new MathContext(3));


remove that, beacuse that is ***

3) I don't understood another method, is is there real Numbers then returns
Double instance, if not sure then BigDecimal (create both methods, if Double overflowed returns false, repeat code for return value in BigDecimal)

4) why substring from something, test if is instanceof Number, write own try-catch for cast Number instance to Double,

5) and another :-), but the rest aren't significant

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.