Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » Table Viewer - Can the extra padding on each table cell be removed
Table Viewer - Can the extra padding on each table cell be removed [message #669881] Wed, 11 May 2011 23:35 Go to next message
Pavan K Immaneni is currently offline Pavan K Immaneni
Messages: 20
Registered: July 2009
Junior Member
I have a table viewer with 2 rows and 80 columns created.

Each of the table cells would contain a maximum of 2 characters and based on this I am trying to remove the extra padding in each cell and could not figure this out.

Here is how the code looks like..

// Code to create the table viewer and columns
int style = SWT.BORDER | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
TableViewer tableViewer = new TableViewer(parent, style);

int maximumWidth = 80;
String tableColumnNames[] = new String[maximumWidth];
for (int i = 0; i < maximumWidth; i++){
tableColumnNames[i] = String.valueOf(i);
TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.NONE);
column.getColumn().setWidth(30);
}

// Code to set text in to each cell
// tableViewer.setInput(model);

// Now that we've set the text into the columns,
// we call pack() on each one to size it to the
// contents.
for (int i = 0; i < tableViewer.getTable().getColumnCount(); i++) {
TableColumn column = tableViewer.getTable().getColumn(i);
column.pack();
}

Even with the above code, I see that there is lot of extra padding on each table cell. Is it possible to remove the extra cell padding in each table cell ?

Any other ideas ? Please help.

Thanks,
Pavan
Re: Table Viewer - Can the extra padding on each table cell be removed [message #670070 is a reply to message #669881] Thu, 12 May 2011 14:36 Go to previous message
Grant Gayed is currently offline Grant Gayed
Messages: 2151
Registered: July 2009
Senior Member
Hi Pavan,

The only way to do this is to draw the item texts yourself, and to specify
their widths so that their columns will pack() tighter. The snippet below
demonstrates this at the SWT level (table1 is a typical Table, while table2
has the three custom draw callbacks added). TableViewer exposes the SWT
Table's custom draw functionality through its owner draw facilities, for an
example of using them see
http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.jface. snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippe ts/viewers/Snippet010OwnerDraw.java?view=markup .
Also, for more info on using custom draw in Tables and Trees see article
http://www.eclipse.org/articles/article.php?file=Article-Cus tomDrawingTableAndTreeItems/index.html .

public class Main {
static int COLUMN_COUNT = 5;
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(10,10,600,200);

Table table1 = createTable(shell);
table1.setBounds(10,10,200,150);

final Table table2 = createTable(shell);
table2.setBounds(300,10,200,150);
table2.addListener(SWT.MeasureItem, new Listener() {
public void handleEvent(Event event) {
/* ensure that the columns will pack() tightly */
int[] widths = (int[])event.item.getData();
if (widths == null) {
widths = new int[COLUMN_COUNT];
TableItem item = (TableItem)event.item;
GC gc = new GC(table2);
for (int i = 0; i < COLUMN_COUNT; i++) {
String text = item.getText(i);
widths[i] = gc.stringExtent(text).x;
}
gc.dispose();
item.setData(widths);
}
event.width = widths[event.index];
}
});
table2.addListener(SWT.EraseItem, new Listener() {
public void handleEvent(Event event) {
/* tell the native to table to not draw the foreground content,
we'll do it */
event.detail &= ~SWT.FOREGROUND;
}
});
table2.addListener(SWT.PaintItem, new Listener() {
public void handleEvent(Event event) {
/* draw the foreground content */
TableItem item = (TableItem)event.item;
event.gc.drawString(item.getText(event.index), event.x,
event.y);
}
});

for (int i = 0; i < COLUMN_COUNT; i++) {
table1.getColumn(i).pack();
table2.getColumn(i).pack();
}

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}

static Table createTable(Composite parent) {
Table table = new Table(parent, SWT.NONE);
table.setLinesVisible(true);
for (int i = 0; i < COLUMN_COUNT; i++) {
new TableColumn(table, SWT.NONE);
}
TableItem item = new TableItem(table, SWT.NONE);
for (int i = 0; i < COLUMN_COUNT; i++) {
item.setText(i, "text");
}
return table;
}
}

HTH,
Grant


"Pavan K Immaneni" <forums-noreply@eclipse.org> wrote in message
news:iqf609$e5u$1@news.eclipse.org...
>I have a table viewer with 2 rows and 80 columns created.
>
> Each of the table cells would contain a maximum of 2
> characters and based on this I am trying to remove the
> extra padding in each cell and could not figure this out.
>
> Here is how the code looks like..
>
> // Code to create the table viewer and columns
> int style = SWT.BORDER | SWT.FULL_SELECTION |
> SWT.HIDE_SELECTION;
> TableViewer tableViewer = new TableViewer(parent, style);
>
> int maximumWidth = 80;
> String tableColumnNames[] = new String[maximumWidth];
> for (int i = 0; i < maximumWidth; i++){
> tableColumnNames[i] = String.valueOf(i);
> TableViewerColumn column = new
> TableViewerColumn(tableViewer, SWT.NONE);
> column.getColumn().setWidth(30);
> }
>
> // Code to set text in to each cell
> // tableViewer.setInput(model);
>
> // Now that we've set the text into the columns,
> // we call pack() on each one to size it to the
> // contents.
> for (int i = 0; i < tableViewer.getTable().getColumnCount();
> i++) {
> TableColumn column = tableViewer.getTable().getColumn(i);
> column.pack();
> }
>
> Even with the above code, I see that there is lot of extra
> padding on each table cell. Is it possible to remove the
> extra cell padding in each table cell ?
>
> Any other ideas ? Please help.
>
> Thanks,
> Pavan
Previous Topic:JFace TreeViewer
Next Topic:How do I scroll to an ExpandItem in an ExpandBar?
Goto Forum:
  


Current Time: Wed Oct 22 23:15:26 GMT 2014

Powered by FUDForum. Page generated in 0.01746 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software