JTable size adjustment after addrow(), JScrollPane  
Author Message
Maximilian





PostPosted: 2004-7-27 17:06:00 Top

java-programmer, JTable size adjustment after addrow(), JScrollPane Hi all.

Basically I'm doing the following:


--snip--
DefaultTableModel myTableModel=
new DefaultTableModel(new String[] {"header1","header2"},0);
JTable myTable=new JTable();
myTable.setModel(myTableModel);

TableScrollPane.setViewportView(myTable);
--snap--

When I do some myTable.addrow(...)s, the table is not resizing istself
appropriately, resulting in data being "cut off" at the bottom.

Does anyone know how to correctly adjust a table's preferred size (or
whatever) after changing its contents to fit the new row count and thus
making it usable in a ScrollPane?

Many thanks in advance,

Maximilian
 
Maximilian





PostPosted: 2004-7-28 3:55:00 Top

java-programmer >> JTable size adjustment after addrow(), JScrollPane Maximilian wrote:

>
> When I do some myTable.addrow(...)s, the table is not resizing istself
> appropriately, resulting in data being "cut off" at the bottom.
>
> Does anyone know how to correctly adjust a table's preferred size (or
> whatever) after changing its contents to fit the new row count and thus
> making it usable in a ScrollPane?
>
> Many thanks in advance,
>
Just given the case that somebody had the same problem...
That's what I was looking for (at least the code does what i wanted):

myTable = new JTable() {
public Dimension getPreferredSize() {
int HeightSum=0;
for(int i=0; i<getRowCount(); i++){
HeightSum+=getRowHeight(i);
}
return new Dimension(SizeOfSolumns(),HeightSum);
}
public Dimension getPreferredScrollableViewportSize(){
return getPreferredSize();
}

public int SizeOfColumns() {
int WidthSum=0;
for (int i=0; i<getColumnCount(); i++) {
WidthSum+=SizeOfColumn(i);
}
if(getParent().getSize().width<WidthSum)
return WidthSum;
return getParent().getSize().width;
}
public int SizeOfColumn(int vColIndex) {
DefaultTableColumnModel colModel =
(DefaultTableColumnModel)getColumnModel();
TableColumn col = colModel.getColumn(vColIndex);
int width = 0;

TableCellRenderer renderer = col.getHeaderRenderer();
if (renderer == null) {
renderer = getTableHeader().getDefaultRenderer();
}
Component comp = renderer.getTableCellRendererComponent(
this, col.getHeaderValue(), false, false, 0, 0);
width=comp.getSize().width;

return width;
}
};

Regards,

Maximilian
 
usenet





PostPosted: 2004-7-28 20:13:00 Top

java-programmer >> JTable size adjustment after addrow(), JScrollPane Maximilian <email***@***.com> wrote:
> Maximilian wrote:
>
>>
>> When I do some myTable.addrow(...)s, the table is not resizing istself
>> appropriately, resulting in data being "cut off" at the bottom.

Please post a compilable example that shows that problem.


>> Does anyone know how to correctly adjust a table's preferred size (or
>> whatever) after changing its contents to fit the new row count and thus
>> making it usable in a ScrollPane?


There is no need to do anything like that.


The code you posted also does not help things in any way, and will pro-
bably also confuse the column layout.



Christian