Here's what I kludged together, it isn't great, but it suits my
immediate needs:
table = new JTable(seqTableModel = new SeqTableModel()){
public void sizeColumnsToFit(int resizingColumn) {
if (resizingColumn == -1) {
// this is what makes it work so bad by default
// it ends up causing all coumns to be resized as a
// division of total width of viewport.getExtent()
//setWidthsFromPreferredWidths(false);
setAllColumnWidths(); // my fix
} else {
super.sizeColumnsToFit(resizingColumn);
}
}
};
table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
public void setAllColumnWidths () {
Component comp;
Dimension d;
TableColumnModel tcm = table.getColumnModel();
TableColumn tc;
int pw;
for (int i = 0 ; i < table.getColumnCount() ; i++) {
comp = table.prepareRenderer(table.getCellRenderer(0, i), 0,
i);
d = comp.getPreferredSize();
tc = tcm.getColumn(i);
if ((pw = tc.getPreferredWidth()) == 75) {
tc.setPreferredWidth(d.width);
tc.setWidth(d.width);
} else {
tc.setWidth(pw);
}
}
}
// when I am about to change the table font...
TableColumnModel tcm = table.getColumnModel();
TableColumn tc;
DefaultTableCellRenderer dtcr;
Dimension d;
int w;
for (int i = 0 ; i < tcm.getColumnCount() ; i++) {
tc = tcm.getColumn(i);
dtcr = (DefaultTableCellRenderer)tc.getCellRenderer();
if (dtcr != null)
tc.setPreferredWidth(dtcr.getPreferredSize().width);
}
table.tableChanged(new TableModelEvent(seqTableModel,
TableModelEvent.HEADER_ROW));
--- "Mancuso, Christopher" <[EMAIL PROTECTED]> wrote:
> Hello,
> I just read you're post on that Advanced swing forum, where you
> posted a query about changing columns widths in a swing table
> programmatically, and I emailing to inquire as to whether you ever
> recieved
> any responses on that topic. And if so, could you possibly forward
> me that
> information? I am having the same problem as you.
>
> Thank you very much.
>
> Chris
>
> >I am trying to resize table columns programatically and don't
> quite
> understand what is going on. The columns will have different
> widths, and
> sometimes I
> >amy be adding or removing columns. It seems like I ought to be
> able to just
> set the column's preferred width. The problems I am having are
> basically
> that
> >I don't understand how the JScrollPane, column preferred widths
> and the
> sizeColumnsToFit() all work together. header width seems to take
> precedent
> >sometimes columns are evenly sized when some should be wider
> columns are
> sized to fit within scrollpane when they should be wider than the
> scrollpane
>
> >inserting or removing a column oftens tends to make the columns
> too small
> and changes the header render back to the default (I don't always
> change the
>
> >header renderer, but I would like to have columns with an up/down
> sort
> indicator) Any big picture sort of explanations would help. The
> documentation I
> >have found is either too vague or outdated.
>
>
__________________________________________________
Do You Yahoo!?
NEW from Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
http://geocities.yahoo.com/ps/info1
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing