滚动窗口的内容需要存在,并将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. "));
/* 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();
}
}
}