aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Only want a jtable cell to take a double value.   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Only want a jtable cell to take a double value.   " Watch "Only want a jtable cell to take a double value.   " New topic
Author

Only want a jtable cell to take a double value.

Jason Richard
Ranch Hand

Joined: Oct 05, 2007
Posts: 69
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.




Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jason Richard
Ranch Hand

Joined: Oct 05, 2007
Posts: 69
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.
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
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

Joined: Oct 05, 2007
Posts: 69
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

Joined: Feb 24, 2001
Posts: 96
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Only want a jtable cell to take a double value.