• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with DocumentFilter for BigDecimal

 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't get the DocumentFilter right for this cell editor. I have searched a lot of places and tried multiple ways. Basically what I am trying to do is allow the user to only enter a number up to two decimal places such as 3.00 0r 4.00. The way that it is set now is that the user can enter one number or a period at a time. I need it to validate that the user entered a number with two decimal places. JFormattedTextField I tried but I didn't like the 0's in the mask formatter ####.##. If the user entered 3.00, the mask formatter would show 0003.00. I don't want the leading zeros. Anyway to remove the leading zeros from the JformattedTextField or to get the DocumentFilter to work properly? Below is the code and any criticism for coding......


This is the DocumentFilter. I have also been playing with the if statements within the insertString and replace methods been tying to debug for a solution.



This is the CellEditor, case 4 within the switch statement handles the above DocumentFilter



This is the ValidateDouble class found within the DocumentFilter replace and insertString methods




This is the JTable setup placed within the Container Class. I figured I would just include this, the last line is the initiation of the CellEditor.

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't use a DocumentFilter but a DocumentListener for those text fields checking number of digits.

Your DocumentListener overrides insertUpdate, removeUpdate and changeUpdate accordingly. For your case most likely just need insertUpdate and removeUpdate.

For your table cell formatting extends DefaultTableCellRenderer and overrides the setValue, rather than XXXCellEditor.

Hope this helps.
 
Rancher
Posts: 3324
32
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you should be using a DocumentFilter, not a DocumentListener. A DocumentListener fires the event AFTER the document has been updated so its too late to reject the typed text.

However, you should forget about your table cell renderer and first learn how to write the DocumentFilter for a plain text field. So start by creating a JFrame with a text field and add your DocumentFilter to the text field. Keep the code simple when learning a new concept.

I don't use regex, so I don't know if your regex is correct or not, however the problem I see with the filter is that you are only validating the newly entered text, not the text already in the Document. So what you need to do is create a text string ASSUMING the text has been inserted into the Document and then validate that string. So the code might be something like:



I don't know why you create a Validate() class instead of just using a validate() method in the filter class.

Finally, there is no need to create a custom editor, just use the DefaultCellEditor:

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:Yes you should be using a DocumentFilter, not a DocumentListener. A DocumentListener fires the event AFTER the document has been updated so its too late to reject the typed text.

However, you should forget about your table cell renderer and first learn how to write the DocumentFilter for a plain text field. So start by creating a JFrame with a text field and add your DocumentFilter to the text field. Keep the code simple when learning a new concept.

I don't use regex, so I don't know if your regex is correct or not, however the problem I see with the filter is that you are only validating the newly entered text, not the text already in the Document. So what you need to do is create a text string ASSUMING the text has been inserted into the Document and then validate that string. So the code might be something like:



I don't know why you create a Validate() class instead of just using a validate() method in the filter class.

Finally, there is no need to create a custom editor, just use the DefaultCellEditor:



Thank You Rob....This made it a lot easier for me and gave me a lot more insight on what I need to do.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic