Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with DocumentFilter for BigDecimal

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • 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.

 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!