我有一段带有ScrolledComposite的代码,其中垂直滚动条不会出现,即使小部件的内容远远小于小部件中的空间。我能够在一个简单的例子中重现这种行为。 我需要这个作为RAP应用程序,但如果我在RCP应用程序的视图中运行它,代码也不起作用。
以下是简单视图的代码(我省略了导入部分):
public class View extends ViewPart {
public static final String ID = "View_spike.view";
List<String> inputData = new ArrayList<String>();
final String LONG_TEXT = "Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text.";
private TableViewer viewer;
/**
* The content provider class is responsible for providing objects to the
* view. It can wrap existing objects in adapters or simply return objects
* as-is. These objects may be sensitive to the current input of the view,
* or ignore it and always show the same content (like Task List, for
* example).
*/
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
public void dispose() {
}
public Object[] getElements(Object parent) {
if (parent instanceof Object[]) {
return (Object[]) parent;
}
return new Object[0];
}
}
class ViewLabelProvider extends LabelProvider implements
ITableLabelProvider {
public String getColumnText(Object obj, int index) {
return getText(obj);
}
public Image getColumnImage(Object obj, int index) {
return getImage(obj);
}
public Image getImage(Object obj) {
return PlatformUI.getWorkbench().getSharedImages().getImage(
ISharedImages.IMG_OBJ_ELEMENT);
}
}
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
populateInputData();
final SashForm container = new SashForm(parent, SWT.HORIZONTAL);
final ScrolledComposite objectViewerContainer = new ScrolledComposite(
container, SWT.H_SCROLL | SWT.V_SCROLL);
objectViewerContainer.setLayout(new GridLayout());
objectViewerContainer.setExpandHorizontal(true);
objectViewerContainer.setExpandVertical(true);
objectViewerContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Composite someContainer = new Composite(container, SWT.NONE);
someContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
container.setWeights(new int[] { 1, 2 });
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
createObjectViewerArea(objectViewerContainer);
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
viewer.getControl().setFocus();
}
private void populateInputData() {
for(int i = 0; i < 100; i++) {
inputData.add(LONG_TEXT);
}
}
private void createObjectViewerArea(final ScrolledComposite parent) {
final Composite panel = new Composite(parent, SWT.NONE);
panel.setLayout(resetMargin(new TableWrapLayout()));
createContent(panel);
panel.setLayoutData(new GridData(GridData.FILL_BOTH));
parent.setContent(panel);
}
private void createContent(final Composite parent) {
final Tree tree = new Tree(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
tree.setLayoutData(new TableWrapData());
TreeViewer treeViewer = new TreeViewer(tree);
treeViewer.setContentProvider(new ITreeContentProvider() {
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void inputChanged(Viewer viewer, Object oldInput,
Object newInput) {
viewer.refresh();
parent.layout();
}
@Override
public Object[] getElements(Object inputElement) {
// TODO Auto-generated method stub
return ((List) inputData).toArray();
}
@Override
public Object[] getChildren(Object parentElement) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getParent(Object element) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean hasChildren(Object element) {
// TODO Auto-generated method stub
return false;
}
});
treeViewer.setInput(this.inputData);
treeViewer.setAutoExpandLevel(1); // don't expand past top level
treeViewer.setLabelProvider(new ILabelProvider() {
@Override
public void removeListener(ILabelProviderListener listener) {
}
@Override
public boolean isLabelProperty(Object element, String property) {
return false;
}
@Override
public void dispose() {
}
@Override
public void addListener(ILabelProviderListener listener) {
}
@Override
public String getText(Object element) {
// TODO Auto-generated method stub
return (String) element;
}
@Override
public Image getImage(Object element) {
// TODO Auto-generated method stub
return null;
}
});
}
public TableWrapLayout resetMargin(final TableWrapLayout layout) {
layout.topMargin = 0;
layout.leftMargin = 0;
layout.bottomMargin = 1;
layout.rightMargin = 0;
layout.horizontalSpacing = 0;
layout.verticalSpacing = 0;
return layout;
}
}
结果如下:
当然,树窗口小部件中的行数多于显示的行数(代码添加了100个对象)。我希望滚动条在树窗口小部件中显示(垂直和水平)。 我的代码中应该改进哪些想法?
答案 0 :(得分:0)
有两种方法可以使用ScrolledComposite(在您的代码段中没有使用)。请参阅ScrolledComposite http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fcustom%2FScrolledComposite.html
的javadoc