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
Solved

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

Posted on 2002-04-11
7
205 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
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
LVL 92

Accepted Solution

by:
objects earned 200 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
more than one jdk and one jre 1 50
difference of if loops 23 60
ejb stateless example 2 18
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
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.

808 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