[英]How to make the text in an SWT Link widget selectable
I have a text in the Link SWT widget created as follow:我在Link SWT 小部件中创建了一个文本,如下所示:
Link message = new Link(parent, SWT.WRAP);
message.setText(myMessage);
I want the text (in myMessage
variable) be selectable, to grant users to copy it.我希望文本(在myMessage
变量中)是可选的,以允许用户复制它。 How can I do this?我怎样才能做到这一点?
I have used Link widget because I need hyperlinks in the text to be clickable.我使用了链接小部件,因为我需要文本中的超链接可以点击。
The SWT Link
widget is not selectable. SWT Link
小部件不可选择。 To work around this I can think of either为了解决这个问题,我可以想到
Link
with a Copy menu item that copies the text to the clipboard为Link
提供上下文菜单,其中包含将文本复制到剪贴板的复制菜单项Link
that copies the text to the clipboard在将文本复制到剪贴板的Link
旁边放置一个复制(工具)按钮Browser
widget which is selectable but harder to layout and requires extra work to trigger the functinality when the link is selected使用一个Browser
小部件,它是可选的,但更难布局,需要额外的工作才能在选择链接时触发功能org.eclipse.ui.forms
, use the FormText
.如果您不介意对org.eclipse.ui.forms
的额外依赖,请使用FormText
。 The FormText
can show hyperlinks and allows to select and copy text FormText
可以显示超链接并允许选择和复制文本Why not using a StyledText to allow text selection ?为什么不使用 StyledText 来允许文本选择?
String string = "This is sample text with a link and some other link here.";
final StyledText styledText = new StyledText (shell, SWT.MULTI | SWT.BORDER);
styledText.setText(string);
String link1 = "link";
String link2 = "here";
StyleRange style = new StyleRange();
style.underline = true;
style.underlineStyle = SWT.UNDERLINE_LINK;
int[] ranges = {string.indexOf(link1), link1.length(), string.indexOf(link2), link2.length()};
StyleRange[] styles = {style, style};
styledText.setStyleRanges(ranges, styles);
styledText.addListener(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event event) {
// It is up to the application to determine when and how a link should be activated.
// In this snippet links are activated on mouse down when the control key is held down
if ((event.stateMask & SWT.MOD1) != 0) {
try {
int offset = styledText.getOffsetAtLocation(new Point (event.x, event.y));
StyleRange style = styledText.getStyleRangeAtOffset(offset);
if (style != null && style.underline && style.underlineStyle == SWT.UNDERLINE_LINK) {
System.out.println("Click on a Link");
}
} catch (IllegalArgumentException e) {
// no character under event.x, event.y
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.