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 Add Row with 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 "Add Row with AbstractTableModel" Watch "Add Row with AbstractTableModel" New topic
Author

Add Row with AbstractTableModel

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

I have searched this site for how to add and remove ROWS from a JTable using a AbstractTableModel, but every response replies with "Use DefaultTableModel".
I have also searched On google for an example or some sort, but have come up empty handed.
Does anyone know how to do this? What I want is to basically add an Empty Row that I can type the data directly into the JTable. I do not want to prompt the user for the data first.
here is my code so far


GenRocket - A Test Data Generation Platform
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Ok, I have solved my own problem, but now I have a new problem. When I insert initial data when the Table is created, all the data in each cell is surrouned by [ ]. But if I edit the data, then those are gone.
Does anyone know why this is happening?

Thanks for any help.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Ok, once again, I have answered my own question. I just don't have a solution for it yet short of parsing through each string and removing the []. But, because it is a Vector within a Vector is why there are the []. I would like to know if there is an easy way to get rid of those brackets though.
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi Gregg,
You don't need to use a 2dim Vector if you are implementing you own table model. In your case (as I understand it) you have a one column table. So a list of strings is all you need to store the data in your model:

If you change your mind and you want to add other columns than you might choose to make a class that describes one column. Say "Person" that has as Attributes "Age", "Nickname", etc. Your table model can then take a list of Person objects and map them to the table.
cheers
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
I forgot,
Reynold Joseph
Greenhorn

Joined: Dec 31, 2009
Posts: 2
I've successfully inserted a row into a JTable using AbstractTableModel , here is the code for the table model ....

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.*;
import java.sql.*;
import database.*;//user created package
class MyModel extends AbstractTableModel{

String[] columnNames = new String[0] ;
Vector<String[]> rows_data = new Vector<String[]>() ;

MyModel(ResultSet results){
setResultSet(results) ;
}

public void setResultSet(ResultSet results){
try{
ResultSetMetaData mdata = results.getMetaData() ;
int columns = mdata.getColumnCount() ;
columnNames = new String[columns] ;

for(int i=0;i<columns;i++){
columnNames[i] = mdata.getColumnLabel(i+1) ;
}

rows_data.clear() ;
String[] rowData ;

while(results.next()){
rowData = new String[columns] ;
for(int i=0;i<columns;i++)
rowData[i] = results.getString(i+1) ;
rows_data.addElement(rowData) ;
}
fireTableChanged(null);

}catch(SQLException se){
se.printStackTrace() ;
}

}
public void insertEmptyRow(){
String[] rowData = new String[columnNames.length] ;
for(int i=0;i< columnNames.length;i++)
rowData[i] = "" ;
rows_data.addElement(rowData) ;
fireTableChanged(null);
}

public int getColumnCount(){
return columnNames.length ;
}
public int getRowCount(){
return rows_data == null ? 0:rows_data.size() ;
}

public String getValueAt(int row,int column){
return rows_data.elementAt(row)[column] ;
}

public String getColumnName(int column){
return columnNames[column];
}

}

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Thanks for sharing, but a) please Use Code Tags, and b) did you know this thread was already 7.5 years old?

Also, don't fire an entire table changed event if you just add one single row. There are separate fire methods for that, I suggest you use them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 377
    
    1

Thanks for the posting, Reynold. Helped me solve a problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add Row with AbstractTableModel
 
Similar Threads
problem displaying jtable values
jtable
Swing--JTable in JInternalFrame
Header in JTable is not showing up.PLEASE...... IT IS URGENT
JTable as User's input (?)