Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes JTable + AbstractTableModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable + AbstractTableModel" Watch "JTable + AbstractTableModel" New topic
Author

JTable + AbstractTableModel

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
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


All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
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

Joined: Jan 24, 2001
Posts: 476
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

Joined: Feb 04, 2001
Posts: 400
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

Joined: Nov 26, 2000
Posts: 133
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).]


<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>"Those who cast the votes decide nothing. Those who count the<BR>votes decide<BR>everything." <BR> -Joseph Stalin<HR></BLOCKQUOTE>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JTable + AbstractTableModel
 
Similar Threads
JTable
JTable
JTable
JTable
JTable