Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8359667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:13:20+00:00 2026-06-09T11:13:20+00:00

I have a ‘formatted’ field – that is that it must be finally in

  • 0

I have a ‘formatted’ field – that is that it must be finally in a form like this: xx/xxxx/xx
I’d like to make it so that while typing you get the ‘/’ added automatically.

The way that I am trying to cobble together is something like this:

JTextField field = new JTextField ("xx/xxxx/xx");

// a focus listener to clear the "xx/xxxx/xx" on focus & restore on focus-out
// the override the 'document' with this:
field.setDocument (new PlainDocument () {
    public void insertString (int off, String str, AttributeSet attr) throws BadLocationException {
      if (off == 2 || off == 7) {
        super.insertString (off + 1, str + "/", attr);
      }
    }
}

This seems like it is going to break – and how do I properly deal with when it goes from: xx/xx.. to xx? I think having them delete the ‘/’ is ok.

I feel there should be a better way? Maybe a library I could use? Something other than my…special stuff.

Thanks for any input you have!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:13:22+00:00Added an answer on June 9, 2026 at 11:13 am

    To achieve this I did this:

    JTextField field = new JTextField ();
    
    field.setDocument (new PlainDocument () {
      public void insertString (int off, String str, AttributeSet attr) throws BadLocationException {
        if (off < 10) {  // max size clause
          if (off == 1 || off == 6) { // insert the '/' occasionally
            str = str + "/";
          }
          super.insertString (off, str, attr);
        }
      }
    });
    
    field.setText ("xx/xxxx/xx"); // set AFTER otherwise default won't show up!
    field.setForeground (ColorConstants.DARK_GRAY_080); // make it light! 
    field.addFocusListener (new ClearingFocusListener (field)); // could be done in an anonymous inner class - but I use it other places
    
    private static class ClearingFocusListener implements FocusListener {
      final private String initialText;
      final private JTextField field;
    
      public ClearingFocusListener (final JTextField field) {
        this.initialText = field.getText ();
        this.field = field;
      }
    
      @Override
      public void focusGained (FocusEvent e) {
        if (initialText.equals (field.getText ())) {
          field.setText ("");
          field.setForeground (ColorConstants.DARK_GRAY_080);
        }
      }
    
      @Override
      public void focusLost (FocusEvent e) {
        if ("".equals (field.getText ())) {
          field.setText (initialText);
          field.setForeground (ColorConstants.LIGHT_GRAY_220);
        }
      }
    }
    

    This is different that the other solution in that the ‘/’ doesn’t exist when there is no text, it is added at the right places. It doesn’t currently doesn’t deal with any of the replacement stuff — eh. :/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have an XML file like this: <VS> <Fields> <Field Id=$1*>Column1</Field> <Field Id=$2*>Column2</Field> <Field Id=$3*>Column3</Field>
have a php code like this,going to convert it in to C#. function isValid($n){
Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',
Have some dates in my local Oracle 11g database that are in this format:
have a small javscript that users can include into their sites like so: <script
Have a digital media device that I'd like to get the ImageMagick command-line tool
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.