2016-09-22 2 views
1

ScrolledComposites을 사용하여 동일하게 만드는 코드는 두 개가 나란히 스크롤되는 StyledText을 만들어야합니다. 나는 다른 용도로도 사용하고 있기 때문에 StyledText을 사용하는 제한이 있습니다. StyledText을 사용하여 동일한 것을 만들고 싶습니다. link here.SWT 함께 스크롤하는 두 개의 StyledText 만들기

나는 ScrolledCompositesStyledText으로 대체했지만 동일한 코드를 시도했지만 setOrigin(x , y)이 허용되지 않았습니다.

답변

1

당신은 StyledText#setHorizontalPixel()StyledText#setTopPixel() (및 respecrive 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); 
    }); 
} 
관련 문제