?
Solved

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

Posted on 2002-04-11
7
Medium Priority
?
209 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
[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 92

Expert Comment

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

Author Comment

by:zollen
ID: 6935563
The cursor will advance after each *insertion*
0
 

Author Comment

by:zollen
ID: 6935567
Any sample code of overfiding insertString() method would be grateful.
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!

 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 6935588
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
ID: 6942105
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
ID: 6943362
0
 
LVL 92

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

777 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