/**
* Correctly resizes the table so no phantom columns appear
*/
private static void configureTableResizing(final Composite parent, final Table table, final TableColumn[] cols) {
parent.addControlListener(new ControlListener()
{
public void controlResized(ControlEvent e)
{
Rectangle area= parent.getClientArea();
Point preferredSize= table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
int width= area.width - 2 * table.getBorderWidth();
if (preferredSize.y > area.height) {
// Subtract the scrollbar width from the total column width
// if a vertical scrollbar will be required
Point vBarSize = table.getVerticalBar().getSize();
width -= vBarSize.x;
}
Point oldSize= table.getSize();
int colCount = cols.length;
if (oldSize.x > width) {
// table is getting smaller so make the columns
// smaller first and then resize the table to
// match the client area width
for(TableColumn col : cols){
col.setWidth(width/colCount);
}
table.setSize(width, area.height);
} else {
// table is getting bigger so make the table
// bigger first and then make the columns wider
// to match the client area width
table.setSize(width, area.height);
for(TableColumn col : cols){
col.setWidth(width/colCount);
}
}
}
public void controlMoved(ControlEvent e)
{
}
});
* Correctly resizes the table so no phantom columns appear
*/
private static void configureTableResizing(final Composite parent, final Table table, final TableColumn[] cols) {
parent.addControlListener(new ControlListener()
{
public void controlResized(ControlEvent e)
{
Rectangle area= parent.getClientArea();
Point preferredSize= table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
int width= area.width - 2 * table.getBorderWidth();
if (preferredSize.y > area.height) {
// Subtract the scrollbar width from the total column width
// if a vertical scrollbar will be required
Point vBarSize = table.getVerticalBar().getSize();
width -= vBarSize.x;
}
Point oldSize= table.getSize();
int colCount = cols.length;
if (oldSize.x > width) {
// table is getting smaller so make the columns
// smaller first and then resize the table to
// match the client area width
for(TableColumn col : cols){
col.setWidth(width/colCount);
}
table.setSize(width, area.height);
} else {
// table is getting bigger so make the table
// bigger first and then make the columns wider
// to match the client area width
table.setSize(width, area.height);
for(TableColumn col : cols){
col.setWidth(width/colCount);
}
}
}
public void controlMoved(ControlEvent e)
{
}
});
}
调用:configureTableResizing(parent, table, table.getColumns());