Prasad Sawant
Prasad Sawant

Reputation: 221

Table shows extra blank columns at the end

I am using jface tableViewer.When table has no data in it ,it shows all columns correctly But when Data gets added to the table it shows extra blank space or column at the end of the table.

Upvotes: 3

Views: 7093

Answers (10)

Srikanth
Srikanth

Reputation: 69

To the end column we need to set the setWidth to window size or shell-size, parent-shell size like 1500,5000

final TableViewerColumn viewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
column.setText(title);
column.setResizable(true);
column.setMoveable(true);
//set the setWidth size upto shell size or set upto to some size like 1000,1500,2000,5000
col.setWidth(comp.getShell().getSize().x); // or col.setWidth(1500) ;
return viewerColumn;

Upvotes: -1

JavaLearner
JavaLearner

Reputation: 1

As a workaround use :

-For Column

use TableColumnLayout for the treeViewer's composite and set appropriate column data for each column using: "tableColumnLayout.setColumnData(column,new ColumnWeightData(...as per your requirement));"

-For Row

Set GridData to the treeViewer's composite and provide height hint using "gridData.heightHint = table.getItemHeight()*numberOfVisibleRows"

Upvotes: 0

tingo
tingo

Reputation: 31

No need for complicated hacks to remove the extra unwanted column space at the end...

Just create a columnLayout:

TableColumnLayout columnLayout = new TableColumnLayout();

and then set it to each of your columns:

columnLayout.setColumnData(YOUR_VIEWER_COLUMN1.getColumn(), new ColumnPixelData(200));
columnLayout.setColumnData(YOUR_VIEWER_COLUMN2.getColumn(), new ColumnWeightData(200, 100));

Finally, set the layout on your parent composite:

parent.setLayout(columnLayout);

Full sample:

public void createPartControl(Composite parent) {
    TableViewer viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);

    TableViewerColumn keyColumn = new TableViewerColumn(viewer, SWT.LEFT);
    TableViewerColumn valueColumn = new TableViewerColumn(viewer, SWT.LEFT);

    TableColumnLayout columnLayout = new TableColumnLayout();
    columnLayout.setColumnData(keyColumn.getColumn(), new ColumnPixelData(200));
    columnLayout.setColumnData(valueColumn.getColumn(), new ColumnWeightData(200, 100));
    parent.setLayout(columnLayout);
}

Upvotes: 1

Ehsan
Ehsan

Reputation: 1285

So simple! Just remove this line in your table commands inside the createContents function:

table.getColumn(i).pack();

Good-luck

Upvotes: 0

Tyagi Akhilesh
Tyagi Akhilesh

Reputation: 860

Thanks Thomas. Your idea worked for me as well, though I was using TableViewer and TableColumn.

Quoting my code so that others can take some hints.

`public void controlResized(ControlEvent e) {

    if ( listOfTableColumns.size() != colProportions.length )
    {
        logger.warn( "Number of columns passed and size of column proportions array are different. " +
                "Columns resizing shall not be effective on GUI window resizing" );
        return;
    }
    Rectangle area = tableBaseComposite.getClientArea();
    Point size = theTable.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    ScrollBar vBar = theTable.getVerticalBar();
    int width = area.width - theTable.computeTrim(0,0,0,0).width - vBar.getSize().x;
    if (size.y > area.height + theTable.getHeaderHeight()) {
        // Subtract the scrollbar width from the total column width
        // if a vertical scrollbar will be required
        Point vBarSize = vBar.getSize();
        width -= vBarSize.x;
    }
    Point oldSize = theTable.getSize();

    if (oldSize.x > area.width) {
        // table is getting smaller so make the columns 
        // smaller first and then resize the table to
        // match the client area width
        int index = 0 ;
        for ( Iterator<TableColumn> iterator = listOfTableColumns.iterator(); iterator.hasNext(); )
        {
            TableColumn column = iterator.next();
            column.setWidth( (int) numberFromPercentage( width, colProportions[index++] ) );
        }
        listOfTableColumns.get( listOfTableColumns.size() - 1).pack();
        theTable.setSize(area.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
        int index = 0;
        theTable.setSize(area.width, area.height);
        for ( Iterator<TableColumn> iterator = listOfTableColumns.iterator(); iterator.hasNext(); )
        {
            TableColumn column = iterator.next();
            column.setWidth( (int) numberFromPercentage( width, colProportions[index++] ) );
        }
        listOfTableColumns.get( listOfTableColumns.size() - 1).pack();
    }
}`

Upvotes: 1

Suryakant B
Suryakant B

Reputation: 1

I used the packAndFillLastColumn() method and it worked for me. But I found one issue with it. My table was created with a border. After using the packAndFillLastColumn() method the border for the row no longer exists. I used the setLinesVisible(true) method within the packAndFillLastColumn() method but still that does not work.

Upvotes: 0

ThomasRS
ThomasRS

Reputation: 8287

I am using TreeViewer + TreeViewerColumn and had this problem too, this workaround might work for your TableViewer too: Programmatically set the size of the last column on parent resize:

    treeViewer.getTree().addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent e) {
            packAndFillLastColumn();
        }
    });

where the action is in

// Resize last column in tree viewer so that it fills the client area completely if extra space.
protected void packAndFillLastColumn() {
    Tree tree = treeViewer.getTree();
    int columnsWidth = 0;
    for (int i = 0; i < tree.getColumnCount() - 1; i++) {
        columnsWidth += tree.getColumn(i).getWidth();
    }
    TreeColumn lastColumn = tree.getColumn(tree.getColumnCount() - 1);
    lastColumn.pack();

    Rectangle area = tree.getClientArea();

    Point preferredSize = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    int width = area.width - 2*tree.getBorderWidth();

    if (preferredSize.y > area.height + tree.getHeaderHeight()) {
        // Subtract the scrollbar width from the total column width
        // if a vertical scrollbar will be required
        Point vBarSize = tree.getVerticalBar().getSize();
        width -= vBarSize.x;
    }

    // last column is packed, so that is the minimum. If more space is available, add it.
    if(lastColumn.getWidth() < width - columnsWidth) {
        lastColumn.setWidth(width - columnsWidth);
    }
}

Works well for me - you might want to set column resizable to false ;-). This can also be called when data in the last column changes (introducting / removing vertical scroll bar).

Upvotes: 5

On windows, you will always get an extra column/row if the net width of all the columns that has been set up is less than the dimension of the table. So its always good to make your columns fit your table, also there is some space left for scroll bars, though I am not very sure about this, but its always better to specify whether you want vertical or horizontal scroll bars.

Upvotes: 0

Prasad Sawant
Prasad Sawant

Reputation: 221

I found eclipse has marked it as WONTFIX.. so can not do much to remove this space..We have tp live with it...:)

Upvotes: -1

dragn
dragn

Reputation: 1050

Just guessing: maybe your columns do not get resized to fill all the table? How do you set the widths of columns? Consider using TableColumnLayout for the table container.

Upvotes: 0

Related Questions