This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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?
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.
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:
The easiest way is to subclass DefaultTableModel and override getColumnClass.
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 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.
There are no new questions, but there may be new answers.
Joined: Mar 17, 2010
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.