我与SWT Table和Hyperlink争夺太久了。我已经阅读了有关stackoverflow和JFace Snippets以及Eclipse SWT Snippets的所有主题,但我无法找到问题的答案。
我尝试使用每行包含“DELETE”超链接的列创建SWT表。 我的问题是我不能让它看起来不错。
也许我会把我的代码放在第一位:
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();
}
}
我的问题是,如果我运行该代码,我会检索超链接的丑陋默认外观:
当我将传递给超链接的样式更改为SWT.TRANSPARENT时:
Hyperlink hyperlink = new Hyperlink(table, SWT.TRANSPARENT);
比表中的行开始看起来没问题,但“透明度”似乎传递给其他表部分,因为表的边框看起来很难看:
当我将鼠标移到超链接上时更是如此,表格的当前行不再突出显示。
我想念什么?如何使SWT表能够在单元格中显示链接(这不仅看起来像一个超链接,但也正确地更改了鼠标光标)?
我将非常感谢任何帮助。
答案 0 :(得分:1)