Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dumb question about jtable

 
morten wilken
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i have made my own tablemodel, but cannot get the jtable to display column names?
i have implemented the getColumnName() method in the tablemodel, and i kinda hoped this was enough.
i have also tried to do: jtable.setAutoCreateColumnsFromModel(true);
in the hope that that would help
but how do i do this?
sincerely
morten wilken
 
Robin Underwood
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had no problem getting the column name to display by overriding getColumnName. Maybe the problem is in the code in the overridden method.
 
Robin Underwood
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had no problem getting the column name to display by overriding getColumnName. Maybe the problem is in the code in the overridden method.
 
morten wilken
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi robin,
my code is this:
public String getColumnName(int index) {
System.out.println(fieldInfo[index].getName().trim());
return (fieldInfo[index].getName().trim());
}
i can see that the corrct names gets written to the console, so i know it gets called.
i instantiate the jtable thusly:
TableModel tm = new TableModel(di, fi);
JTable jt = new JTable(tm);
jt.setAutoCreateColumnsFromModel(true);
tablePanel.add(jt);
can you spot any problems here?
sincerely
morten wilken
 
Robin Underwood
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what's wrong. Here's the table model I used:
TableModel dataModel = new AbstractTableModel() {
public String getColumnName(int column) {return cachedFieldInfo[column].getName();}
... other methods overridden ...
};
Also I had setAutoCreateColumnsFromModel(false). You could try this.
I need to leave for a while. Good luck!
 
morten wilken
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i found my problem and just thought i would post it here if anyone else has the same trouble.
i had to put the jtable in a jscrollpane for the header of the tble to show.. not sure why, but now it works
sincerely
morten wilken
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic