我有一个使用Scrollable Composite的应用程序窗口。
在可滚动复合内部,我们可以有N个复合(相应的数据库数据)。
如何才能检测儿童复合材料的大小以设置正确的高度?
/**
* Create contents of the application window.
* @param parent
*/
@Override
protected Control createContents(Composite parent) {
final ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.BORDER);
final Composite containerEditarAtendimento = new Composite(sc, SWT.BORDER);
containerEditarAtendimento.setSize(800, 630);
sc.setContent(containerEditarAtendimento);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(containerEditarAtendimento.computeSize(820, 480));
GridLayout gridLayout = new GridLayout(1, false);
gridLayout.marginTop = 5;
gridLayout.marginRight = 5;
gridLayout.marginBottom = 5;
gridLayout.marginLeft = 5;
containerEditarAtendimento.setLayout(gridLayout);
{ // Composite com as informações de atendimento.
EditarAtendimentoComposite editarAtendimentoComposite = new EditarAtendimentoComposite(containerEditarAtendimento, SWT.BORDER);
GridData gd_editarAtendimentoComposite = new GridData(GridData.FILL, GridData.FILL, true, false);
gd_editarAtendimentoComposite.heightHint = 249;
editarAtendimentoComposite.setLayoutData(gd_editarAtendimentoComposite);
}
{
Composite compoExterno = formToolkit.createComposite(containerEditarAtendimento, SWT.BORDER);
compoExterno.setLayout(new GridLayout(3, false));
GridData gd_compoExterno = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1);
gd_compoExterno.heightHint = 153;
compoExterno.setLayoutData(gd_compoExterno);
formToolkit.paintBordersFor(compoExterno);
{
Label lblAnexos = formToolkit.createLabel(compoExterno, getLabel("label.anexos"), SWT.NONE);
}
new Label(compoExterno, SWT.NONE);
{
Button btnAddAnexo = formToolkit.createButton(compoExterno, getLabel("label.incluiranexo"), SWT.NONE);
btnAddAnexo.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
}
{ // Composite com os anexos
AnexosComposite anexosComposite = new AnexosComposite(compoExterno,SWT.BORDER);
GridData gd_anexosComposite = new GridData(SWT.FILL, SWT.TOP, true, false, 3, 1);
gd_anexosComposite.heightHint = 107;
anexosComposite.setLayoutData(gd_anexosComposite);
}
}
{ // Composite onde ficará o expandable do diagnostico
PlanoAcaoExternoComposite planoAcaoComposite = new PlanoAcaoExternoComposite(containerEditarAtendimento, SWT.BORDER);
GridData gd_planoAcaoComposite = new GridData(GridData.FILL, GridData.FILL, true, false);
planoAcaoComposite.setLayoutData(gd_planoAcaoComposite);
formToolkit.adapt(planoAcaoComposite);
formToolkit.paintBordersFor(planoAcaoComposite);
planoAcaoComposite.layout(true, true);
log.info("Height: {}",planoAcaoComposite.getClientArea().height);
log.info("getSize().x: {}",planoAcaoComposite.getSize().x);
log.info("getSize().y: {}",planoAcaoComposite.getSize().y);
sc.setMinSize(containerEditarAtendimento.computeSize(800, 2000)); // HOW TO DYNAMICALLY SET HEIGHT?
}
sc.layout(true,true);
return containerEditarAtendimento;
}
答案 0 :(得分:13)
以下代码会根据内容将ScrolledComposite
设置为minSize
,如果缩小Shell
的大小,则会显示滚动条:
ScrolledComposite sc = new ScrolledComposite(content, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
Composite composite = new Composite(sc, SWT.NONE);
composite.setLayout(new FillLayout(SWT.VERTICAL));
new Label(composite, SWT.NONE).setText("1111");
new Label(composite, SWT.NONE).setText("2222");
new Label(composite, SWT.NONE).setText("3333");
new Label(composite, SWT.NONE).setText("4444");
new Label(composite, SWT.NONE).setText("5555");
new Label(composite, SWT.NONE).setText("6666");
new Label(composite, SWT.NONE).setText("7777");
sc.setContent(composite);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
答案 1 :(得分:0)
滚动窗口的内容需要存在并固定 设置grabExxcessHorizontalspace并垂直于“true”。
public class SWT_Composite_scroll_test {
private static Display display;
private Font font;
public static FontData[] fontdata, tmp;
public static FontData fd;
public String name;
public static String fontname;
public String DIALOG;
public String DIALOGINPUT;
public String MONOSPACED;
public String SERIF;
public String SANS_SERIF;
public String LUCIDIA;
public int size, data, style, Plain=0, Bold=1, Italic=2;
Object[] font_objects;
String[] font_names, styletype = {"Normal","Bold","Italic"};
SWT_Composite_scroll_test(){}
public static void main(String args[]){
display = new Display();
tmp = new FontData[Display.getDefault().getFontList(null, false).length];
for(int j=0;j<tmp.length;j++){ tmp[j] = Display.getDefault().getFontList(null, false)[j];}
System.out.println("System fonts loaded.");
SWT_Composite_scroll_test c = new SWT_Composite_scroll_test ();
c.getFonts();
}
private void getFonts(){
final Shell fontdialog = new Shell(display,SWT.BORDER|SWT.PRIMARY_MODAL|SWT.RESIZE|SWT.CLOSE);
fontdialog.setText(Parent.getResourceString("Set fonts for system. "));
/* Create 3 unevenly columns in dialog*/
fontdialog.setLayout(new GridLayout(3,false));
Label label = new Label(fontdialog,SWT.FILL);
/* set label 3 columns wide, grab all horizontal space, but vertical just one row.*/
label.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false,3,1));
label.setText("Dit is een text.");
final Composite tablecontainer = new Composite(fontdialog,SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
/* Design tablecontainer layout, no spacing to prevent white spots when content is scrolled.*/
GridLayout gl = new GridLayout();
gl.marginLeft = 0;
gl.marginHeight = 0;
gl.marginBottom = 0;
gl.marginRight = 0;
gl.verticalSpacing = 0;
gl.horizontalSpacing = 0;
tablecontainer.setLayout(gl);
/* Completely fill and grab all vertical space for content, one column and one row.*/
final GridData tblgd = new GridData(SWT.FILL,SWT.FILL,true,true,1,1);
tablecontainer.setLayoutData(tblgd);
final Composite tableheader = new Composite(tablecontainer,SWT.FILL|SWT.INHERIT_DEFAULT);
tableheader.setFont(Configuration.default_font);
RowLayout rlb = new RowLayout();
rlb.pack = false;//<-------------(all elements are the same size).
rlb.justify = true;// <------- (elements are spread across the available space).
tableheader.setLayout(rlb);
tableheader.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
final Label subjectcolumn = new Label(tableheader,SWT.FILL|SWT.LEFT|SWT.INHERIT_DEFAULT);
subjectcolumn.setText(Parent.getResourceString("Subject name"));
final Label namecolumn = new Label(tableheader,SWT.FILL|SWT.CENTER|SWT.INHERIT_DEFAULT);
namecolumn.setText(Parent.getResourceString("Font name"));
final Label stylecolumn = new Label(tableheader,SWT.FILL|SWT.CENTER|SWT.INHERIT_DEFAULT);
stylecolumn.setText(Parent.getResourceString("Font style"));
final Label sizecolumn = new Label(tableheader,SWT.FILL|SWT.CENTER|SWT.INHERIT_DEFAULT);
sizecolumn.setText(Parent.getResourceString("Font size"));
final Composite table = new Composite(tablecontainer,SWT.INHERIT_DEFAULT);
RowLayout tbfl = new RowLayout();
tbfl.type = SWT.VERTICAL;
table.setLayout(tbfl);
table.setFont(Configuration.default_font);
final GridData tbge = new GridData(SWT.FILL,SWT.FILL,false,false);
table.setLayoutData(tbge);
Group testgroup = new Group(table, SWT.FILL);
testgroup.setLayout(new GridLayout());
new Label(testgroup,SWT.INHERIT_DEFAULT).setText("This is a test label in side the scrolled content.");
new Label(testgroup,SWT.INHERIT_DEFAULT).setText("This is a test label in side the scrolled content.");
Button resize = new Button(testgroup, SWT.PUSH);
resize.setText("Rezise bigger");
resize.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
tablecontainer.setSize(tablecontainer.getSize().x<<1,tablecontainer.getSize().y<<1); /* resize scrolled composite setting data height x 2;*/
tablecontainer.layout();
}
});
Button resizeS = new Button(testgroup, SWT.PUSH);
resizeS.setText("Rezise smaller");
resizeS.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
tablecontainer.setSize(tablecontainer.getSize().x>>1,tablecontainer.getSize().y>>1); /* resize scrolled composite setting data height x 2;*/
tablecontainer.layout();
}
});
for(int i=0;i<tmp.length;i++){
Composite tablerow = new Composite(table,SWT.FILL);
RowLayout rlc = new RowLayout();
rlc.center = true;
rlc.fill = true;
rlc.pack = false;//<-------------(all elements are the same size).
rlc.wrap = false;// <------- (clips if not enough space). [ This one makes content invisible when out of sight. ]
rlc.marginLeft = 1;
rlc.marginHeight = 1;
rlc.marginBottom = 1;
rlc.marginRight = 1;
rlc.marginWidth = 1;
rlc.spacing = 1;
tablerow.setLayout(rlc);
new Label(tablerow, SWT.INHERIT_DEFAULT).setText(tmp[i].getName());
System.out.println(tableheader.getBounds().width+"2: vPage:"+table.getSize().y+" vSelection:"+tablecontainer.getClientArea().y);
final Combo fontcombo = new Combo(tablerow, SWT.RIGHT);
fontcombo.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
fontcombo.setBackground(display.getSystemColor(SWT.COLOR_RED));
for(int j=0;j<tmp.length;j++){fontcombo.add(""+tmp[j].getName());}
fontcombo.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
fd = Display.getDefault().getFontList(null, true)[fontcombo.getSelectionIndex()];
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {}
});
final Combo stylecombo = new Combo (tablerow, SWT.INHERIT_DEFAULT);
for(int j=0;j<styletype.length;j++){stylecombo.add(styletype[j]);}
stylecombo.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
style = stylecombo.getSelectionIndex();
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {}
});
final Combo sizecombo = new Combo(tablerow,SWT.RIGHT|SWT.INHERIT_DEFAULT);
for(int j=0;j<28;j++){sizecombo.add(""+j);}
sizecombo.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
size = sizecombo.getSelectionIndex();
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {}
});
}
final ScrollBar vBar = tablecontainer.getVerticalBar();
final ScrollBar hBar = tablecontainer.getHorizontalBar();
vBar.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
int contentY = table.getSize().y - tableheader.getSize().y; // total height of the page. [ table ]
int windowY = tablecontainer.getSize().y - tableheader.getSize().y;// total height of the window [ tablecontainer ].
int locationY = vBar.getSelection() - tableheader.getSize().y; // point where table is in window. 0 = top.
int vPageEnd = contentY - windowY;
if(locationY >= vPageEnd){ locationY = vPageEnd ;}
table.setLocation(5, - locationY); // 5 is left margin.
}
});
hBar.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
/* Horizontal scrolbar.*/
int contentX = table.getSize().x - vBar.getSize().x; // total height of the page. [ table ]
int windowX = tablecontainer.getSize().x - vBar.getSize().x; // total height of the window [ tablecontainer ].
int locationX = hBar.getSelection(); // point where table is in window. 0 = top.
int hPageEnd = contentX - windowX;
if(locationX >= hPageEnd){ locationX = hPageEnd ;}
table.setLocation(-locationX+5, tableheader.getSize().y); // 5 is left margin.
}
});
tablecontainer.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(Event e) {
int contentY = table.getSize().y - tableheader.getSize().y-9; // total height of the page. [ table ]
int contentX = table.getSize().x - vBar.getSize().x;
int windowY = tablecontainer.getSize().y - tableheader.getSize().y-9; // total height of the window [ tablecontainer ].
int windowX = tablecontainer.getSize().x - vBar.getSize().x;
int locationY = vBar.getSelection() - tableheader.getSize().y; // point where table is in window. 0 = top.
int locationX = hBar.getSelection();
int vPageEnd = contentY - windowY;
int hPageEnd = contentX - windowX;
vBar.setMaximum(contentY);
hBar.setMaximum(contentX);
if(vPageEnd>contentY){vBar.setThumb(vPageEnd - windowY);}else{vBar.setThumb(windowY);}
if(hPageEnd>contentX){hBar.setThumb(hPageEnd - windowX);}else{hBar.setThumb(windowX);}
if(locationY >= vPageEnd) {locationY = vPageEnd;}
if(locationX >= hPageEnd) {locationX = hPageEnd;}
if(e.widget.equals(vBar)){table.setLocation(5,-locationY);}
else {table.setLocation(-locationX,tableheader.getSize().y);}
}
});
fontdialog.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(Event e) {
}
});
Button save = new Button(fontdialog, SWT.PUSH);
save.setText(Parent.getResourceString("Save new settings"));
save.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
}
});
new Label(fontdialog, SWT.SHADOW_OUT); /* Filler */
Button ok = new Button(fontdialog, SWT.PUSH);
ok.setText(Parent.getResourceString("Done"));
fontdialog.setDefaultButton(ok);
ok.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
fontdialog.dispose();
}
});
fontdialog.pack();
fontdialog.setSize(display.getClientArea().width>>1,display.getClientArea().height>>1);
fontdialog.open();
for(;!fontdialog.isDisposed();) {
if (!fontdialog.getDisplay().readAndDispatch())
fontdialog.getDisplay().sleep();
}
}
}