/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.ParseException;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author akulyakh
 */
public class FormattedFieldTest implements FocusListener, ActionListener {
    private JFrame frame;
    private MaskFormatter formatter;
    private JFormattedTextField fmtFld1;
    private MaskFormatter formatter2;
    private JFormattedTextField fmtFld2;
    private JButton button;
    
    public FormattedFieldTest() {
        
        try {
            
            SwingUtilities.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    
                    try {
                        createUI();
                    } catch(Exception x) {
                        x.printStackTrace();
                        fatal();
                    }
                }
                
            });
            
        } catch(Exception x) {
            x.printStackTrace();
            fatal();
        }
    }
    
    public void createUI() throws Exception {
        frame = new JFrame();
        frame.setSize(300, 100);
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        formatter = new MaskFormatter("##-##-##");
        fmtFld1 = new JFormattedTextField(formatter);
        fmtFld1.addFocusListener(this);
        frame.add(fmtFld1);
        
        
        fmtFld2 = new JFormattedTextField(new Integer(32767));
        fmtFld2.addFocusListener(this);
        frame.add(fmtFld2);
        
        button = new JButton("Get val");
        button.addActionListener(this);
        frame.add(button);
        
        
        frame.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        FormattedFieldTest test = new FormattedFieldTest();
        
        
    }
    
    public void fatal() {
        System.err.println("fatal error");
        System.exit(1);
    }

    @Override
    public void focusGained(FocusEvent fe) {
    }

    @Override
    public void focusLost(FocusEvent fe) {
        Object src = fe.getSource();
        if(src.equals(fmtFld1)) {
            log("focus lost for fmtFld1 val="+ fmtFld1.getValue());
        }
        else if(src.equals(fmtFld2)) {
            log("focus lost for fmtFld2 val="+ fmtFld2.getValue());
        }
    }

    private void log(String string) {
        System.out.println(string);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        log("fld1=" + fmtFld1.getValue());
        log("fld2=" + fmtFld2.getValue());
    }
}
