Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Beginner... stuck on how to use FormattedFields in java for inserting dates

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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:
      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);
       }

    sO3em.jpg


  2. #2
    Junior Member
    Join Date
    Feb 2014
    Location
    Philippines
    Posts
    12
    My Mood
    Tired
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Beginner... stuck on how to use FormattedFields in java for inserting dates

    hello,

    As per my understanding of the problem, you want the user to input a date on the text field then as he/she type the text is automatically converted to the format that you want?

    regards

    [edit]

    sorry i think i get what you mean now,

    i think you could use a MaskFormatter

    JFormattedTextField jFormattedTextField1 = new JFormattedTextField(new MaskFormatter("##'/##'/## '- ##':##"));

    just make sure the initial value will fit in the specified mask criteria

    source :
    http://docs.oracle.com/javase/7/docs...Formatter.html

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner... stuck on how to use FormattedFields in java for inserting dates

    How do I use the above code, when I try to execute it from within a button nothing happens.


    What I was hoping for, was as soon as the form loaded three backslashes where locked into place in the jFormatedTextField wherein the user could type a date around them e.g. 10/10/2014

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Location
    Philippines
    Posts
    12
    My Mood
    Tired
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Beginner... stuck on how to use FormattedFields in java for inserting dates

    using your code above

     
            jFormattedTextField1 = new JFormattedTextField(new SimpleDateFormat("MM/dd/yy - mm:HH"));
            jFormattedTextField1.setValue(new Date());
            add(jFormattedTextField1);

    make some alterations

    initialize your : jFormattedTextField1 like so
    JFormattedTextField jFormattedTextField1 = new JFormattedTextField(new MaskFormatter("##'/##'/## '- ##':##"));

    is guess youd want to have initial value using
    jFormattedTextField1.setValue(new Date()); ?

    you have to make the for mat of new Date match the format of the mask using simpledateformater

    or else onload the textfield would be blank.

    if correctly applied

    it should have

    02/11/14 - 02:57

    note : the slash, dash and colon will not be deleted even if the user tries to. i already tried it with your code

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner... stuck on how to use FormattedFields in java for inserting dates

    Also posted at: Beginner... stuck on how to use FormattedFields in java for inserting dates

    http://forums.devshed.com/java-help-...ml#post2924572
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Beginner: For loop excercise (stuck)
    By ObedMarsh in forum Loops & Control Statements
    Replies: 4
    Last Post: September 8th, 2012, 07:37 PM
  2. [SOLVED] Beginner, stuck on implementing while loop, compiles fine but still won't run
    By GregC in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 1st, 2012, 06:36 PM
  3. how to merge java objects with effective dates
    By java4ulok in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 10th, 2011, 01:24 PM
  4. Beginner Needs Help Inserting row to table
    By MoniD in forum JDBC & Databases
    Replies: 5
    Last Post: March 10th, 2011, 02:15 PM
  5. Java/Excel integration, Reading in Dates
    By aussiemcgr in forum JDBC and Database Tutorials
    Replies: 0
    Last Post: July 16th, 2010, 08:38 AM