Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

AbstractTreeModel Cannot insert rows correctly

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
*/
}
}
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic