• Post Reply Bookmark Topic Watch Topic
  • New Topic

How To add data from database to JTable

 
Anthony Levis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i have create a JTable and i want retrieve the data from database. i want to add the data to the column. How to do it??can anyone give me some guidance and some example coding.

thank you very much.....
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You require a TableModel. It is confusing at first, but I learned by following the sun tutorial on Tables and table models. My advice is to go through Sun Table Tutorial

Anything I would say here would just be repeating what they have.
[ December 28, 2004: Message edited by: Tim McGuire ]
 
Dave Mace
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One approach you can take is to write your own class extending AbstractTableModel which will be used as the model for your JTable. If you do extend this class, there are 3 methods that need to be overwritten:

public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);

Also this model is where you will handle making cells non-editable...changing cell renderers so that you can do things like implement JComboBox instead of the default renderer, etc etc.

Do a quick google on AbstractTableModel and you'll find plenty of resources.

David
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!