File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How do I implement my own table model (using JTable)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I implement my own table model (using JTable)?" Watch "How do I implement my own table model (using JTable)?" New topic
Author

How do I implement my own table model (using JTable)?

Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Hello everyone,
I've created an application that used JDBC etc to connect to a database and get the column names and the contents of each column for me. I had to resort to using a JTextArea to display the data, because I didn't fully understand how to implement my own TableModel. Sun's Java tutorial doesn't do a perfect job here. Most explanation of the JTable class populate the table with vaues at creation either using the contructor that takes vectors or something close etc. I did that too alright, but when I used this with the database, I exceptions thrown because the default Table model of the JTable class expects all the data to be of the same type. But my database contained both numbers and charaters in different columns (right?).
I looked up the JTable class in the Java docs and didn't get it either. Does anyone have expericence with implementing their own table model? Would that individual be kind enough to explain the whole concept to me here... what this method does and what that does and which one to subclass and override etc?
Or can someone link me to a web article or tutorial that treats this topic well, in an understandable manner?

Thanks for the help.


SCJP
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Femi,
try the Tables chapter of the book Swing 2nd ed. by Robinson Vorobiev


SCJP2. Please Indent your code using UBB Code
Vikalp Setya
Greenhorn

Joined: May 10, 2003
Posts: 18
Hi,
You got to write a TableModel implementation for your task. You may write a class that extends either of AbstractTableModel or DefaultTableModel. There are a few methods that u need to implement say getColumnCount(), getRowCount(), and most importantly getValueAt(..). Basically JTabel callbacks these methods to get the data. DefaultTableModel uses a Vector of Vector to store the data. Each row is a Vector while a collection of rows is put in another Vector, just like a 2D array. DefaultTableModel provides some additional functionality like moving columns around, deleting a row etc. I would recommend you to extend AbstractTableModel like :
public class MyTableModel extends AbstractTableModel
{
private int columnCount = 0;
private Vector dataVector = new Vector();
public MyTableModel ()
{
//get the database connection
//execute the query
//get the results set meta data
//get the column count
columnCount = rsmd.getColumnCount();
//iterate through the result set
while(rs.next())
{
Vector thisRow = new Vector();
//fetch the data
for(int i=0 ;i < columnCount; i++)
thisRow.add(rs.getObject(i))
//this row now contains the data of current row
//put this in datavector
dataVector.add(thisRow);
}
}
public int getColumnCount()
{
return columnCount;
}

public int getRowCount()
{
return dataVector.size();
}
public Object getValueAt(int row,int col)
{
//get the row's data vector
Vector rowthRow = (Vector) dataVector.get(row);
Object data = rowthRow.get(col);
return data;
}
}
I think implementing these methods will get the stuff done. Please do some exceptional handling.
For reference, use Core Java Volume II by Cay S Horstmann that has an example of constructing a JTable from database table.


Regards,<br />vikalps
Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Dear Jose,
Thanks for those very helpful links. The chapter in that book that deals with the JTable class contains so much material... I felt a little less stupid really when, under the
18.1.1 JTable
class javax.swing.JTable section,
I read
"JTable is one of the most complex Swing components and keeping track of its constituents and how they interact is intially a challenge. "
I must also add that it really helped to look at the source code of the JTable class and see how it was really designed. The comments by the person who wrote the JTable class, one Philip Milne really helped too.
And dear Viklap,
Thanks so very much for your explanation and especially the accompanying code.... But I'll still need your help on one more thing though...
In attempting to override the getValueAt() method, to render numbers and characters (or Strings) differently, do you have an idea of what the actual code should look like? It seems you could be able to help me here with the actual coding of the method....
Thanks.
Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Greetings!,
This is a follow up question to this one I asked several months ago. I managed to escape using the JTable and implementing my TableModel etc as the application worked OK using JTextArea. Now, there's no escape. I have to use a JTable. I'm making progress but still haven't got it yet. I still need a little help...
Dear Jose Botella , I downloaded the chapters you linked me to back in November and they made a big book.Scared me...
I tried using Vikalp Setya's example (above) - thanks so much Vikalp.

and used


to call it up and had it crash at runtime. In case the error messages would help give an insight into the problem, here it is;

Any useful suggestions would be welcome...
Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How do I implement my own table model (using JTable)?