问题
JTable header not showing...
My JTable header wont show even if add it into a container like JScrollPane...tell me why is it happen and how can i fix it or debug it.. I search through internet and all they saying is add container to your jtable, i did but still my header are not showing.
public void table(){
try{
rs = stat.executeQuery("SELECT * FROM payments;");
Vector<String> header = new Vector<String>();
header.add("PAYMENT");
header.add("AMOUNT");
header.add("MODIFIER");
header.add("DATE MODIFIED");
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while(rs.next()) {
Vector<Object> row = new Vector<Object>();
row.add(rs.getString("description"));
row.add(rs.getString("amount"));
row.add(rs.getString("remarks"));
row.add(rs.getString("date"));
data.add(row);
} // loop
table = new JTable(data, header);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(table);
panel.add(table.getTableHeader());
//panel.removeAll();
//scroll.add(table);
validate();
}catch(Exception e){
System.out.println("Error in table: "+e);
}//try and catch
}
回答1:
Start by removing
panel.add(table.getTableHeader());
The JTable is designed to add it's header to the JScrollPane. An instance of a component can only belong to a one parent/container, the above line is removing it from the scrollpane
Also, change this...
panel.add(table);
To
panel.add(scrollPane);
回答2:
Same problem I have face You have to add the JTable to the JScrollPane then add JscrollPane to JFrame e.g.
JTable table = new JTable();
JScrollPane scrollPane= new JScrollPane(table);
frame.add(scrollPane);
来源:https://stackoverflow.com/questions/19065970/jtable-header-not-showing