Solved

JTextField

Posted on 1998-11-27
7
526 Views
Last Modified: 2009-12-16
I want to put a JTextField in my program and the textfield can accept n characters. If the entered characters exceed n, a beep will be generated and the >n character can't be entered anymore.

I am succeded to generate the beep if the entered character exceeds n, but I failed to stop the further entered character. That means, thought a beep can be generated if the length of entered chracters longer than n, but one can further enter characters and that's what I want to solve.

Has anyone idea?
0
Comment
Question by:chenwei
  • 4
  • 2
7 Comments
 
LVL 7

Expert Comment

by:conick
ID: 1228333
Assuming you have a textListener added to the textfield to generate the beep when >n
in the same method couldnt you get the text (getText()) when =n and save it in a String so that when another key is entered you can call setText(String) so it gives the appearance that no more characters are being entered.
Maybe Im misunderstanding the question though... If you could post the code that you are currently using maybe I (or someone else) could come up with something better

0
 

Author Comment

by:chenwei
ID: 1228334
To conick:

thanks for your message. But truly to say I have the same idea as you at the begining. But I don't think this is a good solution. I've used the Exception class to handle it. It's much intellent.

Sorry, I can't accept your suggestion. I've solved the problem myself.
0
 
LVL 4

Accepted Solution

by:
evijay earned 20 total points
ID: 1228335
Here is the solution for the problem from java swing book of O Reilly associates

// FixedLengthTextField.java
//
import com.sun.java.swing.*;

// A fixed-length JTextField
public class FixedLengthTextField extends JTextField {

  public FixedLengthTextField(int length) {
    this(null, length);
  }

  public FixedLengthTextField(String text, int length) {
    super(new FixedLengthPlainDocument(length), text, length);
  }
}

// FixedLengthPlainDocument.java
//
import java.awt.Toolkit;
import com.sun.java.swing.*;
import com.sun.java.swing.text.*;

// An extension of PlainDocument that restricts the length of the content it
// contains.
public class FixedLengthPlainDocument extends PlainDocument {

  // Create a new document with the given max length
  public FixedLengthPlainDocument(int maxLength) {
    this.maxLength = maxLength;
  }

  // If this insertion would exceed the maximum document length, we "beep" and do
  // nothing else. Otherwise, super.insertString() is called.
  public void insertString(int offset, String str, AttributeSet a)
  throws BadLocationException {
    if (getLength() + str.length() > maxLength) {
      Toolkit.getDefaultToolkit().beep();
    }
    else {
      super.insertString(offset, str, a);
    }
  }

  private int maxLength;
}
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:chenwei
ID: 1228336
To, Evijay:

thanks for the answer. truly to say I've solved the problem my self. I used almost the same way you told me. Sure, I will give you the points. But I will  like to ask you if you could give the book's title you mentioned. I guess there are also some other useful examples in this book. If you could also recommend me some othere interesting books, it would be the best.
0
 

Author Comment

by:chenwei
ID: 1228337
If I want to use the case of FixedTextField to all the cells of a column in a JTable, how can I do that?
0
 

Author Comment

by:chenwei
ID: 1228338
Please forget my last comment. I've solved it.
0
 
LVL 4

Expert Comment

by:evijay
ID: 1228339
the book name is

java swing
by Orielly associates

vijay

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now