gpt4 book ai didi

java - 以编程方式滚动 ExpandBar

转载 作者:行者123 更新时间:2023-11-30 08:24:23 24 4
gpt4 key购买 nike

在我的代码中,org.eclipse.swt.widgets.ExpandBar 包含多个 ExpandItemExpandBar 设置为滚动。如何以编程方式滚动 ExpandBar?我寻找示例和 API,但没有成功。

最佳答案

好的,将您的 ExpandBar 包裹在 ScrolledComposite 中,让它处理滚动。

这样做的好处是 ScrolledComposite 有一个名为 .setOrigin(int, int) 的方法,您可以使用它滚动到某个位置。

下面是一些示例代码:

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

final ScrolledComposite scrolledComp = new ScrolledComposite(shell, SWT.V_SCROLL);

final ExpandBar bar = new ExpandBar(scrolledComp, SWT.NONE);

for (int i = 0; i < 3; i++)
{
Composite composite = new Composite(bar, SWT.NONE);
composite.setLayout(new GridLayout());

for (int j = 0; j < 10; j++)
new Label(composite, SWT.NONE).setText("Label " + i + " " + j);

ExpandItem item = new ExpandItem(bar, SWT.NONE, 0);
item.setText("Item " + i);
item.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
item.setControl(composite);
}

bar.getItem(1).setExpanded(true);
bar.setSpacing(8);

/* Make sure to update the scrolled composite when we collapse/expand
* items */
Listener updateScrolledSize = new Listener()
{
@Override
public void handleEvent(Event arg0)
{
display.asyncExec(new Runnable()
{
@Override
public void run()
{
scrolledComp.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
});
}
};

bar.addListener(SWT.Expand, updateScrolledSize);
bar.addListener(SWT.Collapse, updateScrolledSize);

scrolledComp.setContent(bar);
scrolledComp.setExpandHorizontal(true);
scrolledComp.setExpandVertical(true);
scrolledComp.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));

shell.setSize(400, 200);
shell.open();

/* Jump to the end */
scrolledComp.setOrigin(0, scrolledComp.getSize().y);

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

运行后的样子:

enter image description here

如您所见,它已滚动到末尾。


更新

好的,如果你想跳转到特定项目的位置,请执行以下操作:

添加一个 Button 来测试功能。在 Listener 中,获取 y 位置并滚动到它:

Button jumpTo = new Button(shell, SWT.PUSH);
jumpTo.setText("Jump to item");
jumpTo.addListener(SWT.Selection, new Listener()
{
private int counter = 0;
@Override
public void handleEvent(Event e)
{
int y = getYPosition(bar, counter);

/* Increment the counter */
counter = (counter + 1) % bar.getItemCount();

/* Scroll into view */
scrolledComp.setOrigin(0, y);
}
});

使用此方法获取 y 位置:

private static int getYPosition(ExpandBar bar, int position)
{
/* Calculate the position */
int y = 0;
for(int i = 0; i < position; i++)
{
/* Incorporate the spacing */
y += bar.getSpacing();

/* Get the item (On LINUX, use this line) */
ExpandItem item = bar.getItem(bar.getItemCount() - 1 - i);
/* Get the item (On WINDOWS, use this line) */
//ExpandItem item = bar.getItem(i);

/* Add the header height */
y += item.getHeaderHeight();

/* If the item is expanded, add it's height as well */
if(item.getExpanded())
y += item.getHeight();
}

return y;
}

关于java - 以编程方式滚动 ExpandBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22964093/

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