用JScrollPane显示Jtable没有显示出表头及全部数据,还报数组越界 10
这就是运行后的效果图,什么都没有,但是用鼠标点击后,就出现一部分数据。启动时报错:Exceptioninthread"AWT-EventQueue-0"java.lang...
这就是运行后的效果图,什么都没有,但是用鼠标点击后,就出现一部分数据。启动时报错:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
at com.lonsine.testjtable.MyTableModel.getValueAt(MyTableModel.java:36)
源码:
public class TestTableModel extends JFrame{
private static final long serialVersionUID = -7126051211946765420L;
public TestTableModel(){
init();
}
private void init(){
setTitle("测试TableModel");
setSize(600,400);
setContentPane(createPane());
setVisible(true);
this.pack();
}
private JScrollPane createPane(){
String[] columnNames={"琴房号","上琴时间","用户名","用户类型","剩余琴时"};
Object[][] data={{"101","2013-01-24 09:34:32","张三","学生",new Integer(150)},
{"102","2013-01-24 10:23:31","李四","学生",new Integer(50)},
{"103","2013-01-25 08:45:01","李丽华","教师",new Integer(100)}};
JTable table=new JTable(new MyTableModel(columnNames,data));
table.setFillsViewportHeight(true);
JScrollPane scp=new JScrollPane(table);
return scp;
}
public static void main(String[] args) {
TestTableModel testmodel=new TestTableModel();
}
}
Model类:
public class MyTableModel extends AbstractTableModel{ private static final long serialVersionUID = -4430218239528468079L;
private String[] columnNames;
private Object[][] data;
public MyTableModel() {
super();
// TODO Auto-generated constructor stub
}
public MyTableModel(String[] columnNames, Object[][] data) { super();
this.columnNames = columnNames;
this.data = data;
}
@Override public int getRowCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
@Override public int getColumnCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override public Object getValueAt(int row, int col) {
// TODO Auto-generated method stub
return data[row][col];
}
@Override public String getColumnName(int col) {
// TODO Auto-generated method stub
return columnNames[col];
}
}
请问这是哪里出问题了啊?第一次用Jtable,不明白啊、
点击后的效果,各位可以把我代码copy到自己环境运行一下,看看错误,这样好找点啊、多谢了
自己已搞定、、、多谢! 展开
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
at com.lonsine.testjtable.MyTableModel.getValueAt(MyTableModel.java:36)
源码:
public class TestTableModel extends JFrame{
private static final long serialVersionUID = -7126051211946765420L;
public TestTableModel(){
init();
}
private void init(){
setTitle("测试TableModel");
setSize(600,400);
setContentPane(createPane());
setVisible(true);
this.pack();
}
private JScrollPane createPane(){
String[] columnNames={"琴房号","上琴时间","用户名","用户类型","剩余琴时"};
Object[][] data={{"101","2013-01-24 09:34:32","张三","学生",new Integer(150)},
{"102","2013-01-24 10:23:31","李四","学生",new Integer(50)},
{"103","2013-01-25 08:45:01","李丽华","教师",new Integer(100)}};
JTable table=new JTable(new MyTableModel(columnNames,data));
table.setFillsViewportHeight(true);
JScrollPane scp=new JScrollPane(table);
return scp;
}
public static void main(String[] args) {
TestTableModel testmodel=new TestTableModel();
}
}
Model类:
public class MyTableModel extends AbstractTableModel{ private static final long serialVersionUID = -4430218239528468079L;
private String[] columnNames;
private Object[][] data;
public MyTableModel() {
super();
// TODO Auto-generated constructor stub
}
public MyTableModel(String[] columnNames, Object[][] data) { super();
this.columnNames = columnNames;
this.data = data;
}
@Override public int getRowCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
@Override public int getColumnCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override public Object getValueAt(int row, int col) {
// TODO Auto-generated method stub
return data[row][col];
}
@Override public String getColumnName(int col) {
// TODO Auto-generated method stub
return columnNames[col];
}
}
请问这是哪里出问题了啊?第一次用Jtable,不明白啊、
点击后的效果,各位可以把我代码copy到自己环境运行一下,看看错误,这样好找点啊、多谢了
自己已搞定、、、多谢! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询