Results 1 to 2 of 2
  1. #1
    Ralphw is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default JFormattedTextField + SimpleDateFormat

    Hi *

    i am creating a Dateformat with "YYYY" a set the Dateformatter into the JFormattedTextField.

    I expected a text field where i can only edit 4 digits.
    But i can put in even more digits.

    Whats wrong ?

    XML Code:
    import java.awt.BorderLayout;
    import java.text.SimpleDateFormat;
     
    import javax.swing.JFormattedTextField;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.text.DateFormatter;
     
    public class JFormattedTF extends JFrame {
     
      public JFormattedTF() {
        JPanel panel = new JPanel();
        JLabel label = new JLabel("Year :");
        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
        DateFormatter df = new DateFormatter(yearFormat);
        JFormattedTextField tf = new JFormattedTextField(df);
        ((DateFormatter)tf.getFormatter()).setAllowsInvalid(false);
        tf.setColumns(4);
        panel.add(label);
        panel.add(tf);
        getContentPane().add(panel, BorderLayout.SOUTH);
        pack();
      }
     
      public static void main(String[] args) {
        JFormattedTF tfe = new JFormattedTF();
        tfe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tfe.setVisible(true);
      }
    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    Hi,
    I think no methods are avilable in the api.You have to check like this below

    if(tf.getText().length() > 4 )

    -Regards
    Ramya
    Ramya:cool:

Similar Threads

  1. How to use SimpleDateFormat in J2ME Applications
    By thirupathik in forum CDC and Personal Profile
    Replies: 9
    Last Post: 08-13-2010, 08:18 PM
  2. Locale with SimpleDateFormat
    By swati.jyoti in forum New To Java
    Replies: 4
    Last Post: 07-02-2009, 02:34 PM
  3. Problems with JFormattedTextField
    By Gatts79 in forum New To Java
    Replies: 0
    Last Post: 10-03-2008, 04:59 PM
  4. Using SimpleDateFormat
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-18-2008, 08:33 AM
  5. JFormattedTextField Issue
    By teracomp in forum AWT / Swing
    Replies: 1
    Last Post: 01-21-2008, 06:55 PM

Posting Permissions

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