gpt4 book ai didi

java - 拖动时滚动表格

转载 作者:行者123 更新时间:2023-12-02 02:32:19 25 4
gpt4 key购买 nike

我想要一个表作为放置目标,因为它实际上与删除项目的位置相关。因此,用户可能希望在开始拖动后重新调整可见项目。

我缺少的默认行为是:将鼠标悬停在最后一个可见行上并向下滚动表格。将鼠标悬停在第一条可见线上并向上滚动。

所以我现在正在做的是尝试将用户悬停的图标滚动到表格的中心:

    final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());

// drag source: the label

final Label label = new Label(shell, SWT.NONE);
label.setText("Drag from me!");
label.setDragDetect(true);
label.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());

final DragSource source = new DragSource(label, DND.DROP_MOVE | DND.DROP_COPY);
source.setTransfer(new Transfer[]{TextTransfer.getInstance()});
source.addDragListener(new DragSourceAdapter() {
@Override
public void dragSetData(DragSourceEvent event) {
event.data = label.getText();
}
});

// drop target: the viewer

final TableViewer viewer = new TableViewer(shell,
SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setLabelProvider(new ColumnLabelProvider());
viewer.setInput(IntStream.range(1, 100).mapToObj(String::valueOf).collect(Collectors.toList()));
viewer.getControl().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).hint(100, 100).create());

final DropTarget target = new DropTarget(viewer.getTable(), DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
target.setTransfer(new Transfer[]{TextTransfer.getInstance()});
target.addDropListener(new DropTargetAdapter() {

private static final int TIME_BETWEEN_MOVEMENTS = 700;
private long lastMovementTime;

@Override
public void dragOver(DropTargetEvent event) {
final Table table = viewer.getTable();
final TableItem item = table.getItem(table.toControl(new Point(event.x, event.y)));
if (item != null) {
final long currentTime = System.currentTimeMillis();
if (currentTime > this.lastMovementTime + TIME_BETWEEN_MOVEMENTS) {
table.setTopIndex(Math.max(0, table.indexOf(item) - getVisibleItems(table) / 2));
this.lastMovementTime = currentTime;
}
table.setSelection(item);
}
}

private int getVisibleItems(Table table) {
return (table.getClientArea().height - table.getHeaderHeight()) / table.getItemHeight();
}
});

// open everything

shell.pack();
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

这对于高 table 滚动很快,而不是小 table ,所以我想知道是否有更好的方法,因为这是我使用的大多数软件中非常常见的用例。

如何在拖动时滚动表格?

最佳答案

好吧,我进行了一些尝试,并提出了一个至少应该为您提供一个良好起点的解决方案。它的作用是监听各种鼠标事件并记住已选择的项目。当到达边缘(顶部或底部)时,它将把表格向上/向下移动一项。

private static boolean dragging = false;
private static int start = -1;
private static int end = -1;

public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());

List<String> input = IntStream.range(1, 100)
.mapToObj(String::valueOf)
.collect(Collectors.toList());

final TableViewer viewer = new TableViewer(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setLabelProvider(new ColumnLabelProvider());
viewer.setInput(input);

Table table = viewer.getTable();
// Stop dragging when the mouse is released
table.addListener(SWT.MouseUp, e -> dragging = false);
// Start dragging when the mouse is clicked
table.addListener(SWT.MouseDown, e -> {
dragging = true;
start = -1;
end = -1;
});
// Listen to mouse movement
table.addListener(SWT.MouseMove, e -> {
// If we're currently dragging
if (dragging)
{
// Get the item under the mouse pointer
TableItem item = table.getItem(new Point(e.x, e.y));

if(item != null)
{
int current = table.indexOf(item);

// If this is the first selected item, remember the position
if (start == -1 && end == -1)
{
start = current;
end = current;
}
// Else, just remember the "end" of the drag
else
end = current;

// Tell the viewer to select the items
viewer.setSelection(new StructuredSelection(input.subList(Math.min(start, end), Math.max(start, end))));

// Now, we actually move the table when the drag movement reaches the top or bottom
int tableHeight = table.getClientArea().height;
int itemHeight = table.getItemHeight();

if (e.y < itemHeight && current > 0)
table.showItem(table.getItem(current - 1));
else if (e.y > tableHeight - itemHeight && current < table.getItemCount() - 1)
table.showItem(table.getItem(current + 1));
}
}
});

shell.pack();
shell.open();
shell.setSize(400, 300);

while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}

关于java - 拖动时滚动表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46905684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com