hi
I need to assign number formatting to a JFormattedTextField
(to define decimal digit and localization for the currency).
I was able to solve this problem
if I assign formatting when the JFormattedTextFieldis created:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.text.DefaultFormatterFactory;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JFormattedTextField.AbstractFormatterFactory;
import javax.swing.text.NumberFormatter;
/**
*
*
*/
public classTestTheNumFormatProblem {
/** Creates a new instance of testTheNumFormatProblem */
public TestTheNumFormatProblem() {
}
public static void main(String args[]) {
Finestra finestra = new Finestra();
}
} // class testTheNumFormatProblem
class Finestra extends JFrame{
JLabel jl;
JFormattedTextField jft ;
JButton jb = new JButton("but");
public Finestra(){
setLayout(new FlowLayout());
jl = new JLabel("Try with formatted text field ");
jft = setFormat(jft);
add(jb);
//
add(jl);
add(jft);
jft.setColumns(10);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setSize(400,300);
setVisible(true);
jb.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(jb, "nel text Box : " + jft.getText());
}
});
}
private JFormattedTextField setFormat(JFormattedTextField jft ){
DefaultFormatterFactory aff ;
NumberFormat numberFormat;
// Locale local = new Locale("it", "IT");
Locale local = new Locale("en", "US");
numberFormat = NumberFormat.getCurrencyInstance(local);
numberFormat.setMinimumFractionDigits(3);
jft = new JFormattedTextField(numberFormat);
jft.setValue(new Double(342.796));
return jft;
}
}
*But I have to assign formatting
after the JFormattedTextField is already created... *
(using some method like jFormattedTextField.setFormatterFactory(... )
In this case i am not able to do the program work good..
Any help or explanation to assign formatting in dynamic way is welcome
thank you
regards
tonyMrsangelo