Using Java SWT to implement the MSN Style dropdown box

Source: Internet
Author: User
Tags border color

SWT a so-called advantage is its localized appearance, because it is a component of the operating system that is called through JNI to ensure the appearance of the needs of the majority of users, but some IM software vendors often want their products have a unique appearance, which for SWT, the original component is a bit powerless, Strictly speaking, if your users are more demanding on appearance, then swing is preferred, because the l&f mechanism will ensure that you do this, and swing has many advantages that SWT does not have, such as translucent components, rendering, etc., but a few of these features can be simulated with SWT, This article will show you how to implement the MSN-style drop-down box with custom components.

Generally, SWT provides a set of components that basically meets the needs of most users, custom components are typically grouped into 2 categories, one that combines several basic components into a composite component (such as a calendar component), and the second is to improve the appearance of existing components to meet customer requirements, or to mix them. Using SWT to implement custom components is usually inherited by composite or canvas, but most of them are implemented by inheritance composite, if you look at SWT's source code, You will find that many SWT advanced components, such as Expandbar, are inherited directly from composite.

If you want to simulate MSN user name input components, you need to collect some data, respectively: Normal, disable the color of the border in both states, normal, disabled the background color in both states, the right Drop-down button icon. Now give these sets of data.

Color of the border under normal state: RGB 170,183,199

Disabled state Border color: RGB 208,215,229

Background color in normal state: RGB 254, 254, 254

Background color in disabled state: RGB 238, 241, 249

Icon for Drop-down button:

Next, you create a class called Comboselector inherited from Composite. It is important to note that this custom component SWT component library is supported under eclipse if a plugin such as ve and Swt-designer can be guided to import the necessary libraries into the engineering classpath, and if an SWT application is deployed, a dynamic library is required. No elaboration is made on how to deploy this article.

Create the above data constants

private final Color ENABLED_LINE_COLOR = new Color(Display.getCurrent(), 170, 183, 199);
private final Color DISABLED_LINE_COLOR = new Color(Display.getCurrent(), 208, 215, 229);

private final Color ENABLED_BG = new Color(Display.getCurrent(), 254, 254, 254);

private final Color DISABLED_BG = new Color(Display.getCurrent(), 238, 241, 249);

private final Image COMBO_ICON = new Image(Display.getDefault(), "combo.png");

In addition you will need a basic text component for input, a menu to display the saved data.

private Text inputText;

private Menu selectorMenu;

These are the variables that are related to the display, but in addition to these, the temporary data is saved, respectively, by the current user-selected item, and the collection of all data items in the Drop-down box. In order to achieve universality and portability, both sets of data are saved with object.

private Object selectedItem;

private Vector dataSet = new Vector();

The constructor is then defined.

public ComboSelector(Composite parent) {...}

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.