I’m trying to achieve a very basic goal that used to be quite simple in .net platform: create a reusable component and use it in another form. I’ve tryed to do the following:
package ***.composites;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
public class CompTest extends Composite {
/**
* Create the composite.
* @param parent
* @param style
*/
public CompTest(Composite parent, int style) {
super(parent, style);
Composite composite = new Composite(this, SWT.NONE);
composite.setBounds(10, 10, 273, 261);
Button btnCheckButton = new Button(composite, SWT.CHECK);
btnCheckButton.setBounds(82, 112, 93, 16);
btnCheckButton.setText("Check Button");
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
and
package ***.composites;
import org.eclipse.swt.widgets.Display;
public class WindTest {
protected Shell shell;
private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
WindTest window = new WindTest();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 377);
shell.setText("SWT Application");
Composite composite = formToolkit.createComposite(shell, SWT.NONE);
composite.setBounds(10, 10, 173, 105);
formToolkit.paintBordersFor(composite);
}
}
How can I add the first composite class in the second one? There’s a way to do it in design mode? I’m doing the right thing?
I’m a little confused on some of the objects being used in the second part and how they relate to the custom Composite in the first part, but the main thing I’ll note is that you’re not setting a layout on your custom CompTest object. Anytime you’re using SWT widgets they need to reside in a parent Composite that has a layout set on it or nothing will show up. I don’t believe setting arbitrary bounds will work around that restriction.
Also note that Composite and Canvas are automatically extensible, you don’t need to override the “checkSubclass” method.