Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable set column data type

 
Martin Zemanek
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Zemanek
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to Use Tables
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Zemanek
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48645
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now a candidate to move to our GUIs forum.
 
Rob Camick
Ranch Hand
Posts: 2557
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can easly make column types in JTable (I use JXTable but in this thing it is the same):
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic