e4 load table in Mpart using parthandler - java

i am a beginner in E4 and at all in JAVA.
i created a partHandler and i want to display table which is created in other class in this part. table is cteated in TreeTableCreation class. it would be nice if you can help.
now it creates a Tab, but no table inside, and throws Nullpointer exception. tahnk you.
public class DynamicPartHandlerCode {
#Execute
public void execute(EModelService modelService, MApplication application, final IEclipseContext context,
#Named(IServiceConstants.ACTIVE_SHELL) final Shell shell) {
EPartService partService = context.get(EPartService.class);
// create new part
MPart mPart = modelService.createModelElement(MPart.class);
String id = "org.testeditor.ui.uidashboard.partdescriptor.0";
mPart = partService.findPart(id);
if (mPart == null) {
mPart = partService.createPart(id);
}
List<MPartStack> stacks = modelService.findElements(application, null, MPartStack.class, null);
stacks.get(2).getChildren().add(mPart);
((TreeTableCreation) mPart.getObject()).createTable();
partService.showPart(mPart, PartState.ACTIVATE);
}
}
here class to create table
public class TreeTableCreation {
// Injected services
#Inject
#Named(IServiceConstants.ACTIVE_SHELL)
Shell shell;
#Inject
MPart mPart;
public void createTable() {
// public static void main(String[] args) {
// Shell shell;
Display display = new Display();
// final Shell shell = new Shell(display);
// shell = new Shell(Display.getCurrent());
// shell.setSize(500, 500);
shell.setLayout(new FillLayout());
final Tree tree = new Tree(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
tree.setHeaderVisible(true);
tree.setLinesVisible(true);
final TreeViewer v = new TreeViewer(tree);
// Header der
// Tabelle*********************************************************************
String[] titles = { "Datum ", "Testname", "Erfolgreich", "Durchgefallen", "Dauer", "Läufe" };
for (int i = 0; i < titles.length; i++) {
TreeColumn column = new TreeColumn(tree, SWT.CENTER);
column.setText(titles[i]);
column.setWidth(150);
}
v.setLabelProvider(new MyLabelProvider());
v.setContentProvider(new MyContentProvider());
v.setInput(TestResultTest.getData());
// // selecting cells getting
// // items******************************************************
tree.addListener(SWT.MouseDoubleClick, new Listener() {
final int columnCount = 6;
public void handleEvent(Event event) {
Point pt = new Point(event.x, event.y);
TreeItem item = tree.getItem(pt);
int index = tree.indexOf(item);
System.out.println("Item Index-" + index);
if (item == null)
return;
for (int i = 0; i < columnCount; i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
TreeTableCreation2 anothershell = new TreeTableCreation2();
DrawMultipleLine grafshell = new DrawMultipleLine();
anothershell.open();
System.out.println("Läufe gewählt");
grafshell.open();
System.out.println("Dauer gewählt");
}
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}

You can't just convert a standalone SWT program like this. You need to read a tutorial on writing e4 programs such as this one
You must not create a new Display, e4 already has one.
You must not mess around with the current Shell, e4 is in charge of that and has many other objects already in the Shell.
Do not call shell.pack, shell.open or use a display dispatch loop.
Do not dispose of the display.
Do not do ((TreeTableCreation) mPart.getObject()).createTable();. Use the #PostConstruct method of the part.
So something like:
public class TreeTableCreation {
#PostConstruct
public void createTable(Composite parent) {
final Tree tree = new Tree(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
tree.setHeaderVisible(true);
tree.setLinesVisible(true);
final TreeViewer v = new TreeViewer(tree);
// Header der
// Tabelle*********************************************************************
String[] titles = { "Datum ", "Testname", "Erfolgreich", "Durchgefallen", "Dauer", "Läufe" };
for (int i = 0; i < titles.length; i++) {
TreeColumn column = new TreeColumn(tree, SWT.CENTER);
column.setText(titles[i]);
column.setWidth(150);
}
v.setLabelProvider(new MyLabelProvider());
v.setContentProvider(new MyContentProvider());
v.setInput(TestResultTest.getData());
// // selecting cells getting
// // items******************************************************
tree.addListener(SWT.MouseDoubleClick, new Listener() {
final int columnCount = 6;
public void handleEvent(Event event) {
Point pt = new Point(event.x, event.y);
TreeItem item = tree.getItem(pt);
int index = tree.indexOf(item);
System.out.println("Item Index-" + index);
if (item == null)
return;
for (int i = 0; i < columnCount; i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
TreeTableCreation2 anothershell = new TreeTableCreation2();
DrawMultipleLine grafshell = new DrawMultipleLine();
anothershell.open();
System.out.println("Läufe gewählt");
grafshell.open();
System.out.println("Dauer gewählt");
}
}
}
});
}

Related

Save ViewPart on Exit

I am new in GEF & RCP. Now I want to Save my ViewPart on exit for that i have implemented savestate and init method. But I am using a tableItem to fill data in My viewpart. When I try to get text from my table and store them in my savestate nothing is happening. How can I save my data stored in TableItem from savestate and initialize when i start up my graphical editor
public class Theartview extends ViewPart implements Serializable {
private static final long serialVersionUID = -3033215443267698138L;
public static String ID = "TutoGEF.theartview_id";
public Text text;
public Text text_1;
private Table table;
private IMemento memento;
#SuppressWarnings("unused")
private Node node;
private static Node sourceNode;
private static Node targetNode;
private static int connectionType;
TableItem[] items = null;
#Override
public void init(IViewSite site, IMemento memento) throws PartInitException {
this.memento = memento;
super.init(site, memento);
System.out.println("hi there");
}
#Override
public void saveState(IMemento memento) {
super.saveState(memento);
System.out.println("hello there");
}
#SuppressWarnings("static-access")
public void getDataOfConnection(Node sourceNode, Node targetNode,
int connectionType1) {
this.sourceNode = sourceNode;
this.targetNode = targetNode;
this.connectionType = connectionType1;
}
public Theartview() {
}
#Override
public void createPartControl(Composite parent) {
parent.setLayout(new GridLayout(10, false));
table = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 10, 1));
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn tblclmnTheartName = new TableColumn(table, SWT.NONE);// 1
tblclmnTheartName.setWidth(100);
tblclmnTheartName.setText("Theart Name");
TableColumn tblclmnCategory = new TableColumn(table, SWT.NONE);// 2
tblclmnCategory.setWidth(100);
tblclmnCategory.setText("Category");
TableColumn tblclmnCombo = new TableColumn(table, SWT.NONE);// 3
tblclmnCombo.setWidth(100);
tblclmnCombo.setText("Satus");
TableColumn tblclmnCombo_1 = new TableColumn(table, SWT.NONE);// 4
tblclmnCombo_1.setWidth(100);
tblclmnCombo_1.setText("Priority");
TableColumn tblclmnDescription = new TableColumn(table, SWT.NONE);// 5
tblclmnDescription.setWidth(162);
tblclmnDescription.setText("Description");
TableColumn tblclmnJustification = new TableColumn(table, SWT.NONE);// 6
tblclmnJustification.setWidth(212);
tblclmnJustification.setText("Justification");
// getfillTableRoWData();
if (memento != null) {
restoreState(memento);
}
memento = null;
}
void restoreState(IMemento memento) {
}
public void fillTableRoWData() {
try {
if (Connection.Number_Of_Connection != 0) {
TableItem item = new TableItem(table, SWT.NONE);
String tempConType = null;
String sourceNodeName = null;
String targetNodeName = null;
String settingString = null;
for (int s = 1; s <= Connection.Number_Of_Connection; s++) {
if (connectionType == Connection.CONNECTION_DESIGN) {
tempConType = "Deliver Design";
} else if (connectionType == Connection.CONNECTION_RESOURCES) {
tempConType = "Deliver Resource";
} else if (connectionType == Connection.CONNECTION_WORKPACKAGES) {
tempConType = "Distributed Work Packages";
}
// for source
if (Service.class.isInstance(sourceNode)) {
sourceNodeName = "Service-";
} else if (Circle.class.isInstance(sourceNode)) {
sourceNodeName = "Circle-";
}
// for targets
if (Service.class.isInstance(targetNode)) {
targetNodeName = "Service ";
} else if (Circle.class.isInstance(targetNode)) {
targetNodeName = "Circle ";
}
if (sourceNodeName.equals(targetNodeName)) {
settingString = sourceNodeName + tempConType;
} else if (!(sourceNodeName.equals(targetNodeName))) {
settingString = sourceNodeName + targetNodeName
+ tempConType;
}
for (int j = 0; j < 6; j++) {
TableEditor editor = new TableEditor(table);
Text text = new Text(table, SWT.NONE);
editor.grabHorizontal = true;
editor.setEditor(text, item, 0);
text.setText(settingString);
editor = new TableEditor(table);
text = new Text(table, SWT.NONE);
editor.grabHorizontal = true;
editor.setEditor(text, item, 1);
text.setText(settingString);
editor = new TableEditor(table);
Combo Status_Combo = new Combo(table, SWT.NONE);
Status_Combo.add("Mitigated");
Status_Combo.add("Not Applicable");
Status_Combo.add("Not Started");
Status_Combo.add("Needs Investigation");
Status_Combo.setText("Not Started");
editor.grabHorizontal = true;
editor.setEditor(Status_Combo, item, 2);
editor = new TableEditor(table);
Combo priority_Combo = new Combo(table, SWT.NONE);
priority_Combo.add("High");
priority_Combo.add("Medium");
priority_Combo.add("Low");
priority_Combo.setText("High");
editor.grabHorizontal = true;
editor.setEditor(priority_Combo, item, 3);
editor = new TableEditor(table);
text = new Text(table, SWT.NONE);
editor.grabHorizontal = true;
editor.setEditor(text, item, 4);
text.setText(settingString);
editor = new TableEditor(table);
text = new Text(table, SWT.MULTI | SWT.BORDER
| SWT.WRAP | SWT.V_SCROLL);
editor.grabHorizontal = true;
editor.setEditor(text, item, 5);
// text.setText(settingString);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void setFocus() {
}
Your saveState() implementation should store the data you need in order to maintain the state.
See this short tutorial for code example.

SWT Text Listener

I have a Text in SWT:
final Text textArea = new Text(parent, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
textArea.setVisible(false);
textArea.setEditable(false);
textArea.setEnabled(false);
textArea.setText("Scheduler Info");
I have a listener. Once the listener is fired, I would like some data to overwrite again and again in the text area. Is there anyway I can retain the "Scheduler Info" Header in the text area. I do not want the first line to be overwritten. I want the rest of the area to be overwritten.
There are two ways you can do this:
Just use Text#setText(String) with your new String and prepend the original string.
Select everything after the original string and Text#insert(String) your new stuff.
Here is an example with both methods:
private static final String INITIAL_TEXT = "Scheduler Info";
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
final Text text = new Text(shell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
text.setEditable(false);
text.setEnabled(false);
text.setText(INITIAL_TEXT);
Button replace = new Button(shell, SWT.PUSH);
replace.setText("Replace");
replace.addListener(SWT.Selection, new Listener()
{
private int counter = 1;
#Override
public void handleEvent(Event arg0)
{
String replace = INITIAL_TEXT;
for(int i = 0; i < counter; i++)
replace += "\nLine " + i;
text.setText(replace);
counter++;
}
});
Button insert = new Button(shell, SWT.PUSH);
insert.setText("Insert");
insert.addListener(SWT.Selection, new Listener()
{
private int counter = 1;
#Override
public void handleEvent(Event arg0)
{
text.setSelection(INITIAL_TEXT.length(), text.getText().length());
String newText = "";
for(int i = 0; i < counter; i++)
newText += "\nLine " + i;
text.insert(newText);
counter++;
}
});
shell.pack();
shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}

Programmatically scroll ExpandBar

In my code, org.eclipse.swt.widgets.ExpandBar contains multiple ExpandItems. The ExpandBar is setup to scroll. How do I programmetically scroll the ExpandBar? I looked for examples and API but no luck.
Alright, wrap your ExpandBar in a ScrolledComposite and let it handle scrolling.
The advantage of this is that ScrolledComposite has a method called .setOrigin(int, int) which you can use to scroll to a position.
Here is some example code:
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("ExpandBar Example");
final ScrolledComposite scrolledComp = new ScrolledComposite(shell, SWT.V_SCROLL);
final ExpandBar bar = new ExpandBar(scrolledComp, SWT.NONE);
for (int i = 0; i < 3; i++)
{
Composite composite = new Composite(bar, SWT.NONE);
composite.setLayout(new GridLayout());
for (int j = 0; j < 10; j++)
new Label(composite, SWT.NONE).setText("Label " + i + " " + j);
ExpandItem item = new ExpandItem(bar, SWT.NONE, 0);
item.setText("Item " + i);
item.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
item.setControl(composite);
}
bar.getItem(1).setExpanded(true);
bar.setSpacing(8);
/* Make sure to update the scrolled composite when we collapse/expand
* items */
Listener updateScrolledSize = new Listener()
{
#Override
public void handleEvent(Event arg0)
{
display.asyncExec(new Runnable()
{
#Override
public void run()
{
scrolledComp.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
});
}
};
bar.addListener(SWT.Expand, updateScrolledSize);
bar.addListener(SWT.Collapse, updateScrolledSize);
scrolledComp.setContent(bar);
scrolledComp.setExpandHorizontal(true);
scrolledComp.setExpandVertical(true);
scrolledComp.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
shell.setSize(400, 200);
shell.open();
/* Jump to the end */
scrolledComp.setOrigin(0, scrolledComp.getSize().y);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Looks like this after running:
As you can see it's scrolled to the end.
Update
Ok, if you want to jump to the position of a specific item, do the following:
Add a Button to test the functionality. Inside the Listener, get the y position and scroll to it:
Button jumpTo = new Button(shell, SWT.PUSH);
jumpTo.setText("Jump to item");
jumpTo.addListener(SWT.Selection, new Listener()
{
private int counter = 0;
#Override
public void handleEvent(Event e)
{
int y = getYPosition(bar, counter);
/* Increment the counter */
counter = (counter + 1) % bar.getItemCount();
/* Scroll into view */
scrolledComp.setOrigin(0, y);
}
});
Use this method to get the y position:
private static int getYPosition(ExpandBar bar, int position)
{
/* Calculate the position */
int y = 0;
for(int i = 0; i < position; i++)
{
/* Incorporate the spacing */
y += bar.getSpacing();
/* Get the item (On LINUX, use this line) */
ExpandItem item = bar.getItem(bar.getItemCount() - 1 - i);
/* Get the item (On WINDOWS, use this line) */
//ExpandItem item = bar.getItem(i);
/* Add the header height */
y += item.getHeaderHeight();
/* If the item is expanded, add it's height as well */
if(item.getExpanded())
y += item.getHeight();
}
return y;
}

Synchronize Selections in two Tables with the same color

I have two SWT Tables in a Shell.
When the user clicks on a row in one table, I want the corresponding row in the next Table to be selected too.
However, in the current setup (I'm demonstrating with a Snippet I found online) when one row is clicked, the corresponding row is highlighted too but it's in grey. I'd like the selection to be the same purple everywhere.
The Source Code:
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class Snippet234 {
public static void main (String [] args) {
int rowCount = 40;
int columnCount = 15;
final Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
Composite parent = new Composite(shell, SWT.BORDER);
GridLayout layout = new GridLayout(2, false);
layout.marginWidth = layout.marginHeight = layout.horizontalSpacing = 0;
parent.setLayout(layout);
final Table leftTable = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION);
leftTable.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true));
leftTable.setHeaderVisible(true);
final Table rightTable = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
rightTable.setHeaderVisible(true);
GridData table2Data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2);
rightTable.setLayoutData(table2Data);
// Create columns
TableColumn column1 = new TableColumn(leftTable, SWT.NONE);
column1.setText("Name");
column1.setWidth(150);
for (int i = 0; i < columnCount; i++) {
TableColumn column = new TableColumn(rightTable, SWT.NONE);
column.setText("Value "+i);
column.setWidth(200);
}
// Create rows
for (int i = 0; i < rowCount; i++) {
TableItem item = new TableItem(leftTable, SWT.NONE);
item.setText("item "+i);
item = new TableItem(rightTable, SWT.NONE);
for (int j = 0; j < columnCount; j++) {
item.setText(j, "Item "+i+" value # "+j);
}
}
// Make selection the same in both tables
leftTable.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
rightTable.setSelection(leftTable.getSelectionIndices());
}
});
rightTable.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
leftTable.setSelection(rightTable.getSelectionIndices());
}
});
// On Windows, the selection is gray if the table does not have focus.
// To make both tables appear in focus, draw the selection background here.
// This part only works on version 3.2 or later.
/*
Listener eraseListener = new Listener() {
public void handleEvent(Event event) {
event.detail &= ~SWT.HOT;
if((event.detail & SWT.SELECTED) != 0) {
GC gc = event.gc;
Rectangle rect = event.getBounds();
gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
gc.fillRectangle(rect);
event.detail &= ~SWT.SELECTED;
}
}
};
leftTable.addListener(SWT.EraseItem, eraseListener);
rightTable.addListener(SWT.EraseItem, eraseListener);
*/
// Make vertical scrollbars scroll together
ScrollBar vBarLeft = leftTable.getVerticalBar();
vBarLeft.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
rightTable.setTopIndex(leftTable.getTopIndex());
}
});
ScrollBar vBarRight = rightTable.getVerticalBar();
vBarRight.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
leftTable.setTopIndex(rightTable.getTopIndex());
}
});
// Horizontal bar on second table takes up a little extra space.
// To keep vertical scroll bars in sink, force table1 to end above
// horizontal scrollbar
ScrollBar hBarRight = rightTable.getHorizontalBar();
Label spacer = new Label(parent, SWT.NONE);
GridData spacerData = new GridData();
spacerData.heightHint = hBarRight.getSize().y;
spacer.setVisible(false);
parent.setBackground(leftTable.getBackground());
shell.setSize(600, 400);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Ok, I threw something together for you. Don't mind the crappy way of handling the selection, that's not what I want to show. The important part is where I draw the background of the item, i.e. the MyLabelProviders paint() method:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
final TableViewer first = new TableViewer(shell);
addColumn(first);
first.setContentProvider(ArrayContentProvider.getInstance());
final List<MyObject> firstInput = getInput();
first.setInput(firstInput);
first.setLabelProvider(new MyLabelProvider());
final TableViewer second = new TableViewer(shell);
addColumn(second);
second.setContentProvider(ArrayContentProvider.getInstance());
final List<MyObject> secondInput = getInput();
second.setInput(secondInput);
second.setLabelProvider(new MyLabelProvider());
first.addSelectionChangedListener(new ISelectionChangedListener()
{
#Override
public void selectionChanged(SelectionChangedEvent e)
{
int index = first.getTable().getSelectionIndex();
second.getTable().setSelection(index);
setSelection(firstInput, index);
setSelection(secondInput, index);
}
});
second.addSelectionChangedListener(new ISelectionChangedListener()
{
#Override
public void selectionChanged(SelectionChangedEvent e)
{
int index = second.getTable().getSelectionIndex();
first.getTable().setSelection(index);
setSelection(firstInput, index);
setSelection(secondInput, index);
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void setSelection(List<MyObject> input, int index)
{
for(MyObject obj : input)
{
obj.setSelected(false);
}
input.get(index).setSelected(true);
}
private static List<MyObject> getInput()
{
List<MyObject> result = new ArrayList<MyObject>();
result.add(new MyObject("First"));
result.add(new MyObject("Second"));
result.add(new MyObject("Third"));
return result;
}
private static void addColumn(TableViewer viewer)
{
TableViewerColumn col = new TableViewerColumn(viewer, SWT.NONE);
col.getColumn().setWidth(200);
col.getColumn().setText("Name:");
col.setLabelProvider(new ColumnLabelProvider()
{
#Override
public String getText(Object element)
{
MyObject value = (MyObject) element;
return value.getName();
}
});
}
private static class MyObject
{
private String name;
private boolean isSelected;
public MyObject(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public boolean isSelected()
{
return isSelected;
}
public void setSelected(boolean isSelected)
{
this.isSelected = isSelected;
}
#Override
public String toString()
{
return name;
}
}
private static class MyLabelProvider extends StyledCellLabelProvider
{
#Override
protected void paint(Event event, Object element)
{
int width = 1000;
int x = event.x - 2;
int y = event.y;
int height = event.height;
GC gc = event.gc;
MyObject object = (MyObject) element;
Color oldBackground = gc.getBackground();
Color color = object.isSelected ? Display.getCurrent().getSystemColor(SWT.COLOR_LIST_SELECTION) : Display.getCurrent().getSystemColor(SWT.COLOR_LIST_BACKGROUND);
gc.setBackground(color);
gc.fillRectangle(x, y, width, height);
gc.setBackground(oldBackground);
super.paint(event, element);
}
}
Here are two screenshots after selecting the first and then the third row (doesn't matter which TableViewer):

Set width of SWT Table Column

I'm adding column one by one to my swt table. with
int totalwidth=0;
for(String s:phoneErrs){
TableColumn tblclmnError = new TableColumn(tablephone, SWT.CENTER);
tblclmnError.setText(s);
tblclmnError.pack();
totalwidth+=tblclmnError.getWidth();
}
and after this I want to add a last column, that should fill the rest of the
space in table header. Now that I have the total width of the added columns
already, I should be able to calculate the width of my last column and specify
it right? but how? I tried
TableColumn tblclmnComment = new TableColumn(tablephone, SWT.CENTER);
tblclmnComment.setWidth(tablephone.getSize().x-totalwidth);
tblclmnComment.setText("Comment");
but it's not working. the getSize() return 0.
You can achieve this by adding listener on SWT.Resize event type.
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, true));
TableViewer viewer1 = getViewer(shell);
List<String> rows = new ArrayList<String>();
rows.add("Row 1");
rows.add("Row 2");
viewer1.setInput(rows);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private static TableViewer getViewer(final Shell shell) {
TableViewer viewer = new TableViewer(shell, SWT.FULL_SELECTION
| SWT.H_SCROLL | SWT.V_SCROLL | SWT.NONE);
viewer.getTable().addListener(SWT.Resize, new Listener() {
#Override
public void handleEvent(Event event) {
Table table = (Table)event.widget;
int columnCount = table.getColumnCount();
if(columnCount == 0)
return;
Rectangle area = table.getClientArea();
int totalAreaWdith = area.width;
int lineWidth = table.getGridLineWidth();
int totalGridLineWidth = (columnCount-1)*lineWidth;
int totalColumnWidth = 0;
for(TableColumn column: table.getColumns())
{
totalColumnWidth = totalColumnWidth+column.getWidth();
}
int diff = totalAreaWdith-(totalColumnWidth+totalGridLineWidth);
TableColumn lastCol = table.getColumns()[columnCount-1];
//check diff is valid or not. setting negetive width doesnt make sense.
lastCol.setWidth(diff+lastCol.getWidth());
}
});
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.getTable().setLayoutData(
new GridData(SWT.FILL, SWT.FILL, true, true));
TableViewerColumn col = new TableViewerColumn(viewer, SWT.NONE);
col.getColumn().setWidth(100);
col.getColumn().setText("Text Column");
col.setLabelProvider(new ColumnLabelProvider() {
#Override
public void update(ViewerCell cell) {
cell.setText((String) cell.getElement());
}
});
col = new TableViewerColumn(viewer, SWT.NONE);
col.getColumn().setWidth(100);
col.getColumn().setText("Second Text Column");
col.setLabelProvider(new ColumnLabelProvider() {
#Override
public void update(ViewerCell cell) {
cell.setText((String) cell.getElement());
}
});
viewer.getTable().setHeaderVisible(true);
return viewer;
}

Categories