Java tutorial
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.math.RoundingMode; import java.text.NumberFormat; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; public class Main { JFrame frame = new JFrame(); JPanel pnl = new JPanel(); JLabel focusLabel = new JLabel(" focusLost Handle "); JFormattedTextField formTextField; JLabel docLabel = new JLabel(" document Handle "); JFormattedTextField formTextField1; NumberFormat formTextFieldFormat; double amount = 10000.00; public Main() { formTextFieldFormat = NumberFormat.getNumberInstance(); formTextFieldFormat.setMinimumFractionDigits(2); formTextFieldFormat.setMaximumFractionDigits(2); formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP); focusLabel.setPreferredSize(new Dimension(120, 27)); formTextField = new JFormattedTextField(formTextFieldFormat); formTextField.setValue(amount); formTextField.setPreferredSize(new Dimension(120, 27)); formTextField.setHorizontalAlignment(SwingConstants.RIGHT); formTextField.addFocusListener(new FocusListener() { @Override public void focusGained(FocusEvent e) { formTextField.requestFocus(); formTextField.setText(formTextField.getText()); formTextField.selectAll(); } @Override public void focusLost(FocusEvent e) { double t1a1 = (((Number) formTextField.getValue()).doubleValue()); if (t1a1 < 1000) { formTextField.setValue(amount); } } }); docLabel.setPreferredSize(new Dimension(120, 27)); formTextField1 = new JFormattedTextField(formTextFieldFormat); formTextField1.setValue(amount); formTextField1.setHorizontalAlignment(SwingConstants.RIGHT); formTextField1.addFocusListener(new FocusListener() { @Override public void focusGained(FocusEvent e) { formTextField1.requestFocus(); formTextField1.setText(formTextField1.getText()); formTextField1.selectAll(); } @Override public void focusLost(FocusEvent e) { } }); formTextField1.getDocument().addDocumentListener(docListener); pnl = new JPanel(); pnl.setBorder(new EmptyBorder(2, 2, 2, 2)); pnl.setLayout(new GridLayout(2, 2)); pnl.add(focusLabel); pnl.add(formTextField); pnl.add(docLabel); pnl.add(formTextField1); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(pnl, BorderLayout.CENTER); frame.setLocation(200, 200); frame.pack(); frame.setVisible(true); formTextFieldFocus1(); } DocumentListener docListener = new DocumentListener() { @Override public void changedUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } @Override public void insertUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } @Override public void removeUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } void printIt(DocumentEvent documentEvent) { DocumentEvent.EventType type = documentEvent.getType(); double t1a1 = (((Number) formTextField1.getValue()).doubleValue()); if (t1a1 < 100) { formTextField1.setValue(amount); } } }; void formTextFieldFocus1() { formTextField1.grabFocus(); formTextField1.requestFocus(); formTextField1.setText(formTextField1.getText()); formTextField1.selectAll(); } void formTextFieldFocus() { formTextField.grabFocus(); formTextField.requestFocus(); formTextField.setText(formTextField.getText()); formTextField.selectAll(); formTextFieldFocus1(); } public static void main(String args[]) { Main fl = new Main(); } }