Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable + AbstractTableModel

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a table that has one of its 7 columns editable. All the data that comes to the JTable is of type String. Now, what I needed was to make editable column aligned right. I read that in AbstractTableModel I can implement method: public Class getColumnClass(int column){} by changing it to:
public Class getColumnClass(int c){
return (c == 1) ? Number.class : String.class;
}
When I did this, my column did align right, but now I can select it, but cannot edit it as before. Does anybody have any ideas on why this would happen?
thanks,
Alex
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alex Kravets:
I have a table that has one of its 7 columns editable. All the data that comes to the JTable is of type String....I read that in AbstractTableModel I can implement method: public Class getColumnClass(int column){} by changing it to:
public Class getColumnClass(int c){
return (c == 1) ? Number.class : String.class;
}

Hi Alex,
The class of the column is used by the table to know what values are possible to include in the column, if all the data are type String, why you need to make a Number.class as a possible option ?
try change the getColumnClass method into :
public Class getColumnClass(int c){
return getValueAt(0,c).getClass();
}
regard's
stevie

[This message has been edited by Stevie Kaligis (edited November 04, 2001).]
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the thing is that my data comes from the database and is formatted into a string, which is fed into JTable. All the data in the JTable is in String format, so it's left-aligned by default. I need to make it right-aligned. That's why I thought I would make the column display integers this way it's going to align right by default. It works, but I cannot modify the data in the cell of the column.
thanks,
Alex
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah...That's what I tought,
have you tried using Wraper Class, for example :
use type Integer for that column (let say for column 1), and you hold the data on obArrayData[][],
modify setValueAt method :

note :
you must make sure that the data for that column is a String of number ("1", "2", "3", etc...) not ("12F", etc...).
you can create another method (ex : getNumberString, that return String object) to check the string for that column, then you modify the code at setValueAt method to :
obArrayData[row][col] = new Integer(getNumberString(value.toString()));
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch! That's a lot of kludging to get a simple result. All you really need to do is set your own renderer for the column in question. You can even put it in an anonymous inner class. Here's how I did it:

Worked like a charm for me!
e

[This message has been edited by eric moon (edited November 06, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic