Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding new columns and rows to JTable  RSS feed

 
Simran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I want to add some rows or some columns to existing JTable and this the method which I'm using and calling this method when add button is pressed in the frame which contains JTable.
public ListSelectionModel setModel()
{
this.getSelectionModel();
int iMax = selectionModel.getMaxSelectionIndex();
int newMax = iMax + 10;
selectionModel.addSelectionInterval(iMax, newMax);
repaint();
return selectionModel;
}
Can anybody tell me how you can add new rows and columns to JTable.
Thanks.
Simran
 
Bilal Ahmed
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You have to use the DefaultColumnModel's addRow(), addColumn()
methods to add rows and columns in the existing JTable. Below is the code
DefaultTableModel tModel = new DefaultTableModel();
JTable table = new JTable(tModel);
.......
//To Add Columns
tModel.addColumn("Product");
tModel.addColumn("Quantity");
tModel.addColumn("Price");
//To Add rows
Object obj[] = new Object[tModel.getColumnCount()];
tModel.addRow(obj);
Good Luck!


------------------
 
Simran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bilal Ahmed ,
But I want to use AbstractTableModel only.So how can I add columns and rows if my class is extending AbstractTableModel.
As I'm reading all contents of one text file into JTable and want to add news rows and columns to put more data into it.
One more thing I want to show first row of the JTable colored how can i do it.
Thanks.
Simran
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!