SWT controls with predefined styles

I want to have controls (Button-s, Text-s, etc.) with predefined styles in my SWT project. My first idea was to extend, for example, the org.eclipse.swt.widgets.Text class, set some parameters and use new classes instead of the original, but get an org.eclipse.swt.SWTException: Subclassing not allowed exception. How to do it?

+6
source share
2 answers

You must override the checkSubclass method to do nothing, otherwise it will complain about the subclass being invalid - because usually you should not override the standard components.

 @Override protected void checkSubclass() { // Disable the check that prevents subclassing of SWT components } 

You should also consider creating custom widgets containing primitive controls using delegation. For example, you can create a MyText that will contain a text widget inside with a custom setting.

Remember that SWT provides standard controls that look native to each platform. In any case, polishing of standard components is still permitted and even required in production software.

+8
source

See SWT Faq for this issue. There you will also find a link on how to write custom widgets.

+4
source

Source: https://habr.com/ru/post/909996/


All Articles