Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limiting text in a JTextField 2

Status
Not open for further replies.

gregsa

Programmer
Jun 22, 2001
7
GB
I wish to restrict the text entered into a JTextField to just digits and limit the string to 4 chars long. I have tried to do this by attaching a DocumentListener to monitor changes and delete unwanted characters, but this throws an error I presume because I am causing an endless loop by changing the JTextField that the listener is watching while it is still in the process of responding to the previous change.

I don't know how to make this threadsafe. But I have heard of an alternative and possibly better method than attaching a listener to a JTextField which involves creating a subclass of PlainDocument, overriding its insertString method and getting JTextField to use this instead. Has anybody tried this, or have any further details about how to implement this? My experience with java is fairly (and probably apparently) limited. Any help appreciated.

Greg
 
Actually, I've done something similar to this to elminate spaces in the text. What I would suggest is to use a focus listener, and when the text field "loses focus", carry out your process of choice. You could delete all non-acceptable characters, or, you could prompt the user and say, "This input is not valid, please follow these rules..."

I hope this helps. ------
Kennedy Roberts
 
Hi,

I have just created a small little program that you might want to take a look. It still need some improvement...but I am logging off so you will have to edit the rest yourself (nothing is free :p)

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class TextFieldDemo extends JFrame implements KeyListener
{
private JTextField txtField;
public TextFieldDemo()
{
setSize(100,50);
setTitle("TextFieldDemo");

txtField = new JTextField(4);
txtField.addKeyListener(this);

getContentPane().add(txtField);
}

public void keyPressed(KeyEvent e)
{
}

public void keyReleased(KeyEvent e)
{
validateTextField(e.getKeyChar());
}

public void keyTyped(KeyEvent e)
{
}

public void validateTextField(char c)
{
// check for length
if (txtField.getText().length() > 4)
{
txtField.setText((txtField.getText()).substring(0,(txtField.getText()).length()-1));
JOptionPane.showMessageDialog(this,"You have reached the limit.","Error",JOptionPane.ERROR_MESSAGE);
}

// check for non integer
else if (Character.isDigit(c) == false)
{
if (txtField.getText().trim().length() == 1)
txtField.setText("");
else
txtField.setText((txtField.getText()).substring(0,(txtField.getText()).length()-1));
JOptionPane.showMessageDialog(this,"Invalid Key.","Error",JOptionPane.ERROR_MESSAGE);
}
}

public static void main(String[] args)
{
TextFieldDemo tfd = new TextFieldDemo();
tfd.show();
}
}

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top