[英]How to update the view of JTable after adding a new row?
This is my TableModel, I have extended AbstractTableModel 这是我的TableModel,我已经扩展了AbstractTableModel
class CustomTableModel extends AbstractTableModel
{
String[] columnNames = {"Name","Contact","eMail","Address","City","Pin","State","Type","ID"};
Vector<String[]> data = new Vector<String[]>();
CustomTableModel()
{
try
{
//Using JDBC connection//
while(rs.next())
{
String[] s=new String[9];
s[0]=rs.getString(1);
//System.out.println(s[0]);
s[1]=rs.getString(2);
s[2]=rs.getString(3);
s[3]=rs.getString(4);
s[4]=rs.getString(5);
s[5]=rs.getString(6);
s[6]=rs.getString(7);
s[7]=rs.getString(8);
s[8]=rs.getString(9);
data.add(s);
}
}
catch(Exception e)
{
System.out.println("the exception is :"+e.toString());
}
}
public int getColumnCount() {
int columnCount = columnNames.length;
return columnCount;
}
public int getRowCount() {
int rowCount = data.size();
return rowCount;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data.get(rowIndex)[columnIndex];
}
public String getColumnName(int column) {
return columnNames[column];
}
public void removeRow(int r)
{
for(int i=0;i<data.size();i++)
{
String[] s = (String[])data.get(i);
if(s[0]==getValueAt(r,0))
{
try
{
//using JDBC connections to delete the data from DB//
//also removing the value from data and also updating the view//
data.remove(data.get(i));
fireTableRowsDeleted(r, r);
}
catch (Exception e)
{
System.out.println(e.toString());
}
break;
}
}
}
//I am using the following code to update the view but it doesnot work//
public void addRow(String[] a)
{
data.add(a);
fireTableRowsInserted(data.size() - 1, data.size() - 1);
}
}
I have a table class which extends CustomTableModel . 我有一个扩展CustomTableModel的表类。
class table extends CustomTableModel
{
final JButton editButton = new JButton("Edit");
final JButton deleteButton = new JButton("Delete");
final JTable mytable = new JTable(new CustomTableModel());
.
.
.
}
I have a add button , and in its action listener i use the following code to pass the values that i wanted to add. 我有一个添加按钮,在其动作侦听器中,我使用以下代码传递要添加的值。
String[] a = {"a","b","c","d","e","f","g","h","i"};
table myTableObj = new table();
myTableObj.addRow(a);
Pls let me know where i am going wrong . 请让我知道我要去哪里错了。 Thanks
谢谢
Pls let me know where i am going wrong .
请让我知道我要去哪里错了。 Thanks
谢谢
String[] a = {"a","b","c","d","e","f","g","h","i"};
table myTableObj = new table();
myTableObj.addRow(a);
code lines talking about 谈论的代码行
create a new row 创建一个新行
create a new JTable
创建一个新的
JTable
row is added to a new JTable
行添加到新的
JTable
result is that a new JTable
is never added to visible Swing GUI 结果是从未将新的
JTable
添加到可见的Swing GUI中
don't do that, why is a new JTable
recreated on every JButton
s event 不这样做,为什么在每个
JButton
的事件上都重新创建一个新的JTable
add String[] a...
to the CustomTableModel
directly 将
String[] a...
直接添加到CustomTableModel
for better help sooner post an SSCCE , short, runnable, compilable 为了更好地帮助,尽快发布SSCCE ,简短,可运行,可编译
The table class makes no sense. 表类没有任何意义。 It is supposed to be a TableModel that shoud be set into a JTable.
应该是应该设置到JTable中的TableModel。 Instead you have JTable declared as a field inside this table class (which should be Table btw according to Java naming convention).
相反,您已将JTable声明为此表类内的字段(根据Java命名约定应为Table btw)。 The result is that when constructing a new table object, a JTable is constructed inside it with another CustomTableModel inside.
结果是,在构造一个新的表对象时,将在其中构造一个JTable,并在其中构造另一个CustomTableModel 。 So the tableModel you are adding rows into is not the tableModel actually used by your JTable.
因此,您要向其中添加行的tableModel不是JTable实际使用的tableModel。
您还可以使用myCustomTable.fireTableStructureChanged();。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.