| Author |
JTable set column data type
|
Martin Zemanek
Greenhorn
Joined: Mar 17, 2010
Posts: 4
|
|
Hi folks.
Is it possible to set a column in JTable to a certain data type? I mean to set a column to integer data type for example and when is user trying to input non-integer value it will do nothing.
Or do I have to solve this in other way like adding a listener and conditions to all cells in the column?
Martin
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18365
|
|
Check out TableModel.getColumnClass(int column). If this method returns Boolean.class, Integer.class (or any of the other Number subclasses), Date.class and a few others, a JTable will have automatic support for rendering / editing. For Integer.class that means that the editor will not accept the value and show a red border around the editor if the value cannot be converted into an int.
The easiest way is to subclass DefaultTableModel and override getColumnClass.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Martin Zemanek
Greenhorn
Joined: Mar 17, 2010
Posts: 4
|
|
Thanks for reply Rob.
But this TableModel.getColumnClass(int column) did not solve my problem. I have created a JTable using Object[cells][columnNames] and so all the cells are Object data type. I would like to set column class instead of get column class.
I do not quite understand this sentence:
I do not know how to subclass something. I was thinking about overriding it, but I do not know how and I am not sure it will help.
The other my idea is to create a JTable using Vector type, but i do not know if there will be more options to set column data type and also I am afraid it will cost me much more time to convert my table to Vector type.
Martin
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18365
|
|
|
How to Use Tables
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 3212
|
|
Martin Zemanek wrote:I do not know how to subclass something.
Every Java class you've ever written subclassed something. Either you extended another class explicitly, or the class you wrote extended (subclassed) java.lang.Object implicitly.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Martin Zemanek
Greenhorn
Joined: Mar 17, 2010
Posts: 4
|
|
I have read How to Use Tables but it did not help me much. So I attach a shortened example of code and I would be happy if someone showed me the exact solution.
At the beginning is the table empty, user can input values into cells, but it will not let him to enter a non-integer value into "age" column for example. I was searching the web, but the solutions I found did not helped me (or I did not understand). So I would like to see the solution on this example, if it is possible.
Martin
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26710
|
|
|
Now a candidate to move to our GUIs forum.
|
 |
Rob Camick
Ranch Hand
Joined: Jun 13, 2009
Posts: 1652
|
|
|
Where did you override the getColumnClass(...) method? If you don't know how to do this then search the forum or the web for examples that use that method.
|
 |
Martin Zemanek
Greenhorn
Joined: Mar 17, 2010
Posts: 4
|
|
Thanks for replies guys. So I subclassed DefaultTableModel and overridden some methods (getColumnClass, isCellEditable). If anyone else is interested in this so here is the solution.
|
 |
Simon Wielki
Greenhorn
Joined: Dec 09, 2011
Posts: 1
|
|
You can easly make column types in JTable (I use JXTable but in this thing it is the same):
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18365
|
|
Welcome to the Ranch!
That code is horrible with all these casts. If you need to treat the model as a DefaultTableModel, simply declare it as such:
|
 |
 |
|
|
subject: JTable set column data type
|
|
|