Solved

Question about inserting text into a JTextField... (TOUGH)

Posted on 2002-04-11
7
202 Views
Last Modified: 2010-03-31
Hi Guys,

   I want to customize JTextField so that inserting a text into a middle of the content 'overwrites' the old text at the same position.

For example:
JTextField holds "ABCD"
User keys in "Z" between "AB" and "CD". JTextField should display the following:

"ABZD", but NOT "ABZCD"

I am seeking the most effective solution..
thanks.
stephen
0
Comment
Question by:zollen
  • 4
  • 2
7 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
You'll need to define a custom Document object, and override the insertString() method.
0
 

Author Comment

by:zollen
Comment Utility
The cursor will advance after each *insertion*
0
 

Author Comment

by:zollen
Comment Utility
Any sample code of overfiding insertString() method would be grateful.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 92

Accepted Solution

by:
objects earned 200 total points
Comment Utility
Try something like:

class MyDocument extends PlainDocument
{
   public void insertString(int offs, String str,
      AttributeSet a) throws BadLocationException
   {
     if (str==null)
     {
        return;
     }
     remove(offs, str.length());
     super.insertString(offs, str, a);
  }
}

0
 
LVL 5

Expert Comment

by:Calron
Comment Utility
Have a look at the sun java tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html#validation

it explains the concept of using Documents on TextFields.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
A 'C''s a bit harsh though :-(
If you needed more info you just needed to ask.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

772 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

11 Experts available now in Live!

Get 1:1 Help Now