Results 1 to 19 of 19
  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default keyTyped is it correct

    Hello,

    I am trying to make a variant of the JTextField one that accepts only integers and is limited to 6 numbers only, the part where it is only integers i think is correct, but i want someone to tell me if my keyTyped method is correct here or not?

    Java Code:
    import javax.swing.JTextField;
    import java.awt.event.KeyEvent;
    
    public class JIntTextField extends JTextField
    {
        private int column;
        
        private final static String nonInt = "`~!@#$%^&*()_+=\\|\"':;?/>.<, ";
        
        public JIntTextField (int column)
        {
            super(column);
        }
        
        [B]public void keyTyped (KeyEvent e)
        {
            if (this.getText().length() > 6)
            {
                e.consume();
                return;
            }
        }[/B]
        
        public void processKeyEvent (KeyEvent e)
        {
            char c = e.getKeyChar();
            
            if ((Character.isLetter(c) && !e.isAltDown()) || nonInt.indexOf(c) > -1)
            {
                e.consume();
                return;
            }else{
                super.processKeyEvent(e);
            }
        }
    }
    Regards,
    Last edited by aizen92; 04-03-2011 at 12:46 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Sorry, but your plan has holes. The best way to do what you want is to use a DocumentFilter for this. Trust me. The Oracle Tutorials as a section on this that will help.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For example, please check out: limit-number-cols-jtextarea-again

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Heck, you've already been told to use this before, and very recently, yet you seem to be ignoring this good advice, and this begs the question -- why?

  5. #5
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Quote Originally Posted by Fubarable View Post
    Heck, you've already been told to use this before, and very recently, yet you seem to be ignoring this good advice, and this begs the question -- why?
    Yeh i forgot about the other thread, sry about that

    I read this link "http://download.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter", the thing is i should extend the DocumentFilter class but i already am extending the jtextfield, so is there like an alternative for it

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by aizen92 View Post
    Yeh i forgot about the other thread, sry about that

    I read this link "http://download.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter", the thing is i should extend the DocumentFilter class but i already am extending the jtextfield, so is there like an alternative for it
    Your statement here makes no sense, sorry. What is the issue? Are you concerned because you can't have one classes extend two classes, if so that's not a problem since you're talking about two completely separate classes, the JTextField and the filter. Why are you extending the JTextField class rather than just using a JTextField object?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    Yeh i forgot about the other thread, sry about that
    That is very rude; so you forgot about the answers, tips and hints given to you by the people who replied in that thread? And now, you're asking the same question again? We are no idiots you know and we have other (better) things to do than just being ignored and forgotten by you. I, for one, are going to put you on my ignore list right now. Good luck with it.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Iam trying to create a class that extends from text field since i need to create a text field that only accepts only integers as input and is limited to 6 integers only not more, becuase im making an address book, and i want to make it like a text field for entering the phone number which is only 6 ints

  9. #9
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    That is very rude; so you forgot about the answers, tips and hints given to you by the people who replied in that thread? And now, you're asking the same question again? We are no idiots you know and we have other (better) things to do than just being ignored and forgotten by you. I, for one, are going to put you on my ignore list right now. Good luck with it.

    Jos
    its not that i forgot about them, i read the link given to me, and i though it helped me, but then i faced another problem, and wanted to check if my code is correct, so clicked on new thread directly out of hurry

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by aizen92 View Post
    Iam trying to create a class that extends from text field since i need to create a text field that only accepts only integers as input and is limited to 6 integers only not more, becuase im making an address book, and i want to make it like a text field for entering the phone number which is only 6 ints
    Don't extend JTextField for this since these issues are not the textfield's responsibilities. Again all you have to do is use a standard JTextField whose PlainDocument has a DocumentFilter that does all this. Subclassing JTextField is just wrong for what you are trying to do.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    its not that i forgot about them, i read the link given to me, and i though it helped me, but then i faced another problem, and wanted to check if my code is correct, so clicked on new thread directly out of hurry
    That's a lame excuse; your piece of code above clearly shows that you didn't even attempt to use a DocumentFilter at all. You simply ignored the previous replier(s). *plonk*

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Quote Originally Posted by Fubarable View Post
    Don't extend JTextField for this since these issues are not the textfield's responsibilities. Again all you have to do is use a standard JTextField whose PlainDocument has a DocumentFilter that does all this. Subclassing JTextField is just wrong for what you are trying to do.
    so i make a class that extends from document filter and does the job, and set it to the text field ? or do i create a document filter object and pass it to the text field

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by aizen92 View Post
    so i make a class that extends from document filter and does the job, and set it to the text field ? or do i create a document filter object and pass it to the text field
    You may wish to look at the tutorial and read the links of examples from this forum that I've provided above.

  14. #14
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    iam reading the tutorial now and trying to see how i can apply it in my case

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  16. #16
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    ohh, ok now i got it
    in the tutorial it got me confused becuase of the text pane and the styled document, but i only should override the replace and the insertString methods

  17. #17
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    I wrote a class that extends from DocumentFilter and heres the code i got, is it right?

    Java Code:
    import javax.swing.text.*;
    import java.awt.Toolkit;
    
    public class DocumentSizeFilter extends DocumentFilter
    {    
        private final static String NUMBERS = "0123456789";
        private final static int MAX_NUMBERS = 6;
        
        public void insertString (FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException
        {
            if ((fb.getDocument().getLength() + str.length() <= MAX_NUMBERS && NUMBERS.contains(str)))
            {
                super.insertString(fb, offs, str, a);
            }else{
                Toolkit.getDefaultToolkit().beep();
            }
        }
        
        public void replace (FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException
        {
          if ((fb.getDocument().getLength() + str.length() - length) <= MAX_NUMBERS && NUMBERS.contains(str))
          {
              super.replace(fb, offs, length, str, a);
          }else{
              Toolkit.getDefaultToolkit().beep();
          }
        }
    }

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Test it, does it work?

    You can make it even more flexible by changing your constants to fields that are set by constructor parameters. This would allow you to use this class in different situations and with different requirements.

  19. #19
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    ahaa, ill test this class now and see if it works
    thnx for the help

    EDIT:

    I tried it and worked fine
    thnx for everyone for the help and sorry for anyone who was offended by my actions before
    Last edited by aizen92; 04-03-2011 at 01:59 PM.

Similar Threads

  1. Nullifying Keypress or KeyTyped Event
    By tomtraxler in forum New To Java
    Replies: 3
    Last Post: 01-09-2011, 01:19 PM
  2. About focusLost and keyTyped, need feedback
    By mine0926 in forum NetBeans
    Replies: 0
    Last Post: 06-26-2010, 10:08 AM
  3. Getting correct output
    By WarmRegards in forum New To Java
    Replies: 9
    Last Post: 11-01-2009, 04:41 PM
  4. Char comparison/assigning through keyTyped
    By AdamMurg in forum Java Applets
    Replies: 0
    Last Post: 10-21-2009, 05:42 PM
  5. Syntax correct?
    By dbashby in forum New To Java
    Replies: 5
    Last Post: 09-22-2009, 06:44 AM

Posting Permissions

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