将 2 个 jface TreeViewers 垂直对齐到特定元素

发布于 2024-12-28 06:58:49 字数 662 浏览 0 评论 0原文

我在页面上有两个 TreeViewer 对象(2 列,每列中有一个 TreeViewer),并且我想在滚动或选择另一个树时垂直对齐一棵树。

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Tree;

我认为解决方案应该类似于

treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
  @Override
  public void selectionChanged(SelectionChangedEvent arg0) {
     TreeViewer mirrorTree = (treeViewer == treeVwrSource ? treeVwrTarget : treeVwrSource);
     // find position of selected element (element x) in treeViewer
     // set position of element x in mirrorTree, it is already selected.
  }
});

“有什么建议吗?”

I have two TreeViewer objects on a page (2 columns, one TreeViewer in each column), and I want to vertically align a tree when the other is scrolled or selected.

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Tree;

I think the solution should look something like

treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
  @Override
  public void selectionChanged(SelectionChangedEvent arg0) {
     TreeViewer mirrorTree = (treeViewer == treeVwrSource ? treeVwrTarget : treeVwrSource);
     // find position of selected element (element x) in treeViewer
     // set position of element x in mirrorTree, it is already selected.
  }
});

Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2025-01-04 06:58:49

根据我的阅读,滚动表或树是不可能直接实现的(除非同时发生变化)。以编程方式更改滚动条滑块的位置不会更改表/树的视口。

但是您可以尝试以下代码片段是否适合您:

Tree tree1 = ...;
Tree tree2 = ...;

int topIndex = tree1.indexOf(tree1.getTopItem());
tree2.setTopItem(tree2.getItem(topIndex));

您可以在注册到树的垂直滚动条 (tree.getVerticalBar()) 的 SelectionListener 中调用该代码。

同步选择相当容易(如果两个树查看器显示相同的输入/模型):(

viewer.setSelection(otherViewer.getSelection)

由问题中的 ISelectionChangedListener 调用)。

From what I've read, scrolling tables or trees isn't directly possible (unless that has changed in the meantime). Programmatically changing the position of the thumb of the scrollbar would not change the viewport of the table/tree.

But you could try if the following snippet works for you:

Tree tree1 = ...;
Tree tree2 = ...;

int topIndex = tree1.indexOf(tree1.getTopItem());
tree2.setTopItem(tree2.getItem(topIndex));

You would call that code in a SelectionListener registered to the vertical scrollbar of your tree (tree.getVerticalBar()).

Synchronizing the selection is fairly easy (if both tree viewers display the same input/model):

viewer.setSelection(otherViewer.getSelection)

(called by the ISelectionChangedListener from your question).

何止钟意 2025-01-04 06:58:49

有关如何同步两个表的完整示例,请参阅此 SWT 代码段

For a complete example of how to synchronize two tables see this SWT Snippet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文