Reputation: 399
The Jtable doesn't show column headers. I'm using vectors to populate the JTable, but still it doesn't seem to work. Here's the code:
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();
}
}
Here's how the table is called from 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);
}
As I'm a newbie, I dont have any clue of what's wrong. Any help would be highly appreciated. Thanks!
Upvotes: 1
Views: 543
Reputation: 347194
In order for the table to be able to display it's headers, you need to add the table to a scroll pane.
You started out okay, but then went and removed it by changing the scroll pane's viewport
// This was good
JScrollPane scrollpane = new JScrollPane(table);
// Then you changed it
scrollpane.setViewportView(tabbedPane);
// Then you added it to the tabbed pane
tabbedPane.addTab("Process",null,table,"");
Try adding the table back onto a scroll pane and add that to the tabbed pane
Upvotes: 5
Reputation: 51525
The header is added automatically only if the table is in a JScrollPane. If it's not contained in the scrollPane, you'll have to manage the header manually. In your code, it is added initially to the scrollPane, but later removed again - by adding the table to the tabbedPane. Probably not what you want, instead add the scrollPane to the tab:
JScrollPane scrollpane = new JScrollPane(table);
//scrollpane.setViewportView(tabbedPane);
tabbedPane.addTab("Process",null,scrollPane,"");
Upvotes: 6