This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes AbstractTreeModel Cannot insert rows correctly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "AbstractTreeModel Cannot insert rows correctly" Watch "AbstractTreeModel Cannot insert rows correctly" New topic

AbstractTreeModel Cannot insert rows correctly

victor charles

Joined: Feb 26, 2004
Posts: 1
Hi, all:
I want to use JTable to show some data in my data file. And I extend AbstractTeeModel to implement my data model. But I cannot get what I expected. I have written a sample code to show my errors. Can you help me?
I have highlighted my questions in the source codes.
Thank you very much.
/* */
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
public class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"1", "2", "3"};
private Vector tableRows = new Vector();

public MyTableModel(){

/* (non-Javadoc)
* @see javax.swing.table.TableModel#getColumnCount()
public int getColumnCount() {
return columnNames.length;

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

/* (non-Javadoc)
* @see javax.swing.table.TableModel#getRowCount()
public int getRowCount() {
return tableRows.size();
/* (non-Javadoc)
* @see javax.swing.table.TableModel#getValueAt(int, int)
public Object getValueAt(int rowIndex, int columnIndex) {
Object[] row = (Object[])tableRows.get(rowIndex);
if (row[columnIndex] == null) {
return new String();

return row[columnIndex];

public Class getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();

public void addRow(Object[] rowData) {
int rowNumber = tableRows.size();
fireTableRowsInserted(rowNumber, rowNumber);

/* */
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JTable;
public class MyFrame {
public static void main(String[] args){
JFrame frame = new JFrame("Test");
Container contentPane = frame.getContentPane();
BorderLayout layout = new BorderLayout();
// final JLabel label = new JLabel("Hello World");
// frame.getContentPane().add(label);
MyTableModel model = new MyTableModel();
JTable table = new JTable(model);
table.setPreferredSize(new Dimension(100, 100));
contentPane.add(table, BorderLayout.CENTER);


Object data[] = new Object[3];
data[0] = new String("0");
data[1] = new String("1");
data[2] = new String("2");

data[0] = new String("3");
data[1] = new String("4");
data[2] = new String("5");
/* I want to get a table like this
0 | 1 | 2
3 | 4 | 5
but I got the following result
3 | 4 | 5
3 | 4 | 5
Can you tell me why? And how I can get the correct result?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535

actually changes the values in the data array. You added the same array as two different rows.
Try this and see what you get for output:
I agree. Here's the link:
subject: AbstractTreeModel Cannot insert rows correctly
It's not a secret anymore!