【问题标题】:SWT Create Two StyledText That Scroll In TandemSWT 创建两个串联滚动的 StyledText
【发布时间】:2016-09-22 10:46:35
【问题描述】:
我必须创建两个串联滚动的StyledText,我得到的只是使用ScrolledComposites 创建相同的代码。我有使用StyledText 的限制,只是因为我也将它用于其他目的。
我想创建相同的东西link here,但使用StyledText。
我尝试使用相同的代码将 ScrolledComposites 替换为 StyledText ,但它不允许我使用 setOrigin(x , y) 。
【问题讨论】:
标签:
java
swt
scrollbar
styledtext
tandem
【解决方案1】:
您可以使用StyledText#setHorizontalPixel() 和StyledText#setTopPixel() 方法(以及它们各自的get 方法)来获取和设置位置:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
StyledText one = new StyledText(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
StyledText two = new StyledText(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
one.setAlwaysShowScrollBars(true);
two.setAlwaysShowScrollBars(true);
one.setText("Scroll scroll scroll\ndown down down\nto to to\nsee see see\nthe the the\nstyled styled styled\ntexts texts texts\nscroll scroll scroll\nin in in\ntandem tandem tandem");
two.setText("Scroll scroll scroll\ndown down down\nto to to\nsee see see\nthe the the\nstyled styled styled\ntexts texts texts\nscroll scroll scroll\nin in in\ntandem tandem tandem");
handleVerticalScrolling(one, two);
handleHorizontalScrolling(one, two);
shell.pack();
shell.setSize(200, 100);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void handleHorizontalScrolling(StyledText one, StyledText two)
{
ScrollBar hOne = one.getHorizontalBar();
ScrollBar hTwo = two.getHorizontalBar();
hOne.addListener(SWT.Selection, e -> {
int x = one.getHorizontalPixel();
two.setHorizontalPixel(x);
});
hTwo.addListener(SWT.Selection, e -> {
int x = two.getHorizontalPixel();
one.setHorizontalPixel(x);
});
}
private static void handleVerticalScrolling(StyledText one, StyledText two)
{
ScrollBar vOne = one.getVerticalBar();
ScrollBar vTwo = two.getVerticalBar();
vOne.addListener(SWT.Selection, e ->
{
int y = one.getTopPixel();
two.setTopPixel(y);
});
vTwo.addListener(SWT.Selection, e ->
{
int y = two.getTopPixel();
one.setTopPixel(y);
});
}