SWT Custom button

Source: Internet
Author: User

This button mainly combines SWT. Toggle and SWT. Radio to achieve the desired result!
Code:

Import Org. eclipse. SWT. SWT; <br/> Import Org. eclipse. SWT. events. focusevent; <br/> Import Org. eclipse. SWT. events. focuslistener; <br/> Import Org. eclipse. SWT. events. mouseevent; <br/> Import Org. eclipse. SWT. events. mouselistener; <br/> Import Org. eclipse. SWT. graphics. color; <br/> Import Org. eclipse. SWT. graphics. image; <br/> Import Org. eclipse. SWT. widgets. button; <br/> Import Org. eclipse. SWT. widgets. composite; <Br/> Import Org. eclipse. SWT. widgets. control; </P> <p> public class imagebutton extends button implements focuslistener, mouselistener {<br/> private Boolean isselected = false; <br/> private Boolean isusecolorflat = false; </P> <p> Public imagebutton (composite parent, image) {<br/> This (parent, image, false ); <br/>}</P> <p> Public imagebutton (composite parent, image, Boolean flat) {<br/> super (Parent, SWT. toggle); <br/> setimage (image); <br/> addfocuslistener (this); <br/> addmouselistener (this); <br/> isusecolorflat = flat; <br/>}</P> <p> Public void setselection (Boolean selected) {<br/> // todo auto-generated method stub <br/> super. setselection (selected); <br/> If (selected) <br/> clearothersselection (selected ); <br/>}</P> <p>/** <br/> * create data: 2009-3-20 </br> <br/> * This Is method is used to clear the others, when you selected current <br/> * <code> imagebutton <code> <br/> * @ author xiangjie <br/> * @ Param parent <br/> * <br/> */<br/> private void clearothersselection (Boolean selected) {<br/> composite parent = getparent (); <br/> If (parent = NULL) <br/> return; <br/> Control [] controls = parent. getchildren (); <br/> for (INT I = 0; I <controls. length; I ++) {< Br/> If (controls [I] instanceof imagebutton) {<br/> If (! Controls [I]. equals (this) {<br/> (imagebutton) controls [I]). setselection (false); <br/>}</P> <p> Public void focusgained (focusevent E) {<br/> // todo auto-generated method stub <br/> // setselection (true); <br/> If (! Isselected) {<br/> isselected = true; <br/> setselection (true); <br/>}< br/> setselectioncolor (); <br/>}</P> <p> Public void focuslost (focusevent E) {<br/> // todo auto-generated method stub <br/> // setselection (false); <br/> isselected = false; <br/> setselectioncolor (); <br/> // setselection (false); <br/>}</P> <p> Public void mousedoubleclick (mouseevent E) {<br/> // todo auto-generated method stub </P> <p >}</P> <p> Public void mousedown (mouseevent E) {<br/> // todo auto-generated method stub <br/> If (isselected) {<br/> setselection (true); <br/> setselectioncolor (); <br/> return; <br/>}< br/> setselectioncolor (); <br/>}</P> <p> Public void mouseup (mouseevent E) {<br/> // todo auto-generated method stub <br/> If (isselected) {<br/> setselection (true); <br/> setselectioncolor (); <br/> return; <br/>}< br/> setselectioncolor (); <br/>}</P> <p>/** <br/> * create data: 2009-3-13. <br> <br/> * when it's selected, the color of it will be set red, else Is null <br/> * @ author xiangjie <br/> */<br/> protected void setselectioncolor () {<br/> If (isusecolorflat) {<br/> If (isselected) <br/> This. setbackground (new color (null, 255,128, 0); <br/> // [R = 189, G = 149, B = 66] <br/> else <br/> This. setbackground (null ); <br/>}</P> <p>/** <br/> * @ return the isusecolorflat <br/> */<br/> Public boolean isusecolorflat () {<br/> return isusecolorflat; <br/>}</P> <p>/** <br/> * @ Param isusecolorflat <br/> * The isusecolorflat to set <br/> */<br /> Public void setusecolorflat (Boolean isusecolorflat) {<br/> This. isusecolorflat = isusecolorflat; <br/>}</P> <p>/** <br/> * You must inherent this method, please see <a herf = <br/> * "http://www.eclipselight.org/rcp/83/"> inherit the cause of widget error </a> <br/> * @ author Jie. xiang <br/> */<br/> protected void checksubclass () {<br/>}</P> <p>}

Demo:

This article is the original article of eclipselight.org (eclipse light). For more information, see the source.

Fixed Link: http://www.eclipselight.org/swt-jface/329/

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.