Solved

JTextField

Posted on 1998-11-27
7
537 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

810 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