请各位大侠帮帮忙,以下这段代码为何表中的标题总是显示最后一个标题,我希望还是用AbstractTableModel() 这个类来实现显示标题,因为我还得通过它来返回数据库中的信息,然后通过table显示出数据。如果你们有更好的方法,请多多指教,谢谢!!
package student;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import com.borland.dbswing.*;
import java.util.*;
public class Untitled1 extends JFrame{
Vector vector;
AbstractTableModel tm;
String head1[]={"id","name","age","class"};
String head2[]={"hello"};
String title[];
JTabbedPane jTabbedPane1 = new JTabbedPane();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
TableScrollPane tableScrollPane1 = new TableScrollPane();
TableScrollPane tableScrollPane2 = new TableScrollPane();
public Untitled1() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,400);
this.setVisible(true);
}
public static void main(String[] args) {
Untitled1 untitled1 = new Untitled1();
}
private void jbInit() throws Exception {
title=head1;
ininTable();
JdbTable jdbTable1 = new JdbTable(tm);
jdbTable1.setAutoResizeMode(jdbTable1.AUTO_RESIZE_ALL_COLUMNS);
this.getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
jTabbedPane1.add(jPanel1, "jPanel1");
jPanel1.add(tableScrollPane1, null);
tableScrollPane1.getViewport().add(jdbTable1, null);
title=head2;
ininTable();
JdbTable jdbTable2 = new JdbTable(tm);
jTabbedPane1.add(jPanel2, "jPanel2");
jPanel2.add(tableScrollPane2, null);
tableScrollPane2.getViewport().add(jdbTable2, null);
}
public void ininTable(){
vector=new Vector();
tm = new AbstractTableModel() {
public int getColumnCount() {
return title.length;
}
public int getRowCount() {
return vector.size();
}
public Object getValueAt(int row, int column) {
if (!vector.isEmpty()) {
return ( (Vector) vector.elementAt(row)).elementAt(column);
}
else {
return null;
}
}
public void setValueAt(Object value, int row, int column) {
}
public String getColumnName(int column) {
return title[column];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int column) {
return false;
}
};
}
}
image1.bmp (16.58k)