gpt4 book ai didi

java - 布局忽略了希望增长的控件?

转载 作者:行者123 更新时间:2023-12-01 23:13:48 26 4
gpt4 key购买 nike

我正在 Eclipse 插件中创建向导。其中一个向导页面包含一个 TableView,它非常高。根据 TableView 的大小,这使得整个向导窗口也很高。

目前我正在使用FillLayout,就像这样

    @Override
public void createControl(Composite superParent) {

//superParent.setLayout(new FillLayout());


// creating "main" control
Composite control = new Composite(superParent, SWT.NONE) {
@Override
public String toString() {
return super.toString() + ": control of CoreSettingsPage";
}
};
control.setLayout(new FillLayout());
//control.setLayoutData(null);


// creating viewer
viewer = new TreeViewer(control, SWT.BORDER);
viewer.setContentProvider(new TreeNodeContentProvider());

那么,如何让向导窗口忽略TreeView的大小,从而出现滚动条呢?或者SWT中可能有一些布局忽略了包含控件的大小?就像 Swing 中的 JScrollPane 一样?

更新1

下面是完整的向导代码以及一些尝试过的选项

package try_eclipsewizardlayout_01;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

public class TryWizard extends Wizard {

abstract class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
}

public class Page1 extends WizardPage {

protected Page1() {
super("Page 1");
}

@Override
public void createControl(Composite parent) {

Composite control = new Composite(parent, SWT.NONE) {
@Override
public String toString() {
return super.toString() + ": control of Page1";
}
};
control.setLayout(new RowLayout(SWT.VERTICAL));

Group group = new Group(control, SWT.NONE);
group.setLayout(new GridLayout(1, false));
group.setText("Options");

Button option1 = new Button(group, SWT.RADIO);
option1.setText("Option 1");

Button option2 = new Button(group, SWT.RADIO);
option2.setText("Option 2");

Button option3 = new Button(group, SWT.RADIO);
option3.setText("Option 3");

setControl(control);


}

}

public class Page2 extends WizardPage {

TableViewer viewer;

private void createViewer(Composite child) {

viewer = new TableViewer(child, SWT.BORDER);

viewer.setContentProvider(new IStructuredContentProvider() {

@Override
public void dispose() {
}

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}

@Override
public Object[] getElements(Object inputElement) {
Object[] ans = new Object[100];
for(int i=0; i<ans.length; ++i) {
ans[i] = new String[] {"name " + i, "value " + i};
}
return ans;
}

});

viewer.setLabelProvider(
new TableLabelProvider() {

@Override
public String getColumnText(Object element, int columnIndex) {
return ((String[])element)[columnIndex];
}

@Override
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
}
);

// dummy data which is ignored
viewer.setInput(new Object());
}

protected Page2() {
super("Page 2");
}

/*
* this version shows normal sized wizrd (not tall)
* but no table on Page 2
*/
@Override
public void createControl(Composite parent) {

ScrolledComposite control = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL) {
@Override
public String toString() {
return super.toString() + ": control of Page2";
}
};

// Create a child composite to hold the controls
Composite child = new Composite(control, SWT.NONE);
child.setLayout(new FillLayout());

createViewer(child);

control.setContent(child);
control.setMinSize(100, 100);

setControl(control);
}

/*
@Override
public void createControl(Composite parent) {

Composite control = new Composite(parent, SWT.BORDER) {
@Override
public String toString() {
return super.toString() + ": control of Page2";
}
};


createViewer(control);


// control.setLayout(new FillLayout()); // tall wizard, table fills it, vertical scroll present

control.setLayout(new GridLayout(1, false));
viewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); // tall wizard, table fills it, vertical scroll present
//viewer.getTable().setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true)); // tall wizard, thin table, vertical scroll present
//viewer.getTable().setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false)); // tall wizard, thin table, no vertical scroll
//viewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); // tall wizard, thin table, no vertical scroll


setControl(control);

}
*/

}

Page1 page1 = new Page1();
Page2 page2 = new Page2();

@Override
public void addPages() {
addPage(page1);
addPage(page2);
}

@Override
public boolean performFinish() {
return false;
}

}

整个简单的 eclipse 项目 SSCCE 在这里:https://drive.google.com/file/d/0By8pZ9a2478Yb2l4TUp6WTh1UGs/edit?usp=sharing

最佳答案

您可以使用org.eclipse.swt.layout.GridLayout(1, false)作为树的布局:

GridData gd= new GridData(GridData.FILL, GridData.BEGINNING, true, true);
gd.heightHint= 200;
viewer.getTree().setLayout(new GridLayout());
viewer.getTree().setLayoutData(gd);

如果您不确定您的superParent是什么布局,然后为了安全起见,为您的查看器创建合成,如下所示:

Composite c = new Composite(shell, SWT.NONE);
c.setLayout(new GridLayout());
c.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
viewer = new TreeViewer(c, SWT.BORDER);

关于java - 布局忽略了希望增长的控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21528272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com