-1

EDIT: I didn't ask for the syntax on how to set the number of characters. I can find that easily in a simple search. Please read and understand before downvoting someone. I was asking how to find where this is in the source code based ont he code below. I found it on my own using the "Reference" feature, which directed me to the other classes which called the JTextFieldLimit class.

END EDIT.

I'm trying to edit the source code for an app someone built for me. The login field is limited to 15 characters but I need to increase this. After some googling, the closest I've come so far is the code inside the JTextFieldLimit class, which is as follows:

package app;

import javax.swing.text.*;

//import javax.swing.text.*;
public class JTextFieldLimit extends PlainDocument {
  private int limit;
  // optional uppercase conversion
  private boolean toUppercase = false;

  JTextFieldLimit(int limit) {
   super();
   this.limit = limit;
   }

  JTextFieldLimit(int limit, boolean upper) {
   super();
   this.limit = limit;
   toUppercase = upper;
   }

  public void insertString
    (int offset, String  str, AttributeSet attr)
      throws BadLocationException {
   if (str == null) return;

   if ((getLength() + str.length()) <= limit) {
     if (toUppercase) str = str.toUpperCase();
     super.insertString(offset, str, attr);
     }
   }
}

Where do I go from here?

dbconfession
  • 1,147
  • 2
  • 23
  • 36
  • How do they limit the JTextField? You've got their source code. – Hovercraft Full Of Eels Jun 29 '14 at 03:34
  • 1
    Use a DocumentFilter instead – MadProgrammer Jun 29 '14 at 03:40
  • Sorry. That's exactly what my post is asking. How are they limiting the JTextField? I need direction on where to go from this code. I'm not versed in inheritance and Swing/SWT interface yet as I only started learning java recently. Thanks. – dbconfession Jun 29 '14 at 03:41
  • @ElliottFrisch No offence, but there are better solutions available for achieving this, extending the document is not a suitable solution in all situations and would be better achieved through the use of a `DocumentFilter` - just saying – MadProgrammer Jun 29 '14 at 03:47
  • @MadProgrammer I think that's a fantastic answer, and if you care to post on the other question I'll vote it up! – Elliott Frisch Jun 29 '14 at 03:48
  • @ElliottFrisch :P Hadn't thought of that – MadProgrammer Jun 29 '14 at 03:50
  • Guys. I appreciate the alternate suggestions. But right now I'm in a bind and I need to edit the code and repackage this asap so i can put the full login name in. I'm short by one character. Any help would be appreciated. thanks. – dbconfession Jun 29 '14 at 03:54
  • I don't understand the question. What would you think the `limit` parameter is used for? So you need to change the code that uses this class, not the class itself. – camickr Jun 29 '14 at 05:04
  • I found it. I was asking for help finding the class which held the actual code that explicitly stated that the login field was 15 characters long. The code above is merely the initialization of the method. But I ended up finding it. using the "reference" option in eclipse. Thanks! – dbconfession Jun 29 '14 at 20:13

1 Answers1

1

According to Limiting the number of characters in a JTextField, you call the setDocument method of JTextField with the JTextFieldLimit as a parameter. I can't verify that it works, only that it is the highest-voted answer for that question.

Community
  • 1
  • 1
algorowara
  • 1,700
  • 1
  • 15
  • 15