Adding a row in JTable like MS Access

Hello!

I am trying to add a new row to the JTable which is of AbstractTable Model.

I achieved this by just returning one extra count from the method getRowCount. But I do not want to do it.

I am trying to write a method which will do this action. Can anyone help me in this please? I am actually new to the JAVA world and I need assistance in coding. Thanks for your help in advance.

Regards

Irfaan

[436 byte] By [Irfan_dcrtia] at [2007-11-27 9:37:59]
# 1
if you look at the methods for AbstractTableModel in the api it doesn't have an addRow method while DefaultTableModel has and you can use or override that method...I'm not use in using AbstractTableModel.
Yannixa at 2007-7-12 23:10:28 > top of Java-index,Desktop,Core GUI APIs...
# 2
Yeah.. thats the actual problem.... How do we add a new row then in an AbstractTableModel?
Irfan_dcrtia at 2007-7-12 23:10:28 > top of Java-index,Desktop,Core GUI APIs...
# 3
I can't think of any other way except using DefaultTableModel.or maybe you can try this.table having an Abstract Model;DefaultTableModel model = (DefaultTableModel)table.getModel();model.addRow(new Vector());
Yannixa at 2007-7-12 23:10:28 > top of Java-index,Desktop,Core GUI APIs...
# 4
Actually I did that before.. It gave me a ClassCastException...
Irfan_dcrtia at 2007-7-12 23:10:28 > top of Java-index,Desktop,Core GUI APIs...
# 5

just check this demo,

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Vector;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.table.AbstractTableModel;

import javax.swing.table.DefaultTableModel;

public class AddAndDeleteRowUsingAbstractTableModel extends JFrame implements ActionListener

{

JTabletable = null;

JButtonbtn= new JButton("Add");

JButtonbtn1 = new JButton("remove");

MyTableModel mytable;

public AddAndDeleteRowUsingAbstractTableModel()

{

mytable = new MyTableModel();

table = new JTable(mytable);

btn.addActionListener(this);

btn1.addActionListener(this);

getContentPane().add(btn1, "North");

getContentPane().add(new JScrollPane(table), "Center");

getContentPane().add(btn, "South");

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

pack();

}

public static void main(String[] args)

{

new AddAndDeleteRowUsingAbstractTableModel();

}

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == btn)

{

mytable.addRow();

}

else if (e.getSource() == btn1)

{

mytable.removeRow();

}

}

class MyTableModel extends AbstractTableModel

{

Vector data = new Vector();

public MyTableModel(){

Vector v= new Vector();

v.addElement("1");

v.addElement("1");

v.addElement("1");

v.addElement("1");

data.addElement(v);

}

public int getRowCount()

{

return data.size();

}

public int getColumnCount()

{

Vector v =(Vector)data.get(0);

return v.size();

}

public void addRow()

{

Vector v= new Vector();

v.addElement("1");

v.addElement("1");

v.addElement("1");

v.addElement("1");

data.addElement(v);

fireTableRowsInserted(data.size(), data.size());

}

public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

}

public void removeRow()

{

data.remove(data.size() - 1);

fireTableRowsInserted(data.size(), data.size());

}

public Object getValueAt(int rowIndex, int columnIndex)

{

return ((Vector)data.get(rowIndex)).get(columnIndex);

}

}

}

hope it helps

dayanandabva at 2007-7-12 23:10:28 > top of Java-index,Desktop,Core GUI APIs...
# 6
Hey Dayanand...Thanks... I just ran this code and it worked,... But, I am not able to edit any cell in the table... Why?
Irfan_dcrtia at 2007-7-12 23:10:28 > top of Java-index,Desktop,Core GUI APIs...
# 7
add this method AbstractTableModel class public boolean isCellEditable(int rowIndex, int columnIndex) {return true;}
dayanandabva at 2007-7-12 23:10:28 > top of Java-index,Desktop,Core GUI APIs...
# 8
Hey, Actually I got it... I had to program in the setValueAt function... Cool.... Thanks buddy... Will get back if I need any more assistance. Thanks once again!Irfaan
Irfan_dcrtia at 2007-7-12 23:10:28 > top of Java-index,Desktop,Core GUI APIs...
# 9
Hey Guru...Does the removeRow method remove the last row always? or the selected row/rows?I want to delete the row selected....Irfaan
Irfan_dcrtia at 2007-7-12 23:10:28 > top of Java-index,Desktop,Core GUI APIs...
# 10
Why do you insist an creating a custom TableModel when the DefaultTableModel already supports this?
camickra at 2007-7-12 23:10:28 > top of Java-index,Desktop,Core GUI APIs...
# 11
you can remove by specifing the which row you want.by giving index value to remove method
dayanandabva at 2007-7-12 23:10:28 > top of Java-index,Desktop,Core GUI APIs...
# 12
> you can remove by specifing the which row you want. by giving index value to remove method Or you can use the DefaultTableModel which already supports this method.
camickra at 2007-7-12 23:10:28 > top of Java-index,Desktop,Core GUI APIs...