The Artima Developer Community
Sponsored Link

Java Answers Forum
How can I limit the number of characters entered into a JTextField?

2 replies on 1 page. Most recent reply: Jul 14, 2006 3:03 PM by Matthew Ferreira

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Robert Stone

Posts: 2
Nickname: ph4nt0m
Registered: Apr, 2005

How can I limit the number of characters entered into a JTextField? Posted: Apr 24, 2005 9:07 AM
Reply to this message Reply
Advertisement
I haven't worked with Swing in a while and I can not remember how to limit the number of characters that a user can enter into a JTextField. I looked at sun's site and all I could find to accomplish this was the InputVerifier class, but there has to be a simpler way.

Any help will be greatly appreciated.


Robert Stone

Posts: 2
Nickname: ph4nt0m
Registered: Apr, 2005

Re: How can I limit the number of characters entered into a JTextField? Posted: Apr 24, 2005 5:54 PM
Reply to this message Reply
I got the answer. Thanks anyway.

Just incase anyone is curious this is the answer

The best way to do this is to subclass the JTextField class and then add a KeyListener to determine if you reached
the limit that will be passed in the constructor of the subclassed class.

import java.awt.event.*;
import java.awt.*;
import com.sun.java.swing.*;

public class SetLimit {

public static void main(String args[]) {
new SetLimitFrame();
}
}

class SetLimitFrame extends JFrame {
MyTextField mtf = new MyTextField(20,5);

SetLimitFrame() {
super();

/* Components should be added to the container's content pane */
Container cp = getContentPane();

/* Add the window listener */
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
dispose(); System.exit(0);}});

cp.add(BorderLayout.NORTH,mtf);

/* Size the frame */
setSize(200,200);

/* Center the frame */
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);

/* Show the frame */
setVisible(true);
}
}


class MyTextField extends JTextField
{
int limit;

MyTextField(int show,int limit)
{
super(show);

this.limit = limit;

addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent evt) {
if (getText().length() >= MyTextField.this.limit)
evt.consume();}});
}
}

Matthew Ferreira

Posts: 1
Nickname: entropylor
Registered: Jul, 2006

Re: How can I limit the number of characters entered into a JTextField? Posted: Jul 14, 2006 3:03 PM
Reply to this message Reply
Would this stop the user from using any keys at all, like backspace etc?

Flat View: This topic has 2 replies on 1 page
Topic: How to copy from one array to another array? Previous Topic   Next Topic Topic: How can I use input devices other than keyboard and mouse?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use