Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JTextField

Posted on 1998-11-27
7
Medium Priority
?
564 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 80 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

610 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