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 / January 2005

Tip: Looking for answers? Try searching our database.

JFormattedTextField and editing contents

Thread view: 
Stuart Leonard - 17 Jan 2005 03:42 GMT
I have a JFormattedTextField which accepts only numeric input.  The custom
mask formatter contains the "#" character for the format string.  Works
great, however, I would like to "control" the behaviour of the edit.  The
default implementation uses a "beep" if the data input is invalid.  I would
like to extend the editing behaviour to include a "JOptionPane" error
message dialog, because not every user has an audible (speakers).
Stuart Leonard - 17 Jan 2005 04:00 GMT
I have tried trapping the invalid entry using either a custom Key listener
or a Document.
The problem is that the data has already been commited to the
JFormattedTextField
before these edits are invoked.  For example, if I enter an invalid
character, the event
does not occur.  If I press the tab key to change focus, the event is then
fired.
Following is the code that I am using to trap the event.

class FolderKeyEvent extends KeyAdapter
   {
    /** Handle the key pressed event from the text field. */
 public void keyPressed(KeyEvent event) {

    int key = event.getKeyCode();
    char textchar = event.getKeyChar();
    String keytext = event.getKeyText(key);
 System.out.println("keytext = " + keytext);
    if (textchar != '0' &&
     textchar != '1' )
    {
     Toolkit.getDefaultToolkit().beep();
        JOptionPane.showMessageDialog(f, "Enter a \"0\" or \"1\" only.");
        event.consume();
        return;

    }

    }
 }
   /**
 * Provides a <Document> object to ensure that only a "0" or "1"
 * are entered
 */

   class FolderDocument extends PlainDocument
   {
    public void insertString(int pos, String text,
              AttributeSet attr) throws BadLocationException
    {
     System.out.println("\"" + text + "\"");
        for (int i=0; i<text.length(); i++)
        {
            if (text.charAt(i) != '0'
             && text.charAt(i) != '1'
             && text.charAt(i) != ' ')

            {
                Toolkit.getDefaultToolkit().beep();
                JOptionPane.showMessageDialog(f, "Enter a \"0\" or \"1\"
only.");
                return;
            }
        }
        System.out.println("text = " + text);
        super.insertString(pos,text,attr);
    }
}

>I have a JFormattedTextField which accepts only numeric input.  The custom
>mask formatter contains the "#" character for the format string.  Works
>great, however, I would like to "control" the behaviour of the edit.  The
>default implementation uses a "beep" if the data input is invalid.  I would
>like to extend the editing behaviour to include a "JOptionPane" error
>message dialog, because not every user has an audible (speakers).
Roland - 21 Jan 2005 10:50 GMT
> I have a JFormattedTextField which accepts only numeric input.  The custom
> mask formatter contains the "#" character for the format string.  Works
> great, however, I would like to "control" the behaviour of the edit.  The
> default implementation uses a "beep" if the data input is invalid.  I would
> like to extend the editing behaviour to include a "JOptionPane" error
> message dialog, because not every user has an audible (speakers).

Create an (anonymous) subclass of JFormattedTextField and override
invalidEdit().
<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JFormattedTextField.html#inv
alidEdit
()>

Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \



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.