Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;

public class Main {
    public static void main(String[] args) throws Exception {
        MaskFormatter formatter = new MaskFormatter("###-##-####");
        JFormattedTextField tf = new JFormattedTextField(formatter);
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(tf, BorderLayout.NORTH);
        JButton clickBtn = new JButton("Click me!");
        clickBtn.addActionListener(e -> {
            if (!tf.getText().matches(formatter.getMask())) {
                System.err.println("Your Input does not match the pattern!");
            } else {
                System.out.println(tf.getText());
            }
        });
        panel.add(clickBtn, BorderLayout.SOUTH);
        JFrame f = new JFrame();

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(panel, BorderLayout.CENTER);
        f.setSize(800, 600);
        f.setVisible(true);
    }
}