• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Add Row with AbstractTableModel

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot,
 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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];
}

}

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the posting, Reynold. Helped me solve a problem.
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic