关于jtable添加行的问题
import javax.swing.*;import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.*;
public class testq extends JFrame
{
int row;
int column;
Object[][] cells= {{"001","zhang","88"},
{"002","wang","90"},
{"003","lin","99"},
{"004","liao","60"},
{" "," "," "}
};
String[] columnsName={"NO","Name","Score"};
testq()
{
row=5;
column=3;
JTable table=new JTable(cells,columnsName);
add(new JScrollPane(table),BorderLayout.CENTER);
table.setModel(new TableModel());
}
public static void main(String[]args)
{
testq table=new testq();
table.setVisible(true);
table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table.setSize(500,500);
}
class TableModel extends DefaultTableModel
{
public TableModel()
{
addTableModelListener(new TableModelListener()
{
public void tableChanged(TableModelEvent e)
{
if((String)getValueAt(e.getFirstRow(),e.getColumn())!=" ")
{
System.out.println(getRowCount());
Object[] object=new Object[]{" "," "," " };
addRow(object);
}
}
} );
}
public int getRowCount()
{
return row;
}
public int getColumnCount()
{
return column;
}
public Object getValueAt(int row ,int column)
{
return cells[row][column];
}
public String getColumnName(int c)
{
return columnsName[c];
}
public boolean isCellEditable(int row,int column)
{
return true;
}
}
}
为什么每次运行时都会出现越界的异常???
搜索更多相关的解决方案:
jtable
----------------解决方案--------------------------------------------------------
太花呢,看的不怎么懂,写电注释啊
----------------解决方案--------------------------------------------------------