Solved

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

Posted on 2002-04-11
7
208 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

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!

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

690 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