[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

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

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
zollen
Asked:
zollen
  • 4
  • 2
1 Solution
 
objectsCommented:
You'll need to define a custom Document object, and override the insertString() method.
0
 
zollenAuthor Commented:
The cursor will advance after each *insertion*
0
 
zollenAuthor Commented:
Any sample code of overfiding insertString() method would be grateful.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
objectsCommented:
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
 
CalronCommented:
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
 
objectsCommented:
0
 
objectsCommented:
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.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now