Java JFormattedTextField date field
import java.awt.FlowLayout; import java.util.Date; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class Main extends JFrame { public Main() { super("JButton"); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout()); JLabel nameLabel = new JLabel("Value:"); //from w ww . jav a 2 s .c om JFormattedTextField name = new JFormattedTextField(); name.setValue(new Date()); JTextField text = new JTextField("Click here to see the validation result"); getContentPane().add(nameLabel); getContentPane().add(name); getContentPane().add(text); } public static void main(String[] args) { Main frame = new Main(); frame.pack(); frame.setVisible(true); } }
Java JFormattedTextField create date input field for input like "12/31/2020"
import java.awt.FlowLayout; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.text.DateFormatter; public class Main extends JFrame { public Main() { super("JButton"); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout()); JLabel nameLabel = new JLabel("Value:"); /*from w ww . j a va 2s .c om*/ DateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY"); DateFormatter dateFormatter = new DateFormatter(dateFormat); JFormattedTextField name = new JFormattedTextField(dateFormatter); name.setValue(new Date()); JTextField text = new JTextField("Click here to see the validation result"); getContentPane().add(nameLabel); getContentPane().add(name); getContentPane().add(text); } public static void main(String[] args) { Main frame = new Main(); frame.pack(); frame.setVisible(true); } }
Java JFormattedTextField create date value input field for input like "2020-12-31" via SimpleDateFormat
import java.awt.FlowLayout; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class Main extends JFrame { public Main() { super("JButton"); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout()); JLabel nameLabel = new JLabel("Value:"); //from w w w .j a v a 2 s. c o m JFormattedTextField name = new JFormattedTextField(new SimpleDateFormat( "yyyy-MM-dd")); name.setValue(new Date()); Date date = (Date) tft3.getValue(); JTextField text = new JTextField("Click here to see the validation result"); getContentPane().add(nameLabel); getContentPane().add(name); getContentPane().add(text); } public static void main(String[] args) { Main frame = new Main(); frame.pack(); frame.setVisible(true); } }
Java JFormattedTextField create date value input field via DateFormat
import java.awt.FlowLayout; import java.text.DateFormat; import java.util.Date; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class Main extends JFrame { public Main() { super("JButton"); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout()); JLabel nameLabel = new JLabel("Value:"); /*w ww .j a v a2s . co m*/ JFormattedTextField name = new JFormattedTextField( DateFormat.getDateInstance(DateFormat.SHORT)); name.setValue(new Date()); JTextField text = new JTextField("Click here to see the validation result"); getContentPane().add(nameLabel); getContentPane().add(name); getContentPane().add(text); } public static void main(String[] args) { Main frame = new Main(); frame.pack(); frame.setVisible(true); } }