2012-03-24 2 views
4

양식 섹션 내에 SWT 테이블을 표시하려고하지만 열 크기 조정 및 스크롤에 문제가 있습니다.SWT 섹션 테이블 및 스크롤

이클립스 plugin의 ViewPart에 배치 할 수있는 몇 가지 샘플 코드를 첨부했습니다 (3.7은 내가 사용하고있는 버전 임). 코드를 실행하고 오른쪽의 열의 크기를 조정하면 테이블에 가로 스크롤 막대가 생깁니다.

이제 섹션을 축소하면 ("섹션 제목"의 왼쪽에있는 화살표를 클릭하십시오),보기에는 가로 스크롤 막대가 생깁니다 (원하지 않습니다). 섹션을 다시 확장하면 뷰 스크롤이 남아 있고 테이블에 더 이상 테이블이 없습니다.

보기보다 넓은 테이블을 막을 수있는 방법 (아마도 중첩 된 합성물의 약간의 끔찍한 조합)을 찾고있어 어떤 제안이라도 고맙게 생각합니다.

public class ExampleView extends ViewPart { 
    /** Note: other fields/methods removed to condense snippet */ 

    private FormToolkit toolkit; 
    private ScrolledForm scrolledForm; 

    public void createPartControl(Composite parent) { 
      parent.setLayout(new FillLayout()); 

    toolkit = new FormToolkit(parent.getDisplay()); 
    scrolledForm = toolkit.createScrolledForm(parent); 
    scrolledForm.setExpandHorizontal(true); 
    scrolledForm.setText("Form Title"); 

    scrolledForm.getBody().setLayout(new FillLayout()); 
    // here's the modification for the solution 
    scrolledForm.getBody().setLayout(
      new BoundedLayout(new FillLayout(), true)); 

    final Section section = toolkit.createSection(scrolledForm.getBody(), 
      Section.DESCRIPTION | Section.TITLE_BAR | Section.TWISTIE 
        | Section.EXPANDED); 

    section.addExpansionListener(new ExpansionAdapter() { 
     public void expansionStateChanged(ExpansionEvent e) { 
      scrolledForm.reflow(true); 
     } 
    }); 
    section.setText("Section Title"); 

    final Table table = toolkit.createTable(section, SWT.FULL_SELECTION); 
    TableViewer viewer = new TableViewer(table); 

    table.setLinesVisible(true); 
    table.setHeaderVisible(true); 
    table.setItemCount(10); 

    TableColumn col = new TableColumn(table, SWT.NONE); 
    col.setText("Column A"); 
    col.setWidth(150); 
    col.setResizable(true); 
    col.setMoveable(true); 

    TableColumn col2 = new TableColumn(table, SWT.NONE); 
    col2.setText("Column B"); 
    col2.setWidth(150); 
    col2.setResizable(true); 
    col2.setMoveable(true); 

    section.setClient(table); 
    } 
} 
+0

마이너 업데이트를 그대로 적용해야 성가신 틱 반사 해킹 있습니다

- 나는 소스에서 참조가 section.setLayout'호출 (새 FillLayout());'는 섹션 –

답변

1

그래서 여기에 맞춤 레이아웃을 사용하는 해결책이 있습니다. 다른 레이아웃을 래핑하지만 computeSize을 부모 컴포지트의 너비 또는 높이에 바인딩 할 수 있습니다. 기본적으로 한 방향으로 스크롤 만 허용합니다. (더 쉬운 방법을 사용하면 ScrolledForm에이를 알려주십시오!). computeSizelayout 방법 protected

import java.lang.reflect.Method; 

import org.eclipse.swt.graphics.Point; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Layout; 

public class BoundedLayout extends Layout { 
    protected Layout delegateLayout; 

    protected Method computeSizeMethod; 
    protected Method layoutMethod; 

    protected boolean widthBound; 

    public BoundedLayout(Layout delegateLayout, boolean widthBound) { 
     setDelegateLayout(delegateLayout); 
     this.widthBound = widthBound; 
    } 

    public Layout getDelegateLayout() { 
     return delegateLayout; 
    } 

    public void setDelegateLayout(Layout delegateLayout) { 
     this.delegateLayout = delegateLayout; 

     try { 
      computeSizeMethod = delegateLayout.getClass().getDeclaredMethod(
       "computeSize", Composite.class, int.class, int.class, 
       boolean.class); 
      computeSizeMethod.setAccessible(true); 

      layoutMethod = delegateLayout.getClass().getDeclaredMethod(
       "layout", Composite.class, boolean.class); 
      layoutMethod.setAccessible(true); 
     } catch (Exception e) { 
     throw new RuntimeException(e); 
     } 
    } 

    @Override 
    protected Point computeSize(Composite composite, int wHint, int hHint, 
     boolean flushCache) { 
    // get comp size to make sure we don't let any children exceed it 
    Point compSize = composite.getSize(); 

    try { 
     Point layoutComputedSize = (Point) computeSizeMethod.invoke(
       delegateLayout, composite, wHint, hHint, flushCache); 

     if (widthBound) { 
      layoutComputedSize.x = Math.min(compSize.x, 
        layoutComputedSize.x); 
     } else { 
      layoutComputedSize.y = Math.min(compSize.y, 
        layoutComputedSize.y); 
     } 

     return layoutComputedSize; 
    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } 
    } 

    @Override 
    protected void layout(Composite composite, boolean flushCache) { 
    try { 
     layoutMethod.invoke(delegateLayout, composite, flushCache); 
    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } 
    } 
} 
+0

나는 같은 문제가 있었는데, 나는 정말로 그것을 조사하지 않았다. 세부 사항 그러나 어딘가에 내가 어떤 LayoutData를 테이블에 정의하고 0으로 widthHint를 주면 모든 것이 잘 작동한다. GridData tableLayoutData = new GridData (GridData.FILL_BOTH); tableLayoutData.widthHint = 0; table.setLayoutData (tableLayoutData); – Mathias

+0

물론 heightHint가 필요합니다. – Mathias

0

섹션에 Section.COMPACT 스타일을 추가하면 섹션의 너비가 확장 상태로만 조정됩니다. 접힌 상태에서 정상 크기로 돌아갑니다.

다시 확장 한 후에 섹션의 너비가 증가하지 않게하려면 ExpansionAdapter에서 대안을 사용해야합니다. 접을 때 너비를 수동으로 저장하고 펼칠 때 너비를 다시 설정합니다.

Btw, GUI를 작성하여 물건을 조정하고 이동하기가 더 쉽도록 WindowBuilder을 사용하는 것이 좋습니다.

+0

에 의해 무시됩니다. 시간을내어 답변 해 주셔서 감사합니다. 'Section.COMPACT' 스타일은 당신이 말한 것과 똑같지 만,'ExpansionAdapter'도 들여다 보았습니다. 그러나 여전히 레이아웃 (수동으로 크기를 설정하려는 시도를 무시합니다)에 좌우되는 것처럼 보입니다. 나는 기본적으로'FillLayout'과 비슷한 내 자신의 레이아웃을 작성하려고하지만,'computeSize' 메소드에서 테이블의 기본 크기를 무시하는 마법을 쓸 것이라고 생각한다. –