• Post Reply Bookmark Topic Watch Topic
  • New Topic

JFormattedTextFields, DocumentListeners and Formatters  RSS feed

 
Brent Wenson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is all jdk1.4.0
I've created a few JFormattedTextFields to allow user input of dates. I'm using focusListeners and documentListeners to determine if the user changed the data in the textfield in any way. the focusListener adds a documentListener to the textfields document that just got focus. If there is a change in the data the documentListener hears it and sets a flag.
For some reason, as soon as the field gets focus, something is causing the documentListener to fire events. This doesn't happen with regular JTextFields. How do I stop these events (actually insertUpdate and removeUpdate) from firing just when the field receives focus? I'm actually using a SimpleDateFormat in this example but experienced the same behavior with MaskFormatter (and if you have any suggestions how to implement SimpleDateFormat and MaskFormatter at the same time that would be wonderful). Thanks in advance for any advice you can give!
Here's a sample of what I'm doing:
boolean docChanged = false ;
Document doc ;
JFormattedTextField field ;
field = new JFormattedTextField( new
SimpleDateFormat( "MM/dd/yy", Locale.US ) ;
field.addFocusListener( this ) ;
public void focusGained( FocusEvent fe ){
doc = ( (JFormattedTextField)fe.getSource() ).getDocument() ;
doc.addDocumentListener( this ) ;
}
public void focusLost( FocusEvent fe ) {
doc.removeDocumentListener( this ) ;
if( docChanged == true ) {
//do something with the data
docChanged = false ;
}
}
public void removeUpdate( DocumentEvent de ) {
docChanged = true ;
}
public void insertUpdate( DocumentEvent de ) {
docChanged = true ;
}
public void changeUpdate( DocumentEvent de ) {
docChanged = true ;
}
 
Josh Rehman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it's firing when the component gets the focus? Or is it firing when you are adding the listener? You can test this by adding a listener that persists across focus changes, and seeing if anything gets called. If it still happens, I would consider this a bug, and try upgrading to 1.4.1_01, and/or finding another way to do what you want.
 
Brent Wenson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I put some debug code in the focusGained() method to print out what was going on and the docChanged() is definitely firing as soon as the JFormattedTextField gets focus. I'm assuming some kind of validation is going on with the formatter that is causing the docChanged to get fired. Just haven't figured out where yet.
These JFormattedTextFields seem to be more trouble than they're worth. It's a great idea, but it's rough around the edges right now, at least for me.
 
Brent Wenson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I put some debug code in the focusGained() method to print out what was going on and the docChanged() is definitely firing as soon as the JFormattedTextField gets focus. I'm assuming some kind of validation is going on with the formatter that is causing the docChanged to get fired. Just haven't figured out where yet.
These JFormattedTextFields seem to be more trouble than they're worth. It's a great idea, but it's rough around the edges right now, at least for me.
 
Brent Wenson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just re-read your post (sorry I posted mine twice, still not sure how that happened). I think you might have a good point that I skipped over the first time. The docChanged() is probably fired because I installed the documentListener to it. Now I need to figure out a work around that.
Thanks!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!