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

Overwritting characters in JTextField

Posted on 2002-05-31
4
332 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
  • 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 64
use lov values 2 61
hibernate jars 4 45
sql import cannot be resolved jsp 3 44
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…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

792 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