Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2005

Tip: Looking for answers? Try searching our database.

JformattedTextField DateFormat

Thread view: 
Hans E. Kristiansen - 26 Dec 2005 03:28 GMT
Dear al,

I have a really irritating problem:

When I run the following code, I can correctly edit all the fields created
by maskformatter, but the dateformat field is always in insert mode, and it
does not display the format characters properly. I am using JDK Version 5.0
release 6, but the problem exists in 1.4.2 as well. It makes the date format
field rather useless. Note that the date is displayed in the correct format,
but when editing it behaves like a plain text field.

Thanks a million for the help.

import javax.swing.*;
import javax.swing.text.DateFormatter;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.text.ParseException;
import java.text.DateFormat;
import java.text.Format;
import java.util.Date;
import java.util.Locale;

/**
* Created by IntelliJ IDEA.
* User: User
* Date: Dec 26, 2005
* Time: 10:02:13 AM
* To change this template use File | Settings | File Templates.
*/

public class FormattedSample extends JFrame {

   public FormattedSample ()  throws ParseException {
       super("JFormattedTextField Sample");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Container content = getContentPane();
       content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));

       // US Social Security number
       MaskFormatter mf1 = new MaskFormatter("###-##-####");
       mf1.setPlaceholderCharacter('_');
       JFormattedTextField ftf2 = new JFormattedTextField(mf1);
       content.add(ftf2);

       // Four-digit year, followed by month name and day of month,
       // each separated by two dashes (--)
       Format format = DateFormat.getDateInstance(DateFormat.MEDIUM);
       // format.setLenient(false);
       // new SimpleDateFormat("yyyy--MMMM--dd");
       DateFormatter df = new
DateFormatter(DateFormat.getDateInstance(DateFormat.MEDIUM));
       JFormattedTextField ftf1 = new JFormattedTextField(df);
       ftf1.setValue(new Date());
       content.add(ftf1);

       // US telephone number
       MaskFormatter mf2 = new MaskFormatter("(###) ###-####");
       JFormattedTextField ftf3 = new JFormattedTextField(mf2);
       content.add(ftf3);
       // f.setSize(300, 100);
       pack();
       setVisible(true);
       setLocationRelativeTo(null);
   }

     public static void main (String args[])  throws ParseException {

       // Where are we?
       Locale.setDefault(Locale.UK);

       //Schedule a job for the event-dispatching thread:
       //creating and showing this application's GUI.
       javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               //createAndShowGUI();

               // If we got this far, then we can start main.
               try {
               new FormattedSample();
               } catch (ParseException e) {System.err.println("Error:
"+e.getMessage()); }
           }
       });
   }
 }
Ramses the King - 29 Dec 2005 10:06 GMT
Hans E. Kristiansen ha scritto:
> Dear al,
>
[quoted text clipped - 82 lines]
>     }
>   }

Hi! Sorry for my bad english!
So...the class MaskFormatter must be contained in a cycle "try - catch",
 if it does not come executed correctly.!

Bye

Andrea


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.