Solved

Overwritting characters in JTextField

Posted on 2002-05-31
4
329 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 41
sumDigits  challenge 7 59
mapAB Challlenge 35 83
Error with Java/Cache JDBC Classpath 2 19
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now