Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Wrap Text on SWT Table Cell (Wrap the table text as like excel Sheet column)
icon13.gif  Wrap Text on SWT Table Cell [message #1748074] Fri, 18 November 2016 10:47
surekha somisetty is currently offline surekha somisettyFriend
Messages: 18
Registered: April 2016
Junior Member
Hi Friends,

I am doing wrap text on swt table column , but it is not working when use enter line by line , how can i give that functionality to my swt table, i want to warp the text and increase the table column size paralley if any idea on that please explain below is my example :

I need to change column height and width change dynamically as per text entered.



import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class TableCellEditorComboTextButton {
private static Text text_1;
private static Text text_2;
private static Text text_3;
private static Text text_4;

public static void main(String[] args) {

Display display = new Display();
Shell shell = new Shell(display);
shell.setMaximized(true);
FillLayout fl_shell = new FillLayout();
shell.setLayout(fl_shell);
shell.setText("Multiple lines in a TableItem");

final Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setLinesVisible(true);
table.setHeaderVisible(true);
table.setLinesVisible(true);

int columnCount = 4;
for (int i = 0; i < columnCount; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Column " + i);
}
// for item count
for (int i = 0; i < 10; i++) {
TableItem tableitem = new TableItem(table, SWT.NONE);

TableEditor editor = new TableEditor(table);

text_1 = new Text(table, SWT.WRAP);
editor.grabHorizontal = true;
text_1.setText("abcdefghijklmnopqrstuvwxyz");
editor.setEditor(text_1, tableitem, 0);

TableEditor editorCN = new TableEditor(table);
text_2 = new Text(table, SWT.NONE);
editorCN.grabHorizontal = true;
editorCN.setEditor(text_2, tableitem, 1);

TableEditor editorSN = new TableEditor(table);
text_3 = new Text(table, SWT.NONE);
editorSN.grabHorizontal = true;
editorSN.setEditor(text_3, tableitem, 2);

TableEditor editorPN = new TableEditor(table);
text_4 = new Text(table, SWT.NONE);
editorPN.grabHorizontal = true;
editorPN.setEditor(text_4, tableitem, 3);

}

Listener paintListener = new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.MeasureItem: {
TableItem item = (TableItem) event.item;
String text = getText(item, event.index);
Point size = event.gc.textExtent(text);
event.width = size.x;
event.height = Math.max(event.height, size.y);
break;
}
case SWT.PaintItem: {
TableItem item = (TableItem) event.item;
String text = getText(item, event.index);
Point size = event.gc.textExtent(text);
int offset2 = event.index == 0 ? Math.max(0,
(event.height - size.y) / 2) : 0;
event.gc.drawText(text, event.x, event.y + offset2, true);
break;
}
case SWT.EraseItem: {
event.detail &= ~SWT.FOREGROUND;
break;
}
}
}

String getText(TableItem item, int column) {
String text = item.getText(column);
if (column != 0) {
int index = table.indexOf(item);
if ((index + column) % 3 == 1) {
text += "\nnew line";
}
if ((index + column) % 3 == 2) {
text += "\nnew line\nnew line";
}
}
return text;
}

};
table.addListener(SWT.MeasureItem, paintListener);
table.addListener(SWT.PaintItem, paintListener);
table.addListener(SWT.EraseItem, paintListener);
for (int i = 0; i < columnCount; i++) {
table.getColumn(i).pack();
}

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

Previous Topic:Wrap Text on SWT Table Cell
Next Topic:Wrap Text on SWT Table Cell
Goto Forum:
  


Current Time: Tue Dec 12 15:50:29 GMT 2017

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

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