/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.osm.components;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.JFormattedTextField.AbstractFormatterFactory;
import javax.swing.event.DocumentListener;
import javax.swing.text.InternationalFormatter;
/**
*
* @author Win7
*/
public class JAmountField extends javax.swing.JPanel {
private static final long serialVersionUID = 8913369762644440133L;
public String DefaultCurrency[] = {"$ USD", "BsF", "€ EU"};
private boolean isRequiredField = false;
private String currency = "$ USD";
/**
* Creates new form JAmountPanel
*/
public JAmountField() {
initComponents();
final JAmountField _this = this;
this.amountJFormattedTextField.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField field) {
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
format.setRoundingMode(RoundingMode.HALF_UP);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
formatter.setMinimum(0.0);
formatter.setMaximum(10000000.00);
return formatter;
}
});
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
amountJFormattedTextField = new javax.swing.JFormattedTextField();
currencyJLabel = new javax.swing.JLabel();
amountJFormattedTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
amountJFormattedTextField.setText("0.00");
currencyJLabel.setText("$ USD");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(amountJFormattedTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(currencyJLabel))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(amountJFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(currencyJLabel))
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JFormattedTextField amountJFormattedTextField;
private javax.swing.JLabel currencyJLabel;
// End of variables declaration//GEN-END:variables
public void addDocumentListener(DocumentListener listener) {
this.amountJFormattedTextField.getDocument().addDocumentListener(listener);
}
public boolean isIsRequiredField() {
return isRequiredField;
}
public void setIsRequiredField(boolean isRequiredField) {
this.isRequiredField = isRequiredField;
}
public String getUseCurrentSymbol() {
return this.currency = currency;
}
public void setUseCurrentSymbol(String currency) {
this.currency = currency;
this.currencyJLabel.setText(this.currency);
}
public void setValue(Double amount) {
this.amountJFormattedTextField.setValue(amount);
}
public Double getValue() {
return (Double) this.amountJFormattedTextField.getValue();
}
}