【发布时间】:2019-05-18 18:33:21
【问题描述】:
如何使用 Java 和 SWT 将 .lnk 文件成功拖到 Windows 10 任务栏(“固定到任务栏”)?我尝试了以下代码(拖动标签的内容),但无论我使用什么操作常量,它都会在 Windows 10 任务栏上显示不允许拖动的光标。
import org.eclipse.swt.*;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class DragTest {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Label label = new Label(shell, SWT.BORDER);
label.setText("Start drag from here");
final Transfer[] types = new Transfer[] {FileTransfer.getInstance()};
final int operations = DND.DROP_LINK; // DND.DROP_COPY or DND.DROP_MOVE
final DragSource source = new DragSource(label, operations);
source.setTransfer(types);
source.addListener(DND.DragSetData,
event -> event.data = new String[] {
"C:\\ThunderbirdPortable\\ThunderbirdPortable - Shortcut.lnk"
});
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
注意,从 Windows 资源管理器中拖动文件可以正常工作,因此文件是正确的。
【问题讨论】:
-
这里有什么问题?你有什么例外吗?
-
不,我只是得到一个不允许拖动的光标。我需要进行哪些更改才能使其正常工作?
-
其他任务栏功能在 SWT 中通过实际调用平台函数实现来实现。例如。任务项的进度动画在 org.eclipse.swt.widgets.TaskItem.setProgress(int) 中。也许您可以在那里挖掘并找到一种无需拖放即可实现此功能的方法。
标签: java windows drag-and-drop swt