• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

increment date with + but preventing the + from being put in the textfield

 
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to increment my date with + key and I want to decrement date with - key.  Similar to Quicken.
I am getting the date to increment and decrement but the character is still being placed in the textfield which obviously I dont want.  I googled, one person suggested a documentListener but I couldnt figure out how to get the key event and abandoned that one.  Another suggestion was doing some kind of filter but his listener was nothing like mine so I was quite confused on how to try what was being suggested.  

Focus on the Date Field for this post.
 
Saloon Keeper
Posts: 11040
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Carey Brown
Saloon Keeper
Posts: 11040
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After playing with the code some more I came to the conclusion that it's impossible (?) to detect the difference between the hyphens in the date format and the hyphen meaning decrement. You could write your own formatter or you could use slashes as the date delimiters.
 
Sheriff
Posts: 28385
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:After playing with the code some more I came to the conclusion that it's impossible (?) to detect the difference between the hyphens in the date format and the hyphen meaning decrement.



The same thing is going to happen with the DocumentListener, I expect. However, I'm not familiar with Quicken but from the requirements given it looks like +/- are supposed to increment dates which are already fully typed into the input field. So the KeyListener/DocumentListener would need to have access to the contents of the field, but not only that, it would have to have access to partially-typed data which has not been accepted to be the contents of the field. To me that makes a DocumentListener perhaps more practical, I think.

Or perhaps it would be possible to re-invent the requirements and put "+" and "-" buttons next to the field for the purpose of changing the date in the field?
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still not understanding the concept of an SSCCE. Only code directly related to the stated problem should be include. This way we can see exactly what you are doing and don't have to filter out the rest of the code not relevant to the problem.

What you are doing correct:

1. all the code is in a single file

What is unnecessary

1. only the date text field is needed. The other labels and text fields are irrelevant
2. the UIManager properties are irrelevant
3. custom frame size code is irrelevant
4. custom Fonts and Colors are irrelevant

The code should be as simple as possible to make sure the irrelevant code is not causing the problem.

I am getting the date to increment and decrement but the character is still being placed in the textfield which obviously I dont want



Don't use a KeyListener. Swing was designed to be used with Key Binding to associate an Action with a KeyStroke:

I removed 100 lines of code from your SSCCE so you can easily see the changes made to support the Key Bindings:



In addition to incrementing/decrementing the date, the DateAction will handle insertions of "-" when the user types the date in manually.

one person suggested a documentListener


Won't help because the listener is invoked AFTER the Document has already been updated

Another suggestion was doing some kind of filter


There is a DocumentFilter but I don't think this approach will work because the JFormattedTextField uses its own DocumentFilter and filters can't be combined.
 
Carey Brown
Saloon Keeper
Posts: 11040
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good Rob. Better than what I had. I still can't delete the default date, put in a new date, and decrement. Don't know where to look.
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ended up adding a MaskFormatter and that solved the problem.  However, I need to look into what Rob said regarding using KeyBinding instead of KeyListener.  BTW, I thought you wanted an SSCE that you could copy and paste into an editor and it ran to illustrate the problem.  Which is what I did.  I am finding it extremely difficult to please you.  But there are people you can never ask the question right and you will never please them.  I am not trying to offend you but... Cant you give a forced retired 60 year old a break.
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After further testing, I discovered this only works the first time.  It fails once you leave the editor and return to the editor.  I know your going to complain that I never mentioned an editor.  That was because I would have had to put an entire table in the test code and I thought you would have complained about that.
 
Carey Brown
Saloon Keeper
Posts: 11040
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an SSCCE I made using your code and I added a second JTextField for the "editor". Depending on what behavior you were expecting it seems to work to me when I tab back and forth between the fields. I did change your valueToString() method.
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The SSCCE does not implement or test the problem I am trying to overcome.  using the + key to increment dates and - to decrement dates.  I added the Mask to prevent the + or - from being appended to the end of the date.  That was the issue to begin with.  The problem I now have is it only works the first time.  Not the second time.  And I am using this field as an inline editor for a JTable and so to post an SSCCE would require quite a bit of code to illustrate what is actually happening.  When I first posted the test code it was happening there but now that I have added the mask its not happening the test code.  I guess I will have to create a small app to illustrate it.
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, I have tried to make this as small as I could and still reproduce somewhat the environment I am actually using.
If I dont have it right, give me some credit for trying













I hope I got it all.  Some of this code is in production so I really didnt want to change it and it didnt make sense to make a copy.  Notice it works the first time.  but when you click the next field and then click back it puts the + or - in but when you exit it gets rid of it.  but having it in is janky.
 
Carey Brown
Saloon Keeper
Posts: 11040
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Handle individual character response including increment and decrement using documentListener. This could be refactored so that it's all rolled into a LocalDateTextField class.
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:


Did you use "Increment Date" in both cases to only use one name in the action list or was that a mistake?

Also, when I put this in my Cell Editor which implements TableCellEditor, the fireEdditingCancel or Stop doesnt seem to be working any longer.  Do you think this way of capturing the + and - is interfering with the TableCellEditor Parent?

 
Rob Camick
Rancher
Posts: 3324
32
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 weeks ago you got multiple answers/suggestions to your original question.

Yet you didn't respond to either reply and now you expect more help even though you still haven't posted a reasonable SSCCE?

I thought you wanted an SSCE that you could copy and paste into an editor and it ran to illustrate the problem.  Which is what I did.  


And I gave you pointers on how you can improve upon the SSCCE in future questions.

Some of this code is in production so I really didnt want to change it and it didnt make sense to make a copy


And you still don't understand the basic concept of a SSCCE (so you haven't learned from my pointers).

We don't care about your application. We care about the stated problem.

Your question is about using a custom Date editor:

1. So you need a custom DateEditor class
2. You need a simple GUI to test the custom DateEditor class
3. You don't need a custom TableModel to test the DateEditor.
4. You don't need a custom renderer to test an editor.
5. You don't need custom application classes. Your code includes package statements and import statement for your application classes. I am not going go copy all those classes and create a directory structure that matches your package names. The one positive comment I gave to your original SSCCE was that is was contained in a single class file. Now you have 6 class files? How is that a SSCCE?

Below is some old code I have demonstrating a simple custom editor in the form of a SSCCE:



The idea of the SSCCE is to create your reusable editor code and create a simple test GUI. If you write the editor correctly, once you finish testing it then you just plug it into your application because it should not be dependent on any specific application logic.

If you can't get the editor to work then you have simple code to post on a help site that requires no application specific code.

Did you use "Increment Date" in both cases to only use one name in the action list or was that a mistake?


Yes it was a mistake, the name will be used when you add the Action to a JMenuItem or a JButton so it should be different, but it doesn't affect processing.

See: How to Use Actions for more information and examples.

Also check out Using an Editor to Validate User Entered Text. It is a complete working example that uses a JFormattedTextField to validate Integer data in a specified range. This should be a good starting point to create your custom DateEditor. It shows which methods you need to override. It is better to extend from the base class rather than implement all methods. The code already uses an ActionMap for some of the processing so you should be able to implement my earlier suggestions based on that code.

I am definitely NOT an expert on creating editors for a JTable. I have never done this before especially with a JFormattedTextField. Even if you post a SSCCE that more closely resembles the structure of the code above there is no guarantee that I can help.

However, I will not even attempt to look at the code you just posted. It is not even close to a SSCCE for me to look at. My time is valuable. This is the last time I will rant about a SSCCE. How you choose to post a question is up to you. Which questions I choose to answer is up to me.

 
Carey Brown
Saloon Keeper
Posts: 11040
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still a bit over-weight for an SSCCE but it's all in one file, it compiles, and it works.
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:
3 weeks ago you got multiple answers/suggestions to your original question.

Yet you didn't respond to either reply and now you expect more help even though you still haven't posted a reasonable SSCCE?



Three weeks ago I gave up on Swing.
A friend talked me into trying Python ttk which before long I was jumping through even worse hoops.
I then looked into other frontend frameworks.  But it seems as if all the new ones build the pages on the server side and then ship all that info over the network.  Not what I want
And so I came back to Swing.


Your question is about using a custom Date editor:



At first, I was able to reproduce the problem without using a table and so that made it simple.  Which is why my original post was not about a table cell editor.  However, as I implemented yours and Caseys suggestions, the simple one worked but not the table one so that made things more complicated.  So then the only way to reproduce the problem was in a table and a simple example no longer was going to cut it.  Which I did state this in a previous post.  Maybe you missed that one.

BTW, the cell editor I have is pretty much the one you helped me with on another question.  So the code I have came from you. You, being Rob.  And that code worked fine other than the one extra character with the + and -.  that was until I added the Mask and added your binding code rather than the keylistener.  Once I added both those, that is when the editor no longer stopped or cancelled itself when you exited the edit.  It actually stayed on the screen when I clicked another tab for a different form.  Its as if the Mask and the new action binding are fighting each other.  or something is missing.

Sadly, this question has evolved.  I was actually closing out this question because for the scope of the original question we somewhat answered it as a stand alone field editor.  I was actually thinking of posting a new question with the editor in a table.  I am planning on looking at Robs last posted code to see what is different from this one verses the one he gave me in a previous question and if it still does not work, then I will attempt an SSCCE I hope even Rob will be okay with.


As for Caseys Code, I tested it.  The Mask part didnt work.  I endered 010223 and instead of filling in the date it just inserted the date.   Once I hand put in the date the + and minus worked.  however, when I left the field and came back in and pressed the - key I got a BadLocationException:  Invalid Remove.  And of course, Rob claimed I needed to use Bindings and yours uses a document listener.  If your had of worked. I would have gladly replaced Robs example.

javax.swing.text.BadLocationException: Invalid remove
at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:605)
at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:590)
at LocalDateDocListener99.handleInsert(SwingLocalDateTest99.java:307)
at LocalDateDocListener99.lambda$insertUpdate$0(SwingLocalDateTest99.java:271)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

So lets just end this question.
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did finally find the solution.  When I was doing my cell ComboBox to get it to work we used this type of class definition:



However, for some reason when I did the same thing for the Formatted Text Field it didnt work.



If you google JFormattedTextField and TableCellEditor you only get two good results.  Both are using this format:



And you let the Cell Editor manage all the other methods.  and as far as I can tell, it now works.  I have no idea why the first way I did it didnt work but it seemed as if somehow it lost reference to the Mask Formatter and the second way seems to have maintained it.  So the first way works great with Combo Boxes and the second way with FormattedTextFields.  Hope this crazy thread helps someone one day.  
 
Paul Clapham
Sheriff
Posts: 28385
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that!

Even when Swing was less of a niche Java topic, very few people dared to attempt writing a TableCellEditor. So I guess that's why you won't find much online about them.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swing provides many Abstract classes that implement most of the methods. In many cases they also provide "default" implementations of an interface. I guess the trick is to know when to extend the Abstract class or the "default" class.

I would suggest you not attempt to implement all the methods of an interface yourself. By minimizing the code you write you minimize the change in default behaviour and you minimize the risk of introducing an error.

If you google JFormattedTextField and TableCellEditor you only get two good results.


Gee, and I provided you with two working examples in this question and neither extend AbstractCellEditor.

You never actually replied if you tested the IntegerEditor. Since this editor uses a JFormattedTextField and adds bindings to the ActionMap/InputMap I thought it would have been a good place to start.

By extending AbstractCellEditor you are changing the default behaviour of your editor from default editors. The default editor for text, numbers etc. is only focused when you double click on the cell. Since the AbstractCellEditor doesn't have this "click count" concept to start editing I would guess it starts editing as soon as you click on the cell. Just a thought.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic