Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Overwritting characters in JTextField

Posted on 2002-05-31
4
Medium Priority
?
338 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 800 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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 was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

718 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