🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #9785

Limiting the number of characters for a JTextField

#9785
From: Jeff Marendo
Date: Thu, 27 Jan 2000 00:00
119 lines
3632 bytes
Hello,

I'm trying to write a customized class for JTextField where you can set a
maximum amount of characters that can be entered for it.  I think I'm almost
there, and my code is posted below.  I have a couple of things that I'm
stuck on, though.

One, I'd like it so that if the user hits the left arrow key (or right) the
local variable called 'localLimit' isn't incremented.  Currently, it's
incrementing.

Second, I'd like it so that if the cursor is after the last char in the text
field, the 'localLimit' will not be decreased if one hits the 'Delete' key.

Third, does anyone know why the keyboard is trying to do something with my
functionality for the 'Enter' key other than moving the focus to the next
component?

Please let me know.  Thanks.

Jeff

Code..

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

public class NumericLimitJTextField extends JTextField
  implements KeyListener {

  public NumericLimitJTextField() {
    super();
    addKeyListener(this);
    setPreferredSize(prefSize);
  }

  public NumericLimitJTextField(String text) {
    super(text);
    addKeyListener(this);
    setPreferredSize(prefSize);
  }

  // Accessor Functions

  public int getLimit() {
    return limit;
  }

  int getLocalLimit() {
    return localLimit;
  }

  // Modifier Functions

  public void setLimit(int charLimit) {
    if(charLimit >= 0) {
      limit = charLimit;
    }
  }

  void increaseLocalLimit() {
    localLimit = localLimit + 1;
  }

  void decreaseLocalLimit() {
    if(localLimit == 0)
      localLimit = 0;
    else
      localLimit = localLimit - 1;
  }

  void resetLocalLimit() {
    localLimit = 0;
  }

  // Implementation of KeyListener Functions

  public void keyPressed(KeyEvent e) {
    System.out.println("Start of function, the local limit is: " +
getLocalLimit());
    int key = e.getKeyCode();
    // System.out.println("The key code for that char is: " + key);
    /* The check for the VK_ENTER key makes the focus jump
     * to the next component as if you had hit the VK_TAB key.
     */
    if((key == KeyEvent.VK_TAB) || (key == KeyEvent.VK_ENTER)) {
      ((Component)(e.getSource())).transferFocus();
    }

    if((key == KeyEvent.VK_ENTER) || (key == KeyEvent.VK_KP_LEFT)) {
      // do nothing
    }
    else if((key == KeyEvent.VK_DELETE) || (key == KeyEvent.VK_BACK_SPACE))
{
      decreaseLocalLimit();
    }
    else {
      increaseLocalLimit();
    }
    System.out.println("Now the local limit is: " + getLocalLimit());

    if(localLimit > getLimit()) {
      javax.swing.JOptionPane.showMessageDialog(null, "You have entered too
many characters",
      "Input Error Message", javax.swing.JOptionPane.WARNING_MESSAGE);
      resetLocalLimit();
    }
  }

  public void keyReleased(KeyEvent e) {};
  public void keyTyped(KeyEvent e) {};

  // Private Member Variables

  private int limit = 0;
  private int localLimit = 0;
  private Dimension prefSize = new Dimension(63, 21);
}
Technical Details
Message-ID: <86q8sn$rje4@bornews.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,a26b3ba2769008e3,start
X-Google-Attributes: gid111d45,public
From: "Jeff Marendo" <marendoj@commos.com>
Subject: Limiting the number of characters for a JTextField
Date: 2000/01/27
Message-ID: <86q8sn$rje4@bornews.borland.com>
X-Deja-AN: 578452801
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200
Organization: CommTech Corporation
X-MSMail-Priority: Normal
Reply-To: "Jeff Marendo" <marendoj@commos.com>
Newsgroups: borland.public.jbuilder.java.swing