Jtable 不显示列标题。我正在使用向量来填充 JTable,但它似乎仍然不起作用。这是代码:
public class InsertFileToJtable extends AbstractTableModel{
Vector data;
Vector columns;
private String[] colNames = {"col1","col2","col3","col4","col5","col6","col7","col8"};
public InsertFileToJtable() {
String line;
data = new Vector();
columns = new Vector();
try {
FileInputStream fis = new FileInputStream("ProcessList.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");
while (st1.hasMoreTokens())
columns.addElement(st1.nextToken());
while ((line = br.readLine()) != null) {
StringTokenizer st2 = new StringTokenizer(line, " ");
while (st2.hasMoreTokens())
data.addElement(st2.nextToken());
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public int getRowCount() {
return data.size() / getColumnCount();
}
public int getColumnCount() {
return columns.size()-1;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return (String) data.elementAt((rowIndex * getColumnCount())
+ columnIndex);
}
@Override
public String getColumnName(int column) {
return colNames[column];
}
@Override
public Class getColumnClass(int col){
return getValueAt(0,col).getClass();
}
}
下面是从 Main 调用表的方式:
public static void main(String[] args) {
InsertFileToJtable model = new InsertFileToJtable();
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
table.setModel(model);
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setViewportView(tabbedPane);
tabbedPane.addTab("Process",null,table,"");
model.fireTableStructureChanged();
JPanel panel = new JPanel();
panel.add(scrollpane);
frame.add(panel);
}
由于我是新手,我不知道出了什么问题。任何帮助将不胜感激。谢谢!