I fight with SWT Table and Hyperlink for too long time. I've read all of the topics on stackoverflow and JFace Snippets and Eclipse SWT Snippets and I couldn't find an answer to my problem.
I try to create SWT Table with column that contains "DELETE" hyperlink in every row. My problem is that I can't make it look ok.
Maybe I will put my code first:
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
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.ui.forms.widgets.Hyperlink;
public class TableWithDelete {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
TableColumnLayout tableLayout = new TableColumnLayout();
composite.setLayout(tableLayout);
TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL);
Table table = tableViewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(false);
TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.BORDER);
TableColumn columnWeight = column.getColumn();
tableLayout.setColumnData(columnWeight, new ColumnWeightData(2, ColumnWeightData.MINIMUM_WIDTH, true));
columnWeight.setText("Name");
column = new TableViewerColumn(tableViewer, SWT.NONE);
columnWeight = column.getColumn();
tableLayout.setColumnData(columnWeight, new ColumnWeightData(1, ColumnWeightData.MINIMUM_WIDTH, true));
columnWeight.setText("Removal");
//fill with some data
String[] sampleNames = new String[]{"Test 1", "Test 2", "Test 3", "Test 4", "Test 5"};
for (String name : sampleNames) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0, name);
}
TableItem[] items = table.getItems();
final List<TableEditor> editors = new ArrayList<TableEditor>();
for (int i = 0; i < items.length; i++) {
TableEditor editor = new TableEditor(table);
Hyperlink hyperlink = new Hyperlink(table, SWT.NONE);
hyperlink.setText("DELETE");
hyperlink.pack();
hyperlink.addListener(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event event) {
System.out.println("Some action on DELETE");
}
});
editor.minimumWidth = hyperlink.getSize().x;
editor.setEditor(hyperlink, items[i], 1);
editors.add(editor);
}
shell.setSize(300, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
My problem is that if I run that code, I retrieve ugly default look of the hyperlink:
When I change style that I pass to Hyperlink to SWT.TRANSPARENT:
Hyperlink hyperlink = new Hyperlink(table, SWT.TRANSPARENT);
than row in table starts to look ok, but "transparency" seems to be passed to other table parts, and because of that border of the table looks ugly:
and what's more when I move mouse over Hyperlink, current row of the table is not highlighted anymore.
What do I miss? How to make SWT Table able to show link in a cell (That does not only look like a hyperlink but also has Mouse cursor correctly changed)?
I will be very grateful for any help.