我在我的项目中使用我的 JTable,我的 JTableModel。我无法用我的列标题和空数据开始表格。你能帮帮我吗?谢谢你。这是我的代码
主代码类
public class MainCode extends JFrame{
public MainCode(){
...........other codes here........
MyTableModel tm= new MyTableModel();
MyTable table=new MyTable(tm);
//JTable table=new JTable(tm); if I write this line,I see column names.
table.setPreferredScrollableViewportSize(new Dimension(480,80));
table.setFillsViewportHeight(true);
JScrollPane scrollPanetable=new JScrollPane(table);
frame.getContentPane().add(scrollPanetable)
........another codes...........
}
}
MyTable 类
public class MyTable extends JTable{
public MyTable(){
}
public MyTable(int row,int col){
super(row,col);
}
@Override
public void tableChanged(TableModelEvent e){
super.tableChanged(e);
repaint();
System.out.println("public void tableChanged(TableModelEvent e)");
}
}
MyTableModel 类
public class MyTableModel extends AbstractTableModel{
private String[] columnNames;
private Object[][] data;
public MyTableModel(){
super();
columnNames=new String[]{"A","B","C"};
data=new Object[][]{ {null,null,null}};
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
它不是以列名开头的。这段代码有什么问题。我看到下图。