Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By JosAH

Thread: How to set Jtextfield Maxlength?..

  1. #1
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Arrow How to set Jtextfield Maxlength?..

    I have coded my password field's length like this. But i wanna set the password field to 8 characters, user can't write anything bigger than 8 characters. how can i do that ? is there anything something like "textfield maxlength" in jtextfield proporties ??? Waiting for you help...
    Java Code:
     
        String password = txtPassword.getText().trim();
        int passwordLength = sifre.length();
        ...
        ...
        else if(passwordLength != 8 ) {
             JOptionPane.showMessageDialog(null, "Your password must be 8 characters!.."); 
             boolean requestFocusInWindow = txtPassword.requestFocusInWindow();
        }
        ... 
        ...
    Best Regards...
    Last edited by qwerty53; 07-21-2011 at 11:32 AM. Reason: grammar and some mistakes

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by qwerty53 View Post
    I have coded my password field's length like this. But i wanna set the password field to 8 characters, user can't write anything bigger than 8 characters. how can i do that ? is there anything something like "textfield maxlength" in jtextfield proporties ??? Waiting for you help...
    Java Code:
     
        String password = txtPassword.getText().trim();
        int passwordLength = sifre.length();
        ...
        ...
        else if(passwordLength != 8 ) {
             JOptionPane.showMessageDialog(null, "Your password must be 8 characters!.."); 
             boolean requestFocusInWindow = txtPassword.requestFocusInWindow();
        }
        ... 
        ...
    Best Regards...
    A Document object manages the text entered in the JTextField; add a DocumentListener to that object and make it check for the length of the text; if it is too long, remove the offending char from the Document.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    There are also document filters.

  4. #4
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default

    the declaration of the buttons are in initComponents part. they can't be changed...

  5. #5
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default

    Java Code:
      @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            mainPanel = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jLabel5 = new javax.swing.JLabel();
            jLabel6 = new javax.swing.JLabel();
            jLabel8 = new javax.swing.JLabel();
            jLabel9 = new javax.swing.JLabel();
            jLabel10 = new javax.swing.JLabel();
            jLabel11 = new javax.swing.JLabel();
            jLabel12 = new javax.swing.JLabel();
            jLabel13 = new javax.swing.JLabel();
            jLabel14 = new javax.swing.JLabel();
            jLabel16 = new javax.swing.JLabel();
            jLabel17 = new javax.swing.JLabel();
            txtKullaniciAdi = new javax.swing.JTextField();
            txtOgrenciNumarasi = new javax.swing.JTextField();
            txtOgrenciAdi = new javax.swing.JTextField();
            txtVerildigiTarih = new javax.swing.JTextField();
            txtOgrenciSoyadi = new javax.swing.JTextField();
            txtGecerlilikTarihi = new javax.swing.JTextField();
            txtSifre = new javax.swing.JTextField();
            btnBulOgrenciAdi = new javax.swing.JButton();
            btnYazdir = new javax.swing.JButton();
            btnBulOgrenciNo = new javax.swing.JButton();
            txtEpostaAdresi = new javax.swing.JTextField();
            jLabel7 = new javax.swing.JLabel();
            btnBulOgrenciNo1 = new javax.swing.JButton();
            btnTemizle = new javax.swing.JButton();

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by qwerty53 View Post
    the declaration of the buttons are in initComponents part. they can't be changed...
    Who said something about changing those buttons?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default

    so how can i use DocumentListener ???

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by qwerty53 View Post
    so how can i use DocumentListener ???
    That's what the API documentation is for. Also read about the DocumenFilter class (as was suggested by Pbrockway2)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default

    it seems too long... there must be an another way.
    Last edited by qwerty53; 07-21-2011 at 02:10 PM.

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

    Default

    Then maybe you should give up and find a different hobby/career.

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Don't attempt something remotely complex if you want a simple solution.

    Don't attempt Algebra if you only want to use basic math.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  12. #12
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Then maybe you should give up and find a different hobby/career.
    really ? you smart a..

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

    Default

    I don't know, maybe it's just me but:
    "it seems too long"
    sounds very close to:
    "but it's too hard"

    You've been given the answer to what needs to be done (and how it's done in Swing).
    That you think it's too difficult in some way implies this isn't the job for you.

  14. #14
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default

    i don't need philosophical sentences, i just want a simple example. So i ll learn what to do... Oh Gooood!..
    Last edited by qwerty53; 07-21-2011 at 10:55 PM.

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    i just want a simple example
    @OP: Good luck finding it. And thanks for the question: if I had known about DocumentFilter before, I had forgotten about it.

    @World: Is this a modern tendency, to introduce everything with "hafta" or "juswanna"?

    The first denies the essential contingency of everything and ignores the fact that whatever we do involves choice and decision. The second pretends that our actions do not have consequence and entail nothing.

    It is a cheap rhetorical reflex whereby the speaker aims to obtain agreement either by claiming that something is inevitable (the haftas) or a small matter that it would be churlish to reject (the juswannas). With uncritical repetition the rhetoric becomes ingrained in both speaker and listener as an idle and irresponsible philosophy.
    Last edited by pbrockway2; 07-22-2011 at 01:27 AM.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by qwerty53 View Post
    it seems too long... there must be an another way.
    Of course there is a much simpler way but we are complete bastards and do not what to share the secret with a schmuck like you. Therefore the much harder solution was suggested. So you can either bite the bullet and do it the hard way or study lots, become more proficient and experienced and then after ~10 years when you are a Java Guru somebody MIGHT tell you how to do it the easy way.

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Junky View Post
    Of course there is a much simpler way but we are complete bastards and do not what to share the secret with a schmuck like you. Therefore the much harder solution was suggested. So you can either bite the bullet and do it the hard way or study lots, become more proficient and experienced and then after ~10 years when you are a Java Guru somebody MIGHT tell you how to do it the easy way.
    And he has to do the secret handshake and the silly dance! I'll go and fetch the syrup and feathers.

    kind regards,

    Jos ;-)
    Dark likes this.
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    And he has to do the secret handshake and the silly dance! I'll go and fetch the syrup and feathers.

    kind regards,

    Jos ;-)
    The syrup and feathers have left me scarred for life. I should've just done it the hard way.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Help - maxLength method for 2D Array
    By Hardo in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 05:28 PM
  2. JTextField
    By gancio in forum AWT / Swing
    Replies: 20
    Last Post: 08-26-2009, 04:11 PM
  3. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  4. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 05:25 PM
  5. help with JTextfield
    By gary in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 02:58 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
  •