java - How to get rid of wasted space in SWT ScrolledComposite when the scrollbars are not shown -
i have horizontal scrolledcomposite
holding 2 buttons (actually composite 2 buttons). space scroll bar allocated if scroll bars hidden. (they hidden, when horizontal space sufficient , scrolling not necessary.)
how can rid of wasted space?
in mean time have configured scrolled composite show scrollbars (setalwaysshowscrollbars(true)
), rather prefer rid of wasted space.
thank consideration.
import org.eclipse.swt.swt; import org.eclipse.swt.custom.scrolledcomposite; import org.eclipse.swt.layout.filllayout; import org.eclipse.swt.layout.griddata; import org.eclipse.swt.layout.gridlayout; import org.eclipse.swt.widgets.button; import org.eclipse.swt.widgets.composite; import org.eclipse.swt.widgets.display; import org.eclipse.swt.widgets.shell; public class scrollbarspaceissue extends composite { public scrollbarspaceissue(final composite parent, final int style) { super(parent, style); setlayout(new gridlayout(1, false)); final scrolledcomposite scrolledcomposite = new scrolledcomposite(this, swt.border | swt.h_scroll); scrolledcomposite.setlayoutdata(new griddata(swt.fill, swt.center, true, false, 1, 1)); scrolledcomposite.setexpandhorizontal(true); scrolledcomposite.setexpandvertical(true); //scrolledcomposite.setalwaysshowscrollbars(true); { final composite composite = new composite(scrolledcomposite, swt.none); composite.setlayout(new gridlayout(2, false)); { final button btn1 = new button(composite, swt.none); btn1.settext("button 1"); final button btn2 = new button(composite, swt.none); btn2.settext("button 2"); } scrolledcomposite.setcontent(composite); scrolledcomposite.setminsize(composite.computesize(swt.default, swt.default)); } pack(); } public static void main(final string[] args) { final display display = new display(); final shell shell = new shell(display); shell.settext("testcase"); shell.setlayout(new filllayout()); new scrollbarspaceissue(shell, swt.none); shell.pack(); shell.open(); while (!shell.isdisposed()) { if (!display.readanddispatch()) display.sleep(); } display.dispose(); } }
the reason why there "wasted space" preferred size of scrolledcomposite
object includes room scrollbars, whether visible or not. disable default functionality, can override preferred size calculation extending scrolledcomposite
such:
class packedscrolledcomposite extends scrolledcomposite { point scrollbarsize; // size of os-specific scrollbar public packedscrolledcomposite(composite parent, int style) { super(parent, style); composite composite = new composite(parent, swt.h_scroll | swt.v_scroll); composite.setsize(1, 1); scrollbarsize = composite.computesize(0, 0); composite.dispose(); } public point computesize(int whint, int hhint, boolean changed) { point point = super.computesize(whint, hhint, changed); point.x += ((getstyle() & swt.v_scroll) != 0) ? -scrollbarsize.x : 0; point.y += ((getstyle() & swt.h_scroll) != 0) ? -scrollbarsize.y : 0; return point; } }
Comments
Post a Comment