Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Swing - Beginner... stuck on how to use FormattedFields in java for inserting dates

    I know the below code is supposed to format the FormattedTextField but it doesn't seem to do anything to the form (shown in the picture below). I was wanting, when the form loaded: the text field to look something like this

    00/00/2014 - 00:00 am


    Where the user is able to

    - enter datelike information around the symbol separators ( / or : )

    - But where the user could not remove these symbol separators

    Code:

    Code:
     package datefield;
        
        import javax.swing.JFormattedTextField;
        import javax.swing.text.DateFormatter;
        
        
        public class NewJFrame extends javax.swing.JFrame {
            
            public NewJFrame() {
                initComponents();
                formattedTextField();
            }
     
    public void formattedTextField()
       {
            jFormattedTextField1 = new JFormattedTextField(new SimpleDateFormat("MM/dd/yy - mm:HH"));
            jFormattedTextField1.setValue(new Date());
            add(jFormattedTextField1);
       }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Can't see the image, upload a copy here.
    I'll double check it, but it sounds like you want to make use of a MaskFormatter. I just cannot recall offhand if the MaskFormatter allows the removal of the typed chars or not.

    Edit:
    Yep, maskformatter does what you want:
    PHP Code:
    try
    {
        
    MaskFormatter mf = new MaskFormatter("##/##/## - ##:##");
        
    mf.install(jFormattedTextField1);
    }
    catch (
    ParseException ex)
    {
        
    e.printStackTrace(System.err);

    I assume your format was supposed to be HH:mm and not mm:HH.
    Last edited by Fou-Lu; 02-11-2014 at 03:37 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •