Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add Row with AbstractTableModel

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot,
 
Reynold Joseph
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stevens Miller
Bartender
Posts: 1223
23
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the posting, Reynold. Helped me solve a problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic