Solved

Overwritting characters in JTextField

Posted on 2002-05-31
4
334 Views
Last Modified: 2013-12-03
I would like to allow users to overwrite text in my JTextFields on a character by character basis. In other words, if the word cat is in a JTextField and the user puts his cursor in front of the c and types a b, the string would turn to bat.

What's the easiest way to replace text?
0
Comment
Question by:kmenzel
[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
  • 2
4 Comments
 
LVL 2

Expert Comment

by:allie
ID: 7047541
As far as I know, the only way to really do what you seem to be asking is to extend the PlainDocument class then assign that to your JTextField.  So you'd have something like this:

class MyDocument extends PlainDocument      
{  
   MyDocument()
   { super(); }

   public void insertString(int offs, String  str, AttributeSet attr) throws BadLocationException
   {
      if (str == null) return;
      //Get the text as it exists before the user changed it
      String originalText = this.getText(0, this.getLength());
      //Get the text as YOU want it - i.e. replacing a character instead of just adding a character
      String newText = originalText.substring(0, offs) + str + originalText.substring(offs+str.length(), origianlText.length());
      //Put your string back into the text box
      super.insertString(0, newText, attr);
   }
}

Note that I haven't tried this exactly, so you may need to include "remove(0, originalText.length());" before the super.insertString() in order to remove existing text before you write your own.  In any case, when you declare your text box, just do this:

JTextField myField = new JTextField();
myField.setDocument(new MyDocument());

That's it!

allie
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 7048021
try:

public void insertString(int offs, String s,
  AttributeSet a) throws BadLocationException
{
  super.remove(off, off+s.length());
  super.insertString(off, s, a);
}
0
 

Author Comment

by:kmenzel
ID: 7054369
I ended up doing something very similar except that I allow insert or overwrite. Thought I was missing something in the API. I guess not.

Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 7054783
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wild fly 8 startup error 2 79
junit as external jar or library 7 58
Session in java desktop 5 37
installing fonts 5 41
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

734 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