Home » Java » java – how to make a jtextfield having a fixed date format?-Exceptionshub

java – how to make a jtextfield having a fixed date format?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m having trouble creating a fixed date format using JTextField. Is there a way for JTextField to have a fixed date format ?

How to&Answers:

You can use JFormattedTextField with SimpleDateFormat

DateFormat format = new SimpleDateFormat("your_format");
JFormattedTextField dateTextField = new JFormattedTextField(format);

Answer:

You should take a look at

For starters…

Answer:

If you’re using Swing, add a JFormattedTextField to your JFrame. In the Properties, click on formatterFactory. In the dialog, choose the Date Category and then select a Format. Now your format will be enforced.

enter image description here

Answer:

my solution in Netbeans is : select the date from JDateChooser component ,then one unfocusable jtextfield get the date from the selected date in JDateChooser:
(1)in order to make fixed date format, in the properties of JDateChooser ,for example we set the date format :yyyy-MM-dd ;
(2)make “jtextfield.setFocusable(false)”, this is for avoiding input wrong date format or unwanted characters

enter image description here

enter image description here

enter image description here
enter image description here

Answer:

I think the best is using the JFormatedTextField.

I have this code try this one:

package your_package;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class.....{

private String getdate(){
      DateFormat format = new SimpleDateFormat("MM/DD/YYYY"); //display your format.
      Date date = new Date();//puts the date in variable.
      return dateformat.format(date); //returns the format to the date variable.
}

public your_app{
     .....
     String date = new getdate();
     txtDate.setvalue(date);
}
}

Hope this will give you an idea and helps… 🙂