Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

PlainDocument question

 
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have a class that extends a JTextField. What it does is allow a user to enter only numbers and a period in that field (to mimic currency field). I've added a listener in this class so I know when something is added or deleted from the field, in order to know when a period is removed. The problem is that once I get the character that was just removed, the removeUpdate() method is too slow to capture that number since it calculates the character after it was removed, so all I get is an empty space for the last character. Does anybody have any suggestion for solving this?
Here is the listener:
=====================
class MyDocumentListener implements DocumentListener {
final String newline = "\n";
public void insertUpdate(DocumentEvent e) {
updateLog(e, "inserted");
}
public void removeUpdate(DocumentEvent e) {
updateLog(e, "removed");
Document doc = (Document)e.getDocument();
String val = "0";
try{
val = doc.getText(((doc.getEndPosition()).getOffset())-1,1);
}
catch(BadLocationException ble){ble.printStackTrace();}
System.out.println("value: " + val);
if(val.equals("."))
isPeriodEntered = false;
}
public void changedUpdate(DocumentEvent e) {
//Plain text components don't fire these events.
}
public void updateLog(DocumentEvent e, String action) {
Document doc = (Document)e.getDocument();
int changeLength = e.getLength();
System.out.println(changeLength + " character"
+ ((changeLength == 1) ? " " : "s ")
+ action + newline + "text lenth: " + doc.getLength() + newline);
}
}

thanks,
Alex
 
reply
    Bookmark Topic Watch Topic
  • New Topic