Java – prevent SWT scrolledcomposite from eating part of it
What did I do wrong?
This is my code excerpt
public void createPartControl(Composite parent) { parent.setLayout(new FillLayout()); ScrolledComposite scroll@R_86_2419@ = new ScrolledComposite(parent,SWT.V_SCROLL); scroll@R_86_2419@.setExpandHorizontal(true); mParent = new Composite(scroll@R_86_2419@,SWT.NONE); scroll@R_86_2419@.setContent(mParent); FormLayout layout = new FormLayout(); mParent.setLayout(layout); // Adds a bunch of controls here mParent.layout(); mParent.setSize(mParent.computeSize(SWT.DEFAULT,SWT.DEFAULT,true)); }
But it clips the last button:
Bigbrother 82: no job
SCDF: I tried your suggestion, and now the scroll bar has disappeared I need more work
Solution
This is a common obstacle when using scrolledcomposite When the SC becomes so small that the scroll bar must be displayed, the client control must shrink horizontally to make room for the scroll bar This has the side effect of wrapping some labels, which makes the following controls move further down, which increases the minimum height required for the content complex
You need to listen for the width change on the content composite (mparent), give the new content width again, calculate the minimum height, and call setminheight() on the rolling composite file with the new height
public void createPartControl(Composite parent) { parent.setLayout(new FillLayout()); ScrolledComposite scroll@R_86_2419@ = new ScrolledComposite(parent,SWT.V_SCROLL); scroll@R_86_2419@.setExpandHorizontal(true); scroll@R_86_2419@.setExpandVertical(true); // Using 0 here ensures the horizontal scroll bar will never appear. If // you want the horizontal bar to appear at some threshold (say 100 // pixels) then send that value instead. scroll@R_86_2419@.setMinWidth(0); mParent = new Composite(scroll@R_86_2419@,SWT.NONE); FormLayout layout = new FormLayout(); mParent.setLayout(layout); // Adds a bunch of controls here mParent.addListener(SWT.Resize,new Listener() { int width = -1; public void handleEvent(Event e) { int newWidth = mParent.getSize().x; if (newWidth != width) { scroll@R_86_2419@.setMinHeight(mParent.computeSize(newWidth,SWT.DEFAULT).y); width = newWidth; } } } // Wait until here to set content pane. This way the resize listener will // fire when the scrolled composite first resizes mParent,which in turn // computes the minimum height and calls setMinHeight() scroll@R_86_2419@.setContent(mParent); }
When listening for size changes, please note that we ignore any sizing events with the same width This is because the change of content height does not affect the minimum height of content, as long as the width is the same