• 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

Only want a jtable cell to take a double value.

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In an effort to share what I've learned, so the next person that searches the interwebs has an easier time then I did, I'm going to try to post some of my solutions, and leave them up for discussion on better ways to do what I'm doing.

A lot of what I'm doing on my current project doesn't seem to fit in with conventional solutions I find examples for. Swing and renders can be very overwhelming when you want to actually do something worth doing.

My latest issue was that I wanted to only get a double value into a JTable Cell.


So first I Overrode the getCellEditor(int row,int col) method of my JTable, so if I was in a cell i knew I needed a double value I returned my Editor.

My editor filters out anything but numbers. I also check to make sure we get one and only one decimal point.

Other solutions Ive seen use keyboard Listeners, but to me that doesn't work. What if the user cuts and pastes data? My solution handles that as well.

It doesn't handle if they try to cut and paste something like 1234abcd.bob.123.23 , this would result in 1234..123.23 pasted in. I'll handle faulty values on the getValue method of my AbstractTable, but as long as the user is using mostly valid data this works well.




 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be easier to let your model return Double.class for getColumnClass. Sure, the user would be able to enter non-numbers. However, trying to commit that would fail, and the cell would get a nice red border, and the cell would still be in edit-mode. You get the same end result (only doubles can be entered) with a lot less effort.
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:It would be easier to let your model return Double.class for getColumnClass. Sure, the user would be able to enter non-numbers. However, trying to commit that would fail, and the cell would get a nice red border, and the cell would still be in edit-mode. You get the same end result (only doubles can be entered) with a lot less effort.



I did notice that, but I wanted to restrict them to having to have a double value. My AbstractModel is tied to a specific object, and I'm putting a double in particular fields. I didn't want the user to not realize they weren't submitting expected data.
 
Ranch Hand
Posts: 118
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Richard wrote:I'm going to try to post some of my solutions, and leave them up for discussion on better ways to do what I'm doing.


Jason Richard wrote:Other solutions Ive seen use keyboard Listeners, but to me that doesn't work.



Yes, your approach is definitely a better solution; a listener-based approach would be clunky at best.

Since you indicated you're interested in feedback I can think of a couple of things you might want to consider. First, instead of implementing your DocumentFilter as an anonymous inner class you should make it a proper standalone class so that it can be used with other tables without having to copy and paste the code.

Second, instead of overriding getCellEditor() to return the double editor it's generally better to associate that editor with a data type (e.g., Double) returned by your table model or to explicitly assign it to be the editor for that column.
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brett Spell wrote:

Since you indicated you're interested in feedback I can think of a couple of things you might want to consider. First, instead of implementing your DocumentFilter as an anonymous inner class you should make it a proper standalone class so that it can be used with other tables without having to copy and paste the code.



Right, thats a fantastic thought, and in the plans, just haven't gotten there yet.

Brett Spell wrote:
Second, instead of overriding getCellEditor() to return the double editor it's generally better to associate that editor with a data type (e.g., Double) returned by your table model or to explicitly assign it to be the editor for that column.



That's part of the problem. I do like the idea of the associate that editor with a dataType, I'll have to look to see if i can make that work.

I was avoiding trying to make it an editor for the column, because I don't always know that THAT particular column will be of that data type. I read in metadata for a particular object, and then I create the columns based on that metadata, and if that metadata tells me that column "X" happens to be a double, int,picklist, then I need to handle that. It's been quite a pain the moose caboose, but it's been great learning as well. I'm sure I'm far from done with my questions for the community!
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Richard wrote:I was avoiding trying to make it an editor for the column, because I don't always know that THAT particular column will be of that data type.



That's why it's usually better to associate the editor with a type / class using JTable.setDefaultEditor() instead of assigning it to a specific column.
 
Attractive, successful people love this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic