aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "AbstractTreeModel Cannot insert rows correctly" Watch "AbstractTreeModel Cannot insert rows correctly" New topic
Author

AbstractTreeModel Cannot insert rows correctly

victor charles
Greenhorn

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.
/* MyTableModel.java */
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();
tableRows.addElement(rowData);
fireTableRowsInserted(rowNumber, rowNumber);
}

}
/* MyFrame.java */
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();
contentPane.setLayout(layout);
// 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);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

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

data[0] = new String("3");
data[1] = new String("4");
data[2] = new String("5");
model.addRow(data);
/* 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
This

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:
 
 
subject: AbstractTreeModel Cannot insert rows correctly