• 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

JTable set column data type

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22781
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
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
How to Use Tables
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now a candidate to move to our GUIs forum.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic