JFormattedTextFieldDateInputSampleDateFormatFULLLocaleUS.java Source code

Java tutorial

Introduction

Here is the source code for JFormattedTextFieldDateInputSampleDateFormatFULLLocaleUS.java

Source

import java.awt.FlowLayout;
import java.text.DateFormat;
import java.text.Format;
import java.util.Date;
import java.util.Locale;

import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JFormattedTextFieldDateInputSampleDateFormatFULLLocaleUS {
    public static void main(String args[]) {
        JFrame frame = new JFrame("Date/Time Input");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel label;
        JFormattedTextField input;
        JPanel panel;

        BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
        frame.setLayout(layout);

        Format fullUSDate = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
        label = new JLabel("Full US date:");
        input = new JFormattedTextField(fullUSDate);
        input.setValue(new Date());
        input.setColumns(20);

        panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel.add(label);
        panel.add(input);

        frame.add(panel);
        frame.add(new JTextField());
        frame.pack();
        frame.setVisible(true);
    }
}