The swt faq Chinese (13)

Source: Internet
Author: User
Document directory
  • Directly subclass Components

Http://www.eclipse.org/swt/faq.php)

Translation: Frank (AT) zijinshi.cn)

Version: Version 0.9

The SWT FAQ

 25. Q: Why can't I subclass buttons and tables?

A: Actually, you can. However, we do not recommend that you do this. You can read this article to learn how to create your own component: le creating your own widget using SWT.

Directly subclass Components

In extreme cases, you may need to subclass A component. Of course, not justCanvasOrComposite. We strongly recommend that you do not do this unless there is no other way. Before trying to subclass it, try to see if it can be encapsulated. The reason is as follows:

  • Subclass may inherit many meaningless APIs and must be rewritten. In Java, the return type cannot be changed when we override a method, so some methods are implemented again.
  • Subclass is obviously not the best method for extending classes that do not belong to us. If you want to ask me the arguments that support this point of view, you can look at the NovemberBill VennersInJava WorldThis article:Inheritance versus composition: Which one shocould you choose?The address is http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html.
  • The vast majority of components are platform-related, unless you spend a lot of energy to ensure that they can run on all platforms.
  • When the non-api implementation of the superclass changes, the subclass components may be affected.
  • Subclass is prone to system-level defects, and there is a risk of resource leakage. For example, if a subclass implements a method but does not ensure that its superclass still call the code to release resources, it may cause system resource leakage.
  • Different versions may become incompatible. If a method signature or field name is changed, or a new method or field is added, a name conflict may exist in the subclass. OnlyCanvasAndCompositeThis ensures that no name conflict exists in future releases.
  • Refer to Leonid mikhajlov in"Fragile base class problem. You're in http://www.cas.mcmaster.ca /~ Emil/publications/fragile/can easily find the article "A Study of the fragile base class problem.

SubclassCanvasOrCompositeIs the best way to ensure that your components work on any SWT platform. The "is-a" test in the test shows whether your component is a is-a basic component or a compliant component. Make sure that this component is used before subclass of any component.Is a SWT native component that has been subclass of SWT.. For examplePicturelabelIt is a 100% cross-platform, but it is not a nativeLabel.

Subclass any non-CompositeOrCanvasBefore, make sure to override the MethodProtected void checksubclass ()So that this method does not do anything. Make sure that you have read the comments of this method before overwriting it.

26. Q: Why didn't the selection event be triggered when the components in the program change?
A: This is what we did during design, mainly to minimize the number of events that do not need to be sent. For example, SWT does not care about the selected event when the selection status of a control is changed by programming. Otherwise, if you select the event multiple times, the event will be sent multiple times, and the code that receives the event will be executed multiple times, resulting in system efficiency problems. Of course, if you want to do this, you can create EventObject, triggered on the Component Widget.notifyListeners(int, Event)Event. Note that some program-triggered events are triggered, especially some low-level component operations, such as focusing, moving, or changing the size.

(To be continued ...)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.