Results 1 to 15 of 15
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default checking text input

    I thought the next code would be a simple way of dealing with non int input,
    when not int just dump it:

    Java Code:
     private class TextFieldListener implements DocumentListener  {
            public void insertUpdate(DocumentEvent e) {
                 try { // the room field only
                            int pn = Integer.parseInt(panelnumber.getText().trim());
                            System.out.println(pn);
                     }
                    catch(NumberFormatException nFE) {
                            System.out.println( "not integer");
                            panelnumber.setText(""); // java does not like this see error message
                            panelnumber.requestFocus();
                            
                    }
            }
            public void removeUpdate(DocumentEvent e) {
            }
            public void changedUpdate(DocumentEvent e){
            }
       }
    Java protests:

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification
    at javax.swing.text.AbstractDocument.writeLock(Abstra ctDocument.java:1323)
    at javax.swing.text.AbstractDocument.replace(Abstract Document.java:644)
    at javax.swing.text.JTextComponent.setText(JTextCompo nent.java:1693)
    at roomassigment.Main$TextFieldListener.insertUpdate( Main.java:187)
    at javax.swing.text.AbstractDocument.fireInsertUpdate (AbstractDocument.java:185)
    at javax.swing.text.AbstractDocument.handleInsertStri ng(AbstractDocument.java:734)
    at javax.swing.text.AbstractDocument.insertString(Abs tractDocument.java:693)
    at javax.swing.text.PlainDocument.insertString(PlainD ocument.java:114)
    at javax.swing.text.AbstractDocument.replace(Abstract Document.java:655)
    at javax.swing.text.JTextComponent.replaceSelection(J TextComponent.java:1351)
    at javax.swing.text.DefaultEditorKit$DefaultKeyTypedA ction.actionPerformed(DefaultEditorKit.java:864)
    at javax.swing.SwingUtilities.notifyAction(SwingUtili ties.java:1645)
    at javax.swing.JComponent.processKeyBinding(JComponen t.java:2859)
    at javax.swing.JComponent.processKeyBindings(JCompone nt.java:2894)
    at javax.swing.JComponent.processKeyEvent(JComponent. java:2822)
    at java.awt.Component.processEvent(Component.java:615 9)
    at java.awt.Container.processEvent(Container.java:208 3)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4744)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2141)
    at java.awt.Component.dispatchEvent(Component.java:45 72)
    at java.awt.KeyboardFocusManager.redispatchEvent(Keyb oardFocusManager.java:1856)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEv ent(DefaultKeyboardFocusManager.java:722)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKe yEvent(DefaultKeyboardFocusManager.java:1000)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAsse rtions(DefaultKeyboardFocusManager.java:865)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent (DefaultKeyboardFocusManager.java:686)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4616)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2141)
    at java.awt.Window.dispatchEventImpl(Window.java:2489 )
    at java.awt.Component.dispatchEvent(Component.java:45 72)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:710)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:669)
    at java.awt.EventQueue$2.run(EventQueue.java:667)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:683)
    at java.awt.EventQueue$3.run(EventQueue.java:681)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 680)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: checking text input

    panelnumber is a JTextField

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: checking text input

    Exactly what it says.
    You can't change the contents of the text field inside the event handler for changes to that text field.
    You would get stack overflows from an infinite recursion.
    So it prevents that from happening by throwing an exception.

    Look into using a JFormattedTextField and an InputVerifier.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: checking text input

    ... or DocumentFilter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: checking text input

    This is complicated stuff and I need some more study time.
    Here is the best I could figure out for now.
    Is there a way to force the focus to stay on a particular JTextField,
    until the user inserted an integer?


    Java Code:
    private class TextFieldListener implements DocumentListener  {
            public void insertUpdate(DocumentEvent e) {
                 try { 
                            roomnumber =
                               Integer.parseInt(panelnumber.getText().trim());
                     }
                    catch(NumberFormatException nFE) {
                            JOptionPane.showMessageDialog(null, "NOT AN INTEGER");
                            panelnumber.requestFocusInWindow();
                    }
                    try { 
                            xgrid =
                               Integer.parseInt(xgridnumber.getText().trim());
    
                    }
                    catch(NumberFormatException nFE)   {
                            JOptionPane.showMessageDialog(null, "NOT AN INTEGER");
                            xgridnumber.requestFocusInWindow();
                    }
                    try { 
                            ygrid =
                               Integer.parseInt(ygridnumber.getText().trim());
                     }
                    catch(NumberFormatException nFE) {
                            JOptionPane.showMessageDialog(null, "NOT AN INTEGER");
                            ygridnumber.requestFocusInWindow();
                    }
            }
            public void removeUpdate(DocumentEvent e) {
            }
            public void changedUpdate(DocumentEvent e){
            }
       }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: checking text input

    Quote Originally Posted by willemjav View Post
    Is there a way to force the focus to stay on a particular JTextField,
    until the user inserted an integer?
    Did you read response #3 by Tolls? Did you try to follow the advice given?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: checking text input

    Yes, but it is too much info so I need time to digest.....
    I found this useful link:

    InputVerifier (Java Platform SE 7 )

  8. #8
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: checking text input

    This is the code I was looking for:

    Java Code:
    import java.awt.*; 
    import java.util.*; 
    import java.awt.event.*; 
    import javax.swing.*;
    // This program demonstrates the use of the Swing InputVerifier class. 
    // It creates two text fields; the first of the text fields expects the 
    // string "pass" as input, and will allow focus to advance out of it 
    // only after that string is typed in by the user.
    public class VerifierTest extends JFrame { 
        public VerifierTest() {
            JTextField tf1 = new JTextField ("Type \"pass\" here"); 
            getContentPane().add (tf1, BorderLayout.NORTH); 
            tf1.setInputVerifier(new PassVerifier());
            
            JTextField tf2 = new JTextField ("TextField2"); 
            getContentPane().add (tf2, BorderLayout.SOUTH);
            
            WindowListener l = new WindowAdapter() {  
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            };
            addWindowListener(l);
        } 
        
    class PassVerifier extends InputVerifier {
        public boolean verify(JComponent input) {
            JTextField tf = (JTextField) input;
            return "pass".equals(tf.getText());
        }
    }
    
        public static void main(String[] args) {
            Frame f = new VerifierTest();
            f.pack();
            f.setVisible(true);
    
        }
    }

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: checking text input

    What would you need to change --getting a little exhausted here: so much to go over--
    here: return "pass".equals(tf.getText());
    so the verifier would check for an integer instead of the string "pass"?

    return int.equals(tf.getText()); something similar....

  10. #10
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: checking text input

    Wow that code up is so short and compact, and it works!
    But I do not understand why?
    Where one calls the important boolean verify method in the code.....
    really weird that it works
    Last edited by willemjav; 03-26-2013 at 06:45 PM.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: checking text input

    Well, you supply the Verifier to the JTextField.
    The code inside JTextField uses that to determine whether text entered by the user is valid or not.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: checking text input

    What about this, it works, but does it make since:

    Java Code:
    InputVerifier verifier = new InputVerifier() {
                public boolean verify(JComponent input) {
                    JTextField tf = (JTextField) input;
                    try {
                          Integer.parseInt(tf.getText().trim());
                          blockokbutton = true;
                          return true;
                    }
                    catch(NumberFormatException nFE) {
                            blockokbutton = false;
                            return false;
    
                    }
                }
        };

  13. #13
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: checking text input

    There are three textfields but the "equals" is not working...
    something is still wrong here, can't get the three fields like this

    Java Code:
    InputVerifier verifier = new InputVerifier() {
                public boolean verify(JComponent input) {
                    JTextField tf = (JTextField) input;
                    try {
                          System.out.println(tf);
                          int x = Integer.parseInt(tf.getText().trim());
                          blockokbutton = true;
                          if (tf.equals("panelnumber")) {      
                              roomnumber = x;
                          }   
                          if (tf.equals("xgridnumber"))
                              xgrid = x;
                          if (tf.equals("ygridnumber"))
                              ygrid = x;
                          return true;
                    }
                    catch(NumberFormatException nFE) {
                            blockokbutton = false;
                            return false;
    
                    }
                }
        };
    Last edited by willemjav; 03-26-2013 at 09:58 PM.

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: checking text input

    sure the Textfield ref does not show the name string right:

    javax.swing.JTextField[,95,0,50x28,layout=javax.swing.plaf.basic.BasicTex tUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,bo rder=com.apple.laf.AquaTextFieldBorder@18987a33,fl ags=288,maximumSize=java.awt.Dimension[width=50,height=28],minimumSize=,preferredSize=,caretColor=javax.swin g.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResourc e[r=128,g=128,b=128],editable=true,margin=javax.swing.plaf.InsetsUIRes ource[top=0,left=0,bottom=0,right=0],selectedTextColor=com.apple.laf.AquaImageFactory$ SystemColorProxy[r=0,g=0,b=0],selectionColor=com.apple.laf.AquaImageFactory$Sys temColorProxy[r=166,g=201,b=255],columns=3,columnWidth=12,command=,horizontalAlign ment=LEADING]

  15. #15
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: checking text input

    That was again silly! Of course I would do first this:

    Java Code:
    InputVerifier verifier = new InputVerifier() {
                public boolean verify(JComponent input) {
                    JTextField tf = (JTextField) input;
                    try {      
                          int x = Integer.parseInt(tf.getText().trim());                     
                          blockokbutton = true;
                          return true;
                    }
                    catch(NumberFormatException nFE) {
                            blockokbutton = false;
                            return false;
                    }
                }
        };
    than ones checked that no none int are entered I would just getText the fields....

Similar Threads

  1. Checking textfield text
    By jsobel in forum New To Java
    Replies: 4
    Last Post: 06-14-2012, 05:48 AM
  2. Replies: 1
    Last Post: 11-14-2011, 02:03 PM
  3. checking entered text in a text box?
    By nickburris in forum New To Java
    Replies: 2
    Last Post: 06-24-2011, 03:09 AM
  4. Checking if enter has been pressed on a text field
    By BoomPony in forum New To Java
    Replies: 1
    Last Post: 01-28-2011, 04:18 PM
  5. Need help with checking user input
    By hmwtd in forum Java Gaming
    Replies: 0
    Last Post: 10-28-2010, 11:07 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
  •