gpt4 book ai didi

java - JTable 中没有标题

转载 作者:行者123 更新时间:2023-11-29 09:49:23 24 4
gpt4 key购买 nike

Jtable 不显示列标题。我正在使用 vector 来填充 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);
}

因为我是新手,我不知道哪里出了问题。任何帮助将不胜感激。谢谢!

最佳答案

仅当表格位于 JScrollPane 中时,标题才会自动添加。如果它不包含在 scrollPane 中,您将必须手动管理标题。在您的代码中,它最初被添加到 scrollPane,但后来又被删除 - 通过将表格添加到 tabbedPane。可能不是您想要的,而是将 scrollPane 添加到选项卡:

JScrollPane scrollpane = new JScrollPane(table);
//scrollpane.setViewportView(tabbedPane);
tabbedPane.addTab("Process",null,scrollPane,"");

关于java - JTable 中没有标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12551751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com