滚动窗口的内容需要存在,并且固定设置grabExxcessHorizontalspace和vertical为“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. "));
fontdialog.setLayout(new GridLayout(3,false));
Label label = new Label(fontdialog,SWT.FILL);
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);
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);
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;
rlb.justify = true;
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);
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);
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;
rlc.wrap = false;
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;
int windowY = tablecontainer.getSize().y - tableheader.getSize().y;
int locationY = vBar.getSelection() - tableheader.getSize().y;
int vPageEnd = contentY - windowY;
if(locationY >= vPageEnd){ locationY = vPageEnd ;}
table.setLocation(5, - locationY);
}
});
hBar.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
int contentX = table.getSize().x - vBar.getSize().x;
int windowX = tablecontainer.getSize().x - vBar.getSize().x;
int locationX = hBar.getSelection();
int hPageEnd = contentX - windowX;
if(locationX >= hPageEnd){ locationX = hPageEnd ;}
table.setLocation(-locationX+5, tableheader.getSize().y);
}
});
tablecontainer.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(Event e) {
int contentY = table.getSize().y - tableheader.getSize().y-9;
int contentX = table.getSize().x - vBar.getSize().x;
int windowY = tablecontainer.getSize().y - tableheader.getSize().y-9;
int windowX = tablecontainer.getSize().x - vBar.getSize().x;
int locationY = vBar.getSelection() - tableheader.getSize().y;
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);
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();
}
}
}