Eclipse Community Forums - RDF feed
https://www.eclipse.org/forums/
Eclipse Community ForumsTable Viewer - Can the extra padding on each table cell be removed
https://www.eclipse.org/forums/index.php/mv/msg/209118/669881/#msg_669881
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 ?
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);
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]]>Grant Gayed2011-05-12T14:36:34-00:00