Jump to content

How to limit Character input in a JTextField?

- - - - -

  • Please log in to reply
10 replies to this topic

#1
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Cant seem to set a max input length on a JTextField without altering the outlook of the gui. Tried a few things including this which were all unsuccessful. The link pointed to changes the outlook of the gui and i can still enter more chars the the amount i specify.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Is it the entering part, or the processing part, that's important?

Because if it's the processing part of it, then you can use the substring () method to grab only the first some characters of the string.

If it's the entering part of it, I'm not experienced at all with Java, so I wouldn't know. One idea, though, is to check the input field, every time it changes, and if there is more text than allowed, then truncate the text and save it to the input field.

#3
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Its the entering part. If i could limit from the input then i wont have to ever truncate(there are a few text fields), plus when the user can enter an ridiculously long strings it look awkward.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
It looks like you'll have to either add an actionListener to the text field and once your LIMIT has been reached, ignore/remove the newly typed characters.

You can also change the text fields document to limit the amount of text entered.

---------- Post added at 04:51 PM ---------- Previous post was at 04:38 PM ----------

Here's a simple example that creates a default document model for the text field.
import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.text.AttributeSet;

import javax.swing.text.BadLocationException;

import javax.swing.text.Document;

import javax.swing.text.PlainDocument;


public class LimitedJTextField extends JTextField {

	private static int limit;


	public LimitedJTextField(int limit) {

		setLimit(limit);

	}


	public int getLimit() {

		return limit;

	}


	public void setLimit(int limit) {

		this.limit = limit;

	}


	protected Document createDefaultModel() {

		return new LimitedLengthDocument();

	}


	static class LimitedLengthDocument extends PlainDocument {

		public void insertString(int offs, String str, AttributeSet a)

				throws BadLocationException {

			if( str == null )

				return;

			

			if( (getLength() + str.length()) <= limit )

				super.insertString(offs, str, a);

				

		}

	}

	

	public static void main(String[] args) {

		JFrame frame = new JFrame();

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.add( new LimitedJTextField(10));

		frame.pack();

		frame.setVisible(true);

	}


}
I seen the idea right from the JTextField documentation here: JTextField (Java Platform SE 7 ) They create an UpperCaseOnlyJTextField though.

#5
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Nice. It works i also used fieldname.setColumns to get the display size i wanted. I have not attempted this yet but while we are here: You ever tried entering an ip address in MS Windows IP configuration dialog box? The input field is separated by three periods(dots) so as you type the first three numbers it automatically jumps to the 'second' area and so on, of the text field. Any ideas on this type of formatting. I'm going to use it for a program where the user would have to input ip addresses.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
You could use 3 JTextFields separated with 2 JLabels containing "."
To jump from field to field you'd have to add a listener and when the length of the first 2 fields equals 3, jump to the next field.

The only way I could think of drawing the dots in the textfield is to manually draw over the text field. If you tried to add an actual character of a (.) then the user would be able to erase it. Either that or you'd have to constantly keep updating the text field to match the dot pattern you want every time the user enters a number.

#7
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Hmm. I like the first idea. Doing this versus relying on the user to enter the ip address in the correct format, i prefer leave as little as possible to the user. I'll have to play around with it for a while and see.

PS: I'm really wondering how the Microsoft programmers did that. pretty neat :)
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#8
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

fread said:

Hmm. I like the first idea. Doing this versus relying on the user to enter the ip address in the correct format, i prefer leave as little as possible to the user. I'll have to play around with it for a while and see.

PS: I'm really wondering how the Microsoft programmers did that. pretty neat :)

Making sure the user just enters in digits is also easy. You can modify the code I provided very easily and check to make sure digits are entered. You can also see(once the user has entered 3 digits into 1 field) that the digits are less than 256.

#9
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
@lethalwire: duly noted. Working on it now..will update in an hour or two.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#10
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
I moved
static class LimitedLengthDocument extends PlainDocument
to a stand alone class and extend to a JTextField class
public class myFixedLengthJTextField extends JTextField
then call
super(new LimitedLengthDocument(length), "", length);
int the constructor. So I am now able to limit not just the JTextField but also the JPasswordField. Still working on the ip field.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#11
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
I used the multiple text field separated by J label dots to accomplish the I.P feel separation i wanted. Moving on to the menu.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users